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.
Related
I added Log4j2 to the project, and added config.
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# 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
Than, I log code like here:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
...
final static Logger logger = LogManager.getLogger(AbstractEditor.class);
...
logger.info("updated: " + entity);
logger.debug("==> debug");
logger.info("==> info");
logger.warn("==> warn");
logger.error("==> error");
logger.fatal("==> fatal");
logger.trace("==> trace");
As I understand, all logs with level higher than DEBUG must be written to console and file. But only this has been printed into console:
15:08:52.285 [http-nio-8080-exec-1] ERROR ru.example.AbstractEditor - ==> error
15:08:52.292 [http-nio-8080-exec-1] FATAL ru.example.AbstractEditor - ==> fatal
I see this strings not matches my config. And they are not witten into file. When I added this config, all logs disappeared from console, excluding this 2 strings.
Please help to write config to see all logs with level from DEBUG on console and file.
You are programmatically using Log4j 2 but using the configuration format of Log4j 1. Log4j 2 is ignoring your configuration and using the default. You have 2 choices.
Convert your configuration to use Log4j 2 syntax (recommended), or
Enable Log4j 2's experimental support for log4j 1 configuration files by setting the system property "log4j1.compatibility=true". This support was added in release 2.13.0 so you would have to be using that version. The property file would also have to be named log4j.properties, not log4j2.properties.
my problem is that I can not see the JUnit log4j logs of my integration tests, when I run them on jenkins.
When I start them from my local eclipse everything works fine. I tried Java Utils Logging before that worked fine as well.
I tried lots of log4j.properties settings. The current one looks as follows:
# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE
# Set the enterprise logger priority to FATAL
log4j.logger.org.apache.axis2.enterprise=FATAL
log4j.logger.de.hunsicker.jalopy.io=FATAL
log4j.logger.httpclient.wire.header=FATAL
log4j.logger.org.apache.commons.httpclient=FATAL
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %m%n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=axis2.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
What am I doing wrong? The log4j.properties file is placed in /src/main/resources. As there is no No log4j appenders-warning in the log, I assume that it is in the classpath and consumed.
I found the solution in Logging while testing through Gradle
The problem was in the gradle settings:
testLogging {
events 'passed', 'failed', 'standardError'
showStandardStreams = true
}
I am working on a java application and I am using log4j for logging. My "log4j.properties" file (in WEB-INF/conf) is here -
log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=C:/billingAutomationService/infotrac20.log
log4j.appender.R.MaxFileSize=30MB
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
In my application there are a some debug messages with logger.debug("debug message"). But with the configuration I ca not see these debug message in my log file - "stdout.log". To see the debug message in my log file ("stdout.log") I have added the following line in my log4j.properties file -
log4j.rootLogger=debug,stdout
But adding this line enables all 'DEBUG' level on my jar/lib along the whole project. I am using a log of spring, hibernate and other jars/libs. This incident makes my log file ("stdout.log") very large even on a single operation. Because a lots of 'DEBUG' message coming from the standard lib/jars which I don't want to debug. Is there any way to turn off the debug message coming form the lib/jars?
Thanks
Please check the below thread.
log4j: package-specific logging
Usually you would have a common package starting for all your files, only enable debug for those package names.
Use root logger as warn.
Also.
Specify only some packages to have debug output
I want to make to log file to my application.
I try Appach log4j for this. In Java code i says:
Logger log = Logger.getLogger(uploadfile.class);
log.debug("Start");
I create file src\log4j.properties with:
# ***** Set root logger level to DEBUG and its only appender to A.
log4j.rootLogger=DEBUG, R, A
log4j.rootLogger=INFO, A
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.MaxFileSize=100mb
log4j.appender.R.MaxBackupIndex=1000
log4j.appender.R.File=c:/uploaded_files/server.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# ***** A is set to be a ConsoleAppender.
log4j.appender.A=org.apache.log4j.ConsoleAppender
# ***** A uses PatternLayout.
log4j.appender.A.Threshold=INFO
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.logger.org.hibernate.type.BasicTypeRegistry=INFO
log4j.logger.com.opensymphony.xwork2.config.providers.XmlConfigurationProvider=INFO
I copy this there. And i get error:
13.02.2013 9:03:34 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [uploadfile] in context with path [/gis-mrsk-portlet] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoClassDefFoundError: Could not initialize class org.apache.log4j.LogManager
at org.apache.log4j.Logger.getLogger(Logger.java:117)
at test.uploadfile.doPost(uploadfile.java:35)
What i doing wrong? And how to configure log4j for simple?
UPDATE
If i use this config i get log file but its empty:
Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE, LOGFILE
Set the enterprise logger priority to FATAL
log4j.logger.org.apache.axis2.enterprise=FATAL
log4j.logger.de.hunsicker.jalopy.io=FATAL
log4j.logger.httpclient.wire.header=FATAL
log4j.logger.org.apache.commons.httpclient=FATAL
CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %m%n
LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=C:/uploaded_files/LogFile.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
It seems correct.. have you set correctly the log4j Lib in your path?
The problem is not with your configuration, the problem is the non-existant support of log4j for catalina it's not as easy to get it to work together as it should be. To make log4j work with catalina follow the documentation available at http://tomcat.apache.org/tomcat-6.0-doc/logging.html#Using_Log4j.
EDIT
To summarize the link I gave:
[...] put log4j.jar and log4j.properties into WEB-INF/lib and WEB-INF/classes of your web application.
Download or build tomcat-juli.jar and tomcat-juli-adapters.jar that are available as an "extras" component for Tomcat. See Additional Components documentation for details. http://tomcat.apache.org/tomcat-6.0-doc/extras.html
After following those steps your example should already work.
/EDIT
If you want the easy part in logging I would suggest using logback which comes with native servlet support for tomcat based applications. If you want to go one step further in logging: clusterlog.net (full disclosure I own part of that company)
java.lang.NoClassDefFoundError
It looks like your Apache does not know definition of org.apache.log4j.LogManager. Have you put required jar in Apache classpath?
We already have extensive logging in our application using org.apache.log4j. We now want to shunt some of these messages off to a new XML log file (while continuing to go to the original log file).
Is this possible? Is there a way we can identify these messages and send them someplace special in addition to the regular log file?
I think you can define two file appenders. One at the root level and other at the package(select the package level as appropriate) level as below:
# Root logger option
log4j.rootLogger=DEBUG, RootFileAppender
#Shunted Logger option
log4j.logger.com.shunted=ERROR,ShuntedFileAppender
# RootFileAppender - used to log messages in the root.log file.
log4j.appender.RootFileAppender=org.apache.log4j.FileAppender
log4j.appender.RootFileAppender.File=root.log
log4j.appender.RootFileAppender.MaxFileSize=100MB
log4j.appender.RootFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RootFileAppender.layout.ConversionPattern= %5p [%t] (%F:%L) - %m%n
# ShuntedFileAppender - used to log messages in the shunted.log file.
log4j.appender.ShuntedFileAppender=org.apache.log4j.FileAppender
log4j.appender.ShuntedFileAppender.File=shunted.log
log4j.appender.ShuntedFileAppender.MaxFileSize=10MB
log4j.appender.ShuntedFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ShuntedFileAppender.layout.ConversionPattern= %5p [%t] (%F:%L) - %m%n
Please Note: You can define the two logger option as different levels as well. In above example, ROOT is defined at DEBUG while shunted is devined as ERROR level.