Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you Change a Package's Log Level using Log4j?

I've encountered the following bug:

http://issues.apache.org/jira/browse/AXIS2-4363

It states the following:

This error only occurs when log level for org.apache.axiom is DEBUG so a workaround is to set log level > DEBUG.

My question is HOW do I go about doing that? I've been scouring my directories for a properties file or something and I've been looking to see if there was something I could set in code, but I really have no idea what I'm doing. I'm running a console app on my desktop right now while trying to get this to work.

Update 1: I noticed that my Axis2 directory has its own log4j.properties file in its root. Is this safely ignored or is it part of the solution (or part of the problem)?

Update 2: The root level log4j.properties file is apprently not set correctly. Right now it looks like this:

log4j.rootLogger=DEBUG, R  log4j.logger.org.apache.axiom=WARN log4j.appender.R=org.apache.log4j.RollingFileAppender  log4j.appender.R.MaxFileSize=10MB  log4j.appender.R.MaxBackupIndex=10  log4j.appender.R.layout=org.apache.log4j.PatternLayout  log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 

but that is apparently wrong as this code returns "Log level is null":

System.out.println("Log level is " + logger.getLevel()); 

For now I am setting the log level in code using

Logger logger = Logger.getLogger("org.apache.axiom"); logger.setLevel(Level.WARN); 
like image 911
Ryan Elkins Avatar asked Nov 12 '09 22:11

Ryan Elkins


People also ask

How do you change the logger level?

To change log levels as a root user, perform the following: To enable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=DEBUG) To disable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=INFO)

How do you change the log level in slf4j?

When using log4j, the Logger. log(Priority p, Object message) method is available and can be used to log a message at a log level determined at runtime. We're using this fact and this tip to redirect stderr to a logger at a specific log level. slf4j doesn't have a generic log() method that I can find.

What is the default logging level in log4j?

Note that by default Log4j assigns the root logger to Level. ERROR.


2 Answers

Which app server are you using? Each one puts its logging config in a different place, though most nowadays use Commons-Logging as a wrapper around either Log4J or java.util.logging.

Using Tomcat as an example, this document explains your options for configuring logging using either option. In either case you need to find or create a config file that defines the log level for each package and each place the logging system will output log info (typically console, file, or db).

In the case of log4j this would be the log4j.properties file, and if you follow the directions in the link above your file will start out looking like:

log4j.rootLogger=DEBUG, R  log4j.appender.R=org.apache.log4j.RollingFileAppender  log4j.appender.R.File=${catalina.home}/logs/tomcat.log  log4j.appender.R.MaxFileSize=10MB  log4j.appender.R.MaxBackupIndex=10  log4j.appender.R.layout=org.apache.log4j.PatternLayout  log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 

Simplest would be to change the line:

log4j.rootLogger=DEBUG, R 

To something like:

log4j.rootLogger=WARN, R 

But if you still want your own DEBUG level output from your own classes add a line that says:

log4j.category.com.mypackage=DEBUG 

Reading up a bit on Log4J and Commons-Logging will help you understand all this.

like image 182
Benjamin Cox Avatar answered Sep 25 '22 00:09

Benjamin Cox


I encountered the exact same problem today, Ryan.

In my src (or your root) directory, my log4j.properties file now has the following addition

# https://issues.apache.org/jira/browse/AXIS2-4363 log4j.category.org.apache.axiom=WARN 

Thanks for the heads up as to how to do this, Benjamin.

like image 33
ian_scho Avatar answered Sep 26 '22 00:09

ian_scho