How to enable logging in mysql j connector driver?
I used the following command while creating the connection
"jdbc:mysql://localhost/test?logger=com.mysql.jdbc.log.StandardLogger&profileSQL=true";
My log4j property file is like this
# Root logger option
log4j.rootLogger=TRACE, rfile
log4j.logger.com.mysql=trace, rfile
log4j.appender.rfile = org.apache.log4j.RollingFileAppender
log4j.appender.rfile.File =PrepStmt.log
log4j.appender.rfile.MaxFileSize = 100KB
log4j.appender.rfile.Append = true
log4j.appender.rfile.layout = org.apache.log4j.PatternLayout
log4j.appender.rfile.layout.ConversionPattern= %d [%t] %-5p %c %x - %m%n
No log is getting captured in my log file.
Mysql does not know about your log4j properties.
From the docs, scroll to section Debugging/Profiling:
logger
The name of a class that implements "com.mysql.jdbc.log.Log" that will
be used to log messages to. (default is
"com.mysql.jdbc.log.StandardLogger", which logs to STDERR)
Default: com.mysql.jdbc.log.StandardLogger
So your mysql logs simply go to STDERR. If you want to log to a log4j defined destination you can implement com.mysql.jdbc.log.Log, forward to a log4j logger, and specify that implementation class in the connect URL.
You could configure log4jdbc to intercept the JDBC connection and log the sql.
Related
I have maven application with log4j.properties in it with setting to write the logs to a specified file instead of console. When I run the EAR on one of the websphere servers it is creating the file as expected and writing logs to it. But, when I am running the same EAR on other webspehere server it is writing to console instead of writing the logs to the specified file. I have checked the permissions and everything seems to be fine. Please help me in identifying what the issue is. Thanks in advance.
# CONSOLE APPENDER (stdout)
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=DEBUG
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d] [%t] %-5p %20c - %m%n
# ROLLING FILE APPENDER (on the file system) for memberpolicyattributesservice code
log4j.appender.xxxxService=org.apache.log4j.RollingFileAppender
log4j.appender.xxxxService.Threshold=DEBUG
log4j.appender.xxxxService.File=/var/logs/xxxServer1/xxxServiceLog.log
log4j.appender.xxxxService.layout=org.apache.log4j.PatternLayout
log4j.appender.xxxxService.layout.ConversionPattern=%d{MM-dd#HH:mm:ss} %-5p (%13F:%L) %3x - %m%n
log4j.appender.xxxxService.MaxFileSize=10000KB
log4j.appender.xxxxService.MaxBackupIndex=30
log4j.appender.xxxxService.layout=org.apache.log4j.PatternLayout
log4j.appender.xxxxService.layout.ConversionPattern=[%d] [%t] %-5p %20c - %m%n
# ROLLING FILE APPENDER (on the file system) for hiberate, open source code log files
log4j.appender.open_source_code=org.apache.log4j.RollingFileAppender
log4j.appender.open_source_code.layout=org.apache.log4j.PatternLayout
log4j.appender.open_source_code.Threshold=DEBUG
#message format:YYYY-MM-DD HH:mm:ss,ms [ThreadId] <PRIORITY> classname.message
log4j.appender.open_source_code.layout.ConversionPattern=%d [%t]<%-5p> %c.%m \r\n
#file that will be logged to
log4j.appender.open_source_code.File=/var/logs/xxxServer1/open_source_code.log
log4j.appender.open_source_code.Append=true
log4j.appender.open_source_code.MaxFileSize=1024KB
log4j.appender.open_source_code.MaxBackupIndex=5
#turn on log4j verbose mode
log4j.debug = true
# Set root logger level to INFO and its appender to DSInstrumentor,stdout.
log4j.rootLogger=DEBUG,stdout,xxxxService
# YOUR CATEGORIES (to customize logging per class/pkg/project/etc)
log4j.category.fatal=FATAL,xxxxService
log4j.category.error=ERROR,xxxxService
#This will also enable the logging for all the children (packages and classes) of this package
log4j.logger.com.xxxxx=ALL,xxxxService
# Print only messages of level INFO in the open source code
log4j.logger.org=INFO,open_source_code
You have multiple loggers defined in your root logger (DEBUG, stdout, xxxxService) but the xxxxService loggers look like they're bound to the file system on one of the systems:
log4j.appender.open_source_code.File=/var/logs/xxxServer1/open_source_code.log
Make sure that path is valid for each server in your WAS cluster.
As a side note, you should probably avoid using debug and stdout on a remote server. This is fine for local development in your workstation, but not on a remote box. Instead, provide different log4j properties on your various deployment tiers. This lets you customize the log location or appender (say c:\temp or CONSOLE on your desktop, but /var/logs ... on all remote machines) as well as your log levels (DEBUG for desktop, maybe INFO for QA or Staging, and WARN or ERROR for Production).
My log messages always show up as "info" in the app engine logging console, even if i log a message as error (see screenshot).
I log a message like that:
private static Logger logger = Logger.getLogger(PingServlet.class);
logger.error("Database was successfully pinged!!.");
The configs like this:
src/main/resources/log4j.properties
# Root logger option
log4j.rootLogger=INFO, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
src/main/webapp/WEB-INF/logging.properties
# A default java.util.logging configuration.
# (All App Engine logging is through java.util.logging by default).
#
# To use this configuration, copy it into your application's WEB-INF
# folder and add the following to your appengine-web.xml:
#
# <system-properties>
# <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
# </system-properties>
#
# Set the default logging level for all loggers to WARNING
.level = INFO
How can I change that? I want to show an error as error and so on.
If you are logging with log4j the log messages are being printed to the "console" with ConsoleAppender which AppEngina then collects in its own log. The two log parts you have are not connected.
you either need to use java.util.logging directly instead of log4j or use slf4 to redirect log4j to java.util.logging (a bit tricky, read the documentation carefully)
I have a web application. When I run 'mvn test', it logs the debug messages to console, as I have configured it. But when I deploy it to tomcat, I don't see the the logs of the application. I am absolutely sure that I got the log4j.properties file on the right place in the war, as when I change values in the deployed /var/lib/tomcat7/webapps/worldmodel/WEB-INF/classes/log4j.properties for root logger or for hibernate and touch web.xml, I see/chease to see debug logs for hibernate. But I cannot get my application's debug messages to be logged with any configuration I've tried.
Here is how I do the logging:
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
logger = Logger.getLogger(BaseObject.class);
log(Level.DEBUG,"message");
Here is log4j.properties for testing:
log4j.rootLogger=INFO, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding=UTF-8
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.SYSLOG = org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.syslogHost = 127.0.0.1
log4j.appender.SYSLOG.layout = org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.SYSLOG.Facility = LOCAL0
log4j.logger.org.rulez.magwas.worldmodel=DEBUG
#log4j.logger.org.hibernate.SQL=debug
#log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=trace
#log4j.logger.org.hibernate.type=trace
here is the log4j.properties which gets deployed:
log4j.rootLogger=INFO, SYSLOG
log4j.appender.SYSLOG = org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.syslogHost = 127.0.0.1
log4j.appender.SYSLOG.layout = org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.SYSLOG.Facility = LOCAL0
log4j.logger.org.rulez.magwas.worldmodel=DEBUG
#log4j.logger.org.hibernate.SQL=debug
#log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=trace
#log4j.logger.org.hibernate.type=trace
I get such lines in mvn test:
2013-05-21 07:12:17,751 [main] DEBUG org.rulez.magwas.worldmodel.BaseObject- setValue0e9é 072r 074t 0e9é 06bk
The whole project: https://github.com/magwas/worldmodel/commit/c6b08da0a733d9b61257c669e0cc4af9e59444be
edit:
ok, forget it, the code (getter and setter methods in a bean) seems not being been called, perhaps hibernate sets/gets the values directly?
Since, you're configuring your ROOT logger with level INFO it won't display any DEBUG level messages.
log4j.rootLogger=INFO, SYSLOG
The above rootLogger should be configured as DEBUG to see debug and other higher levels (Fatal, Error, Warn and Info) in your logs.
log4j.rootLogger=DEBUG, SYSLOG
I'm not sure what your Maven test case configuration is but perhaps the log4j.properties you've shared isn't having any effect there.
EDIT: It seems from your edit that your logging in production is misconfigured. If your logging is set to INFO it should not log DEBUG messages. I mean, that simply defeats the purpose of having log levels, clearly shows Log4j has not been configured properly and drains Prod resources both space and time.
How can I disable Struts2 zillions of logs?
I constantly get logs like this:
2012-04-12 23:20:31,487 DEBUG [XWorkConverter.java:388] : Property: menuExpandedOps
I'm using struts 2.0 and standard java logging (I'm not using log4j).
The logging.properties file of the JVM is set by default to INFO, and I already have struts.devMode = false in my struts.properties file.
If you are free to use log4j, i suggest you to use that, since Log4j has the ability to set different log levels for different packages and hence using the log level OFF we can disable logging for a particular package.
In S2 most of the log messages will be from these packages
xwork2
struts2
freemarker
ognl
and a simple property file/xml file in class-path can help you to turn on or off the logging information.
and a simple property file/xml file in class-path can help you to turn on or off the logging information.
For those who want an example:
1) Put log4j.properties inside src (where your packages are):
2) Turn OFF low-level logging:
# root logger option
log4j.rootLogger=DEBUG, stdout, file
# direct log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%p: %d{yyyy-MM-dd HH:mm:ss} %F:%L] %m%n
# direct log messages to a log file, support file rolling
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/sortingmonitor.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p: %d{yyyy-MM-dd HH:mm:ss} %F:%L] %m%n
# disable apache low level logging
log4j.category.com.opensymphony.xwork2=OFF
log4j.category.org.apache.struts2=OFF
log4j.category.freemarker.beans=OFF
log4j.category.freemarker.cache=OFF
Further reading for XML: http://deepaksrivastav.github.io/blog/2011/01/06/disable-struts-2-log-messages/
I am using log4j for my application - there I need to see all log messages, so I defined
# General configuration
log4j.rootLogger = ALL, ConsoleAppender
# Appender configuration
log4j.appender.ConsoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.ConsoleAppender.layout.ConversionPattern = %5p (%c) %m%n
But, after I started using Apache's HTTPClient library (which is awesome btw), my console got clouded up by its logging - everything was logged and I am not able to see the log output from my own application any more.
So how can I tell the HTTPClient library that it should only log WARN messages, while my own application still logs on ALL levels?
I create my application logger using
Logger logger = Logger.getLogger(MyClass.class);
PropertyConfigurator.configure("log4j.properties");
It's quite simple. Just add the below line.
log4j.logger.org.apache.commons.httpclient=WARN
Similarly, you can configure levels for any package com.foo or class com.foo.Bar by appending it to the prefix log4j.logger as follows:
log4j.logger.com.foo=MYLEVEL
log4j.logger.com.foo.Bar=DEBUG