Java log4j Configuration file doesn't work - java

I have a log4j configuration file as below, what I want to do is output the TRACE in the console and output the ERROR message to the file (fileerror). But it doesn't work, both the console and file is at the TRACE level.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="fileerror" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="1024MB" />
<param name="maxBackupIndex" value="2" />
<param name="file" value="ExonImpact.error.log.txt" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="ccbb.hrbeu" additivity="true">
<level value="TRACE"/>
<appender-ref ref="console" />
</logger>
<root>
<level value="ERROR"/>
<appender-ref ref="fileerror" />
</root>
</log4j:configuration>
Below is my log4j debug information.
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [ccbb.hrbeu] additivity to [true].
log4j: Level value for ccbb.hrbeu is [TRACE].
log4j: ccbb.hrbeu level set to TRACE
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: Adding appender named [console] to category [ccbb.hrbeu].
log4j: Level value for root is [ERROR].
log4j: root level set to ERROR
log4j: Class name: [org.apache.log4j.RollingFileAppender]
log4j: Setting property [append] to [false].
log4j: Setting property [maxFileSize] to [1024MB].
log4j: Setting property [maxBackupIndex] to [2].
log4j: Setting property [file] to [ExonImpact.error.log.txt].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: setFile called: ExonImpact.error.log.txt, false
log4j: setFile ended
log4j: Adding appender named [fileerror] to category [root].

By default, all messages caught by a child logger are propagated to the root logger. That's why both of your loggers log the same message. You can set your additivity parameter to false (additivity="false") to prevent your logger from passing messages to the root logger. Or you can add an additional "threshold" parameter to your file appender:
<param name="Threshold" value="ERROR" />
then it will ignore all messages below Error level.

Related

Disable log4j and slf4j initialization output for external libraries

When running my application I see on the stdout the following log4j initialization infos:
log4j: Threshold ="null".
log4j: Level value for root is [OFF].
log4j: root level set to OFF
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: Adding appender named [console] to category [root].
This is probably coming from external libraries using log4j and slf4j.
I am trying to suppress these initialization infos, but even setting the root logger level to OFF does not seem to have any effect.
This is the log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="OFF" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
Any suggestion?
Solved setting debug to false in log4j.xml:
<log4j:configuration debug="false"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="OFF" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

How to configure log4j to write DEBUG messages into a file and INFO to console

How can I configure log4j properties file to print DEBUG messages into a file and only INFO messages to console?
This is my properties file:
# Root logger option
log4j.rootLogger=INFO, stdout
log4j.logger.org.hibernate.SQL=TRACE, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{dd-MM-yyyy HH:mm:ss,SS} %t %c{1.}: %m%n
You can use log4j.properties like below:
# Root logger option
log4j.rootLogger=INFO, stdout, file
log4j.logger.org.hibernate.SQL=TRACE, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout..Threshold=INFO
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{dd-MM-yyyy HH:mm:ss,SS} %t %c{1.}: %m%n
# Direct log messages to file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.File=/var/log/logfile.log
log4j.appender.file.Append=true
log4j.appender.file.Encoding=UTF-8
log4j.appender.file.DatePattern='-'yyyy-MM-dd'.log'
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d{ISO8601}] %-5p %x%m%n
By this properties file, log4j create new logfile for each day and save previews logfile with date.
Alternative way with log4j xml configuration. Because I prefer to use xml for log4j config. Perhaps this may help you from some way.
eg.
<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false">
<appender name="CONSOLE_LOG" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] %m%n" />
</layout>
</appender>
<appender name="FILE_LOG" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${catalina.home}/logs/debug.log" />
<param name="Threshold" value="DEBUG" />
<param name="maxBackupIndex" value="5" />
<param name="maxFileSize" value="1500MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS} |%5p| [%c] | %m%n" />
</layout>
</appender>
<root>
<appender-ref ref="CONSOLE_LOG" />
<appender-ref ref="FILE_LOG" />
</root>
</log4j:configuration>
So, your Java code be like:
logger.debug("your debug log message");
logger.info("your info log message");
To configure with log4j.properties, possible duplicate at How can I create 2 separate log files with one log4j config file?
Hope this help!

Writing Only INFO logs to console and ERROR logs to file

