Logging Spring bean initialization with Log4J - java

When I run my application, it stops when beans are initializing but doesn't show any logs entries. So I don't know what happened:
Log4j.properties
log4j.rootLogger=DEBUG, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
org.springframework=DEBUG
org.springframework.beans.factory.support=DEBUG
log4j.logger.org.springframework.beans.factory.support=DEBUG
log4j.logger.org.springframework.beans=DEBUG
log4j.category.org.springframework.beans.factory=DEBUG
log4j.logger.org.springframework=DEBUG
log4j.logger.org.hibernate.hql.ast.AST=info
log4j.logger.org.hibernate.tool.hbm2ddl=warn
log4j.logger.org.hibernate.cache=info
log4j.logger.org.hibernate.jdbc=debug
log4j.logger.org.hibernate.type=trace
log4j.additivity.org.hibernate.SQL=false
log4j.logger.org.hibernate.transaction=debug
log4j.logger.java.sql.Statement=DEBUG
log4j.appender.stdout.layout.ConversionPattern=%d %t %C{1}- %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${log4j.appender.R.File}
log4j.appender.R.MaxFileSize=2MB
log4j.appender.R.MaxBackupIndex=0
log4j.appender.R.Append=true
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %t (%l) - %m%n
I want to get something like:
"BeanName" initialized
"BeanName" initialized
etc...
So then I would know where the initialization stopped. Is it possible to get such an output in the logs, when beans are initializing?

You need to set "org.springframework.beans.factory.support.DefaultListableBeanFactory" to debug level. The output looks something like this:
... - Creating instance of bean ...
... - Finished creating instance of bean ...
Update:
Add this to log4j.properties:
log4j.logger.org.springframework.beans.factory.support.DefaultListableBeanFactory=DEBUG
Keep in mind that Spring is using the commons-logging framework, therefore these lines will not appear in your Log4J logs. To redirect them use SLF4J. Add slf4j-api.jar, jcl-over-slf4j.jar, slf4j-log4j12.jar and log4j.jar to your lib directory and remove commons-logging.jar from it.

add to your log4j xml (check if you already have one with lavel ERROR, change it either INFO or DEBUG
<logger name="org.springframework">
<level value="INFO" />
</logger>

Related

How to make simple log4j configuration?

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?

Shunting certain org.apache.log4j log messages off to a particular log file

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.

Disable Struts2 logs

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/

Log4j output on console instead of configured file

I configured Log4j to log into a file but it is instead logging to stdout.
it creates the log file but it does not write to it, instead to stdout.
Here is my config file:
log4j.rootCategory=INFO, file, mail
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/path-to/jobs-batch.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[jobs-batch] %p [%t] %c{1}.%M(%L) | %m%n
log4j.logger.org.springframework.jdbc=WARN
# Configuration for receiving e-mails when ERROR messages occur.
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.To=xxx#xxx.com
log4j.appender.mail.From=xxx#xxx.com
log4j.appender.mail.SMTPHost=mail.xxx.de
log4j.appender.mail.Threshold=ERROR
log4j.appender.mail.Subject=Jobs Batch Error
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d %-5p %c %x - %m%n
any ideas?
Thanks
Adjust to suite your needs:
log4j.rootCategory=DEBUG, C
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%d{MMddyyyy HH:mm} %-5p %c{1}:%L - %m%n
log4j.appender.C.Target=System.out
Things to check:
What is the name of your Log4j property file? The default is log4j.properties, which is what the Log4j library will look for on startup.
Related to what Thorbjorn asks in his comment, where is the log4j property file located? Placing it in the default package will ensure it's found (by default Log4j searches the classpath).
Optionally specify the location of the log4j property file using the log4j.configuration property when starting the JVM.
i was stupid by myself.
i was using the -Dlog4j.configuration to set my file.
BUT i was also configuring log4j programmatically to use the SAME configuration file.
i still do not understand why it did behave like that since i was just setting the SAME configuration file twice. But once i did it only once it worked as expected.

Tomcat 6 Log4j Configuration - stdout still being written to?

I have followed the directions to configure tomcat 6 to log using log4j from here: Logging in Tomcat
However, I have noticed that threads appear to be logging to stdout, with no regard to the log level set in log4j.properties. Here is my log4j.properties file:
log4j.rootLogger=WARN, CATALINA
# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.file=${catalina.base}/logs/tomcat.out
log4j.appender.CATALINA.encoding=UTF-8
# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.CATALINA.layout=org.apache.log4j.PatternLayout
log4j.appender.CATALINA.append=true
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.encoding=UTF-8
log4j.appender.CONSOLE.conversionPattern = %d [%t] %-5p %c- %m%n
# Configure which loggers log to which appenders
#log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST
#log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=INFO, MANAGER
#log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=INFO, HOST-MANAGER
log4j.logger.org.hibernate.hql.ast=ERROR
This is the file that's in my ${CATALINA_BASE}/lib directory. Notice the last line where I set org.hibernate.hql.ast level to ERROR. The following is constantly being spewed into my tomcat.out log:
15663118 [http-8080-1] WARN org.hibernate.hql.ast.QueryTranslatorImpl - firstResult/maxResults specified with collection fetch; applying in memory!
15663531 [TP-Processor7] WARN org.hibernate.hql.ast.QueryTranslatorImpl - firstResult/maxResults specified with collection fetch; applying in memor
62100 [http-8080-18] INFO org.hibernate.cache.StandardQueryCache - starting query cache at region: video
I have a similar log4j.properties file deployed in my webapp under WEB-INF/classes that have similar properties (but logs to a different file). How come the log statements do not follow the conversion pattern I have defined, nor follow the log level limit I set?
I'm running tomcat 6.0.30, Java 1.6.0_23, and log4j 1.2.16 on SunOS 5.10 amd64. The output to my application log works as expected. Thanks in advance for your help.
Andrew
The main problem here is that you're using the old properties file -style way of configuring Log4J. Because most people use XML configuration method these days, you'll be hard pressed to find anyone who can answer the question.
However, what you need to do is to set threshold for each appender. This is how it's done in XML:
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
...
<param name="Target" value="System.out"/>
<param name="Threshold" value="WARN"/>
...
</appender>
It might look like his on flat file:
log4j.appender.Console.Threshold=WARN

Categories