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

6

63

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);
Flite answered 12/11, 2009 at 22:48 Comment(1)
I'm still having problems with this - I have added a log4j.properties file to the console app's root but neither adding log4j.rootLogger=WARN, R or log4j.logger.org.apache.axiom=WARN seem to do anything - I get the same error as before.Flite
C
65

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.

Caston answered 12/11, 2009 at 23:13 Comment(3)
Right now I'm just trying to run some code as a console app. I'll see if I can figure out where the log4j.properties file should go. Thanks.Flite
category is deprecated in log4j with loggerPlovdiv
As an important note from log4j docs: "This observation had previously led us to choose category as the central concept of the package. However, since log4j version 1.2, Logger class has replaced the Category class. For those familiar with earlier versions of log4j, the Logger class can be considered as a mere alias to the Category class."Genie
C
9

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.

Clive answered 7/7, 2010 at 6:53 Comment(0)
A
7

This work for my:

log4j.logger.org.hibernate.type=trace

Also can try:

log4j.category.org.hibernate.type=trace
Almeta answered 14/6, 2012 at 13:18 Comment(0)
T
3

I just encountered the issue and couldn't figure out what was going wrong even after reading all the above and everything out there. What I did was

  1. Set root logger level to WARN
  2. Set package log level to DEBUG

Each logging implementation has it's own way of setting it via properties or via code(lot of help available on this)

Irrespective of all the above I would not get the logs in my console or my log file. What I had overlooked was the below...


enter image description here


All I was doing with the above jugglery was controlling only the production of the logs(at root/package/class etc), left of the red line in above image. But I was not changing the way displaying/consumption of the logs of the same, right of the red line in above image. Handler(Consumption) is usually defaulted at INFO, therefore your precious debug statements wouldn't come through. Consumption/displaying is controlled by setting the log levels for the Handlers(ConsoleHandler/FileHandler etc..) So I went ahead and set the log levels of all my handlers to finest and everything worked.

This point was not made clear in a precise manner in any place.

I hope someone scratching their head, thinking why the properties are not working will find this bit helpful.

Treacy answered 24/9, 2017 at 20:51 Comment(0)
A
0

set the system property log4j.debug=true. Then you can determine where your configuration is running amuck.

Aphotic answered 26/1, 2016 at 18:47 Comment(0)
T
0

Log4J2 appears to support changing log levels at runtime using environment variables.

For example, this configuration will use the LOG_LEVEL environment variable, or default to info if the env var is not set:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <!-- other config elements -->
    <Loggers>
        <Root level="${env:LOG_LEVEL:-info}">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>
Tully answered 26/4 at 17:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.