I have a requirement to write the INFO (not error and lower levels of INFO) logs to console and ERROR logs to file. Need to use log4j for this.
You need to set a Threshold on your appender for console output. Here's an example modified from the log4j distro:
# A1 is set to be a FileAppender sending its output to
# System.out. However, only info messages and above will be printed
# in A1 because A1's threshold is set to Level.INFO.
# The fact that the root level is set to Prority.DEBUG only influences
# log requests made to the root logger. It has no influence on the
# *appenders* attached to root.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Threshold=INFO
This way, anything printed to your ConsoleAppender A1 will be ignored if it's not at least INFO level. If you set the logging level to INFO then ONLY the INFO messages will be logged.
There's an FAQ question about this on the apache log4j site.
Try this.
log.file.path=C:/log
datestamp=yyyy-MM-dd HH:mm:ss.SSS
# Define the root logger with appender file
log4j.rootLogger=INFO, stdout
log4j.logger.biz.netweb=error, LOG_FILE
#, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p,[%d{${datestamp}}],[Thread:%t],[%C.%M(%L)]==>: %m%n
# LOG_FILE
log4j.appender.LOG_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOG_FILE.File=${log.file.path}/system-log/system-log.txt
log4j.appender.LOG_FILE.ImmediateFlush=true
log4j.appender.LOG_FILE.MaxFileSize=5MB
log4j.appender.LOG_FILE.MaxBackupIndex=50
log4j.appender.LOG_FILE.Append=true
log4j.appender.LOG_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG_FILE.layout.conversionPattern=%5p,[%d{${datestamp}}],[%t,%C.%M(%L)]==>, %m%n
log4j.rootLogger = debug,stdout,D,E
print to console
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
print to file
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/error.log ## log file name
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR ## ERROR log info!!!
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
Following log4j configuration file will logs only "INFO" level logs in to console and between "INFO" and "FATAL" logs in to log.log file.
I you want to change the log level, you can change the MinLevel/MaxLevel parameters of LevelRangeFilter section on both console and Rolling file appender.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!--Redirect log messages to console-->
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
<!--Redirect log messages to a log file, support file rolling.-->
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="INFO"/>
<param name="File" value="log.log"/>
<param name="maxFileSize" value="2Mb"/>
<param name="maxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="FATAL"/>
</filter>
</appender>
<root>
<level value="INFO"></level>
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>

log4j appender priority conflict?

I have this log4j.properties:
# Global logging configuration
log4j.rootLogger=WARN, file
### Direct log messages to a log file
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File= C:\\eclipse\\servers\\apache-tomcat-6.0.39\\logs\\log4j.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
### Console messages Appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
### Appender-to-Class definition
log4j.logger.com.XXX.payplatform.test.*=INFO, console
log4j.additivity.com.XXX.payplatform.test.*=false
So, ok, the WARN level information is being registered in the log4j.log file. That's OK.
But, I want to see INFO-level messages in my eclipse console, but i am not getting any response there.
Any solutions? I think the problem is in the "WARN, file" rootLogger configuration, so i tried this:
# Global logging configuration
log4j.rootLogger=INFO, file
But no changes resulted...
Thank you!
Read more Apache log4j 1.2 - Short introduction to log4j and find the samples as well.
Level Inheritance
The inherited level for a given logger C, is equal to the first non-null level in the logger hierarchy, starting at C and proceeding upwards in the hierarchy towards the root logger.
Basic Selection Rule
A log request of level p in a logger with (either assigned or inherited, whichever is appropriate) level q, is enabled if p >= q.
This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have DEBUG < INFO < WARN < ERROR < FATAL.
It should be like this. Add console here and set default root log level to INFO.
log4j.rootLogger=INFO, file, console
OR
Try with log4j.xml instead of log4j.properties. Read more Configuring Log4j 2
Sample log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="FILE_LOG" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/logs/log4j.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true"/>
<param name="DatePattern" value="'.'dd-MM-yyyy-HH"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<category name="com.XXX.payplatform.test">
<priority value="INFO" />
<appender-ref ref="STDOUT"/>
</category>
<root>
<priority value ="ERROR" />
<appender-ref ref="FILE_LOG" />
</root>
</log4j:configuration>

Possible to use different log4j appenders for different methods?

I have a log4j configuration file and it uses a ConsoleAppender with a specific layout.
I use
logger.info("some text");
in several places in my java code.
Is it possible to create another appender with a different layout in the configuration file and use the specific appender wherever I choose in the code?
Is it possible to create another appender with a different layout in the configuration file and use the specific appender wherever I choose in the code?
Yes you can create different appender with a different layout and create different categories for each appender.
use the category name to get the logger instead of appender name.
Sample: (log4j.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="stdout1" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%.4t] %-5p %c{1} - %m%n" />
</layout>
</appender>
<appender name="stdout2" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%.4t] %-5p %c{1} - %m%n" />
</layout>
</appender>
<category name="category1">
<priority value="INFO" />
<appender-ref ref="stdout1" />
</category>
<category name="category2">
<priority value="DEBUG" />
<appender-ref ref="stdout2" />
</category>
</log4j:configuration>
Java Code: (get the logger based on category name)
org.apache.log4j.Logger.getLogger("category1").warn("debug msg for stdout1 appender");
org.apache.log4j.Logger.getLogger("category2").info("info msg for stdout2 appender");
Output:
[main] WARN category1 - warn msg for stdout1 appender
2014-05-09 02:46:29,216 [main] INFO category2 - info msg for stdout2 appender

Categories

Resources