Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the same logger to log different levels to console + logfile?

I have a log4j logger that currently writes the log both to console and to a file, which works fine.

Later I'd like to configure it to log INFO + ERROR to the logfile, but only show ERROR on console. What would I have to change to achieve this?

log4j.rootLogger=INFO, console, MyFileAppender

log4j.logger.org.apache.cxf=INFO, console
log4j.logger.org.apache.cxf.interceptor.LoggingInInterceptor=INFO, console
log4j.logger.org.apache.cxf.interceptor.LoggingOutInterceptor=INFO, console

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out

log4j.appender.MyFileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.MyFileAppender.Append=true
log4j.appender.MyFileAppender.File=c:/logs.log

Further, I'd like to prevent the CXF XML requests to be logged in the file. But I want them still to be shown in the console. How?

like image 760
membersound Avatar asked Nov 05 '13 12:11

membersound


People also ask

How do I create a multiple logging level in Python?

You can set a different logging level for each logging handler but it seems you will have to set the logger's level to the "lowest". In the example below I set the logger to DEBUG, the stream handler to INFO and the TimedRotatingFileHandler to DEBUG. So the file has DEBUG entries and the stream outputs only INFO.

How do you log all levels in Python?

There are six log levels in Python; each level is associated with an integer that indicates the log severity: NOTSET=0, DEBUG=10, INFO=20, WARN=30, ERROR=40, and CRITICAL=50. All the levels are rather straightforward (DEBUG < INFO < WARN ) except NOTSET, whose particularity will be addressed next.


1 Answers

Appender based configuration

Configuring the log levels per each appender has to be done separately unless it is same as the root level configuration. Below sample log4.properties file is configured to log INFO and above into the console, but only ERROR and above into the file.

log4j.appender.[appender-name].Threshold=[Level]

Look at the last line of the below example (from "How to integrate log4j with your Java project").

# root level configurations 
log4j.rootLogger=INFO,console,file   

# configuration for console outputs  
log4j.appender.console=org.apache.log4j.ConsoleAppender  
log4j.appender.console.layout=org.apache.log4j.PatternLayout  

# configuration for file output (into a file named messages.log)  
log4j.appender.file=org.apache.log4j.RollingFileAppender  
log4j.appender.file.File=messages.log  
log4j.appender.file.layout=org.apache.log4j.PatternLayout 

# threshold for file output 
log4j.appender.file.Threshold=ERROR

Package based log levels

Any of the followings will help.

log4j.logger.[package]=[Level]
log4j.logger.[package]=[Level], [Appender]

As an example:

log4j.logger.org.apache.cxf=INFO, console
like image 155
lkamal Avatar answered Sep 20 '22 07:09

lkamal