Kliver Max
Kliver Max

Reputation: 5299

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

Upvotes: 2

Views: 8181

Answers (3)

Sebastian van Wickern
Sebastian van Wickern

Reputation: 1749

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:

  1. [...] put log4j.jar and log4j.properties into WEB-INF/lib and WEB-INF/classes of your web application.

  2. 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)

Upvotes: 1

Vargan
Vargan

Reputation: 1327

It seems correct.. have you set correctly the log4j Lib in your path?

Upvotes: 1

Michał Herman
Michał Herman

Reputation: 3587

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?

Upvotes: 0

Related Questions