Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to log only one level messages with Log4J

If I set the log level to DEBUG, All messages with log level >= DEBUG will be logged/printed. But can I set log level to only DEBUG, such that messages with log level only with DEBUG will be printed. Or can give a range like print all messages whose log level is >=DEBUG but < ERROR?

like image 660
24 revs, 18 users 67% Avatar asked May 04 '10 07:05

24 revs, 18 users 67%


People also ask

What is the lowest level of logging?

Trace is of the lowest priority and Fatal is having highest priority. Below is the log4j logging level order. Trace < Debug < Info < Warn < Error < Fatal.

What is the default logging level in log4j?

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

Which is invalid logging level in log4j?

If a logger or appender is configured with an undefined level, that logger or appender will be invalid and will not process any log events.


2 Answers

Maybe you can use a LevelMatchFilter?

At some situation, You have to write logs to different outputs according to the level. how can it be done by simply configuration of Log4j? There are some methods below.

http://wiki.apache.org/logging-log4j/LogToAppenderByLevel

like image 187
Jarle Hansen Avatar answered Oct 25 '22 12:10

Jarle Hansen


As said Jarle you have to use LevelMatchFilter. I will demonstrate it with one simple exam:

log4j.rootLogger = WARN, admin 
log4j.appender.admin=org.apache.log4j.rolling.RollingFileAppender
        log4j.appender.admin.rollingPolicy = org.apache.log4j.rolling.TimeBasedRollingPolicy
        log4j.appender.admin.rollingPolicy.FileNamePattern = Files\\TestLevels-%d{dd-MM-yyy}.txt
        log4j.appender.admin.layout = org.apache.log4j.PatternLayout
        log4j.appender.admin.layout.ConversionPattern = Date: %d{dd-MM-yyyy} Time: %d{HH:mm:ss} Message [%m]%n
        log4j.appender.admin.filter.01=org.apache.log4j.varia.LevelMatchFilter
        log4j.appender.admin.filter.01.LevelToMatch=FATAL
        log4j.appender.admin.filter.01.AcceptOnMatch=false
        log4j.appender.admin.filter.02=org.apache.log4j.varia.LevelMatchFilter
        log4j.appender.admin.filter.02.LevelToMatch=ERROR
        log4j.appender.admin.filter.02.AcceptOnMatch=true
        log4j.appender.admin.filter.03=org.apache.log4j.varia.LevelMatchFilter
        log4j.appender.admin.filter.03.LevelToMatch=WARN
        log4j.appender.admin.filter.03.AcceptOnMatch=false

In my source I append only ERROR messages to file with name TestLevels.txt

like image 31
Armer B. Avatar answered Oct 25 '22 13:10

Armer B.