问题
I used log4j to log some steps in my application. To be quick and dirty, I used:
org.apache.log4j.BasicConfigurator.configure();
This output my logs in the Eclipse console.
I want to know if and how to set the level threshold higher than DEBUG? In other word, I do not want to display DEBUG level message, just ERR, WARN, INFO.
Thank you.
EDIT: May I use this following?
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
[...]
Logger logger = Logger.getLogger(this.class);
logger.setLevel(Level.INFO);
回答1:
I think the simplest way would be:
Logger.getRootLogger().setLevel(Level.INFO);
回答2:
org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO);
回答3:
Assuming you are calling BasicConfigurator.configure() before any loggers are called:
You can use either of these config files to change it without recompiling:
log4j.properties
log4j.rootLogger=INFO
log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<root>
<priority value="INFO"/>
</root>
</log4j:configuration>
One of these must be on command line.
回答4:
1) Find your appender, you sould have something like this in your log4j.xml configuration file.
<appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/logs/rmDebug.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="1500KB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="**FOOBAR** %d{dd.MM.yyyy HH:mm:ss} %c %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
You see levelMin and LevelMax value ? levelMin is where you begin to log, and levelMax, where you stop to log. ( with this specific appender ). You can have several appender.
Then for assign this appender to a class or package. You can do something like that :
<category name="com.foobar.automation.doremiResourceManager" additivity="true">
<appender-ref ref="DEBUG"/>
</category>
回答5:
If you are not configuring inside the properties file, use this:
Logger root = Logger.getRootLogger();
root.setLevel(Level.INFO);
root.addAppender(new ConsoleAppender(
new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
回答6:
What BasicConfigurator.configure() do is adding root logger to a ConsoleAppender and set the appender layout to a PatternLayout with the pattern "%r [%t] %-5p %c - %m%n". So you need to set the root logger's level. If you only set the level of the logger of this class, the level of root logger is unchanged, then all the other loggers(except this class's) may still use root logger's level, so you will still see the unwanted logs.
See http://logging.apache.org/log4j/1.2/manual.html.
来源:https://stackoverflow.com/questions/1509324/log4j-basic-configuration