Different log4j layout for debug and error? - java

In log4j, is there a way to have logger.error("") and logger.debug("") include different output layouts?
I'd like errors to include method names and line numbers, both of which slow down application performance.
EDIT:
After adding apache-log4j-extras, the following config file works.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="WARNINGS" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="warnings.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MMM-dd HH:mm:ss} %-5p %c - %m%n%n"/>
</layout>
<filter class="org.apache.log4j.filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG"/>
<param name="LevelMax" value="WARN"/>
</filter>
</appender>
<appender name="ERRORS" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="errors.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- %M for method name , %L for line number -->
<param name="ConversionPattern" value="%d{yyyy-MMM-dd HH:mm:ss} %-5p %c %M %L - %m%n%n"/>
</layout>
<filter class="org.apache.log4j.filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR"/>
<param name="LevelMax" value="FATAL"/>
</filter>
</appender>
<logger name="com.foo.bar">
<appender-ref ref="WARNINGS"/>
<appender-ref ref="ERRORS"/>
</logger>
</log4j:configuration>
This code was used to test
// goes to errors.txt
log.error("error");
//goes to warnings.txt
log.warn("warn");

Within your log4j.xml configuration, you can create two appenders, each using a LevelMatchFilter and each with a separate pattern output.

I would just setup two different loggers. One called ERROR and one called DEBUG. Then you can easily setup different output layouts and only call ERROR.error or DEBUG.debug.

Related

Need to Set Minimum and Maximum Level of Logging in Log4j

My requirement is little weird.
I want different formatting pattern for different Logging levels that's why I have used this code. But I am unable to set the min and max limit. Because when I am setting logging level to Debug, it is also printing the ERROR and FATAL log messages in that format but I have defined a different format for ERROR and FATAL and because of this ERROR messages are printing two times for this code. I do not want to print ERROR and FATAL log messages in DEBUG pattern format.
log4j.rootLogger=DEBUG, infoLog, errorLog
log4j.appender.infoLog=org.apache.log4j.ConsoleAppender
log4j.appender.infoLog.Target=System.out
log4j.appender.infoLog.layout=org.apache.log4j.PatternLayout
log4j.appender.infoLog.layout.ConversionPattern=%m%n
#log4j.appender.infoLog.Threshold=DEBUG
log4j.appender.infoLog.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.infoLog.filter.a.LevelToMatch=INFO
log4j.appender.infoLog.filter.a.AcceptOnMatch=true
log4j.appender.infoLog.filter.a.LevelMin=DEBUG
log4j.appender.infoLog.filter.a.LevelMax=INFO
#log4j.appender.infoLog.filter.b=org.apache.log4j.varia.LevelMatchFilter
#log4j.appender.infoLog.filter.b.LevelToMatch=DEBUG
#log4j.appender.infoLog.filter.b.AcceptOnMatch=true
log4j.appender.errorLog.Threshold=ERROR
log4j.appender.errorLog=org.apache.log4j.ConsoleAppender
log4j.appender.errorLog.Target=System.out
log4j.appender.errorLog.layout=org.apache.log4j.PatternLayout
log4j.appender.errorLog.layout.ConversionPattern=\u001b[31;1m%d{yyyy-MM-dd HH:mm:ss} [%-5p] %c{1}:%M():%L - %m%n
This xml Configuration will resolve this problem.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="debugLog" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
<appender name="errorLog" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="WARN"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%-5p] %c{1}:%M():%L - %m%n"/>
</layout>
</appender>
<root>
<level value="DEBUG"></level>
<appender-ref ref="debugLog"/>
<appender-ref ref="errorLog"/>
</root>
</log4j:configuration>

Not getting expected output from log4j when the logs are printed in file

Problem:
We got 2 logs printed in the file like this:
Analysis
1)These 2 logs are printed one after the other in the code and here too but the time difference is 7 minutes which is huge.
2)This has been observed when there is only one War file as well as multiple wars in the Tomcat.
Below is the log4j configuration file to analyse the issue clearly:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c - %m%n" />
</layout>
</appender>
<appender name="callback" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="file" value="${catalina.base}/logs/subs_engine.log" />
<param name="append" value="true" />
<param name="encoding" value="UTF-8" />
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern"
value="${catalina.base}/logs/%d{yyyy-MM-dd_HH}_subs_engine.gz" />
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c:%L - %m%n" />
</layout>
</appender>
<root>
<priority value="info" />
<appender-ref ref="callback" />
</root>
</log4j:configuration>
Can anyone guide me how to solve or debug this problem:
Is this the problem of log4j?
We are using log4j-1.2.17.jar
#Edit:
Logger.sysLog(LogValues.APP_DEBUG, Utility.class.getName(),"Inside loadHttpReqToBean.");
Logger class Calling syslog method

Log tomcat logs to rsyslog

I have a java web application that runs with tomcat7 and logs with log4j. I want to send my logs to rsyslog.
I have use the appeneder in my log4j.xml file.
<appender name="syslog" class="org.apache.log4j.net.SyslogAppender">
<param name="Facility" value="USER"/>
<param name="SyslogHost" value="localhost"/>
<param name="Threshold" value="WARN"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} MYAPP: %-5p %m%n"/>
</layout>
</appender>
But I still don't see the logs that I've implemented in my java application in rsyslog. I can see them in catalina.out but not on rsyslog.
Do I have to define new rules for rsyslog configuration?
Thanks in advance.
Updated 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="System.out" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="Target" value="System.out" />
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="SYSLOG" class="org.apache.log4j.net.SyslogAppender">
<param name="SyslogHost" value="localhost"/>
<param name="Facility" value="LOCAL1"/>
<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>
<!-- for package specific -->
<!-- <category name ="my.package" /> -->
<logger name ="Logger">
<level value ="debug" />
</logger>
<!-- root logger -->
<root>
<level value="info"/>
<appender-ref ref="SYSLOG"/>
</root>

Is "xmlns:log4j" a required attribute of the <log4j:configuration> tag?

I see a bunch of sample log4j configurations that contain xmlns:log4j="http://jakarta.apache.org/log4j/" in the <log4j:configuration> tag, is this attribute required? What does having this attribute in my configuration do for me?
Example:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="infoLogsFile" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="MyApplication.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<root>
<priority value ="DEBUG" />
<appender-ref ref="infoLogsFile"/>
</root>
</log4j:configuration>
Could I do this instead?
<log4j:configuration>
<appender name="infoLogsFile" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="MyApplication.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<root>
<priority value ="DEBUG" />
<appender-ref ref="infoLogsFile"/>
</root>
</log4j:configuration>
The xmlns attribute specifies that elements prepended with log4j are defined by the schema referenced by that URL. Log4J does not actually perform schema validation before attempting to parse the configuration file, so it is not actually required.

log different log levels to different files with log4j

I am using log4j for logging in my applicaion. I am trying to different level logs in different files but something going wrong. Any help regrading this issue will be highly appreciated.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="TCS_patch_9/log/retailer_debug.log" />
<param name="Threshold" value="DEBUG" />
<param name="MaxFileSize" value="2MB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p %c - %m%n"/>
</layout>
</appender>
<appender name="INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="TCS_patch_9/log/retailer_info.log" />
<param name="Threshold" value="INFO" />
<param name="MaxFileSize" value="2MB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p %c - %m%n"/>
</layout>
</appender>
<appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="TCS_patch_9/log/retailer_error.log" />
<param name="Threshold" value="ERROR" />
<param name="MaxFileSize" value="2MB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p %c - %m%n"/>
</layout>
</appender>
<appender name="FATAL" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="TCS_patch_9/log/retailer_repeat_delay.log" />
<param name="Threshold" value="FATAL" />
<param name="MaxFileSize" value="2MB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p %c - %m%n"/>
</layout>
</appender>
<root>
<priority value ="error" />
<appender-ref ref="console"/>
<appender-ref ref="DEBUG"/>
<appender-ref ref="INFO"/>
<appender-ref ref="ERROR"/>
<appender-ref ref="FATAL"/>
</root>
</log4j:configuration>
The standard behaviour for log4j appenders is that they log all messages at their threshold level or higher, i.e. an appender with threshold INFO will log INFO, WARN, ERROR and FATAL messages but not DEBUG. If you want to log only INFO messages but not WARN and above then you need to use a LevelMatchFilter.
You will also need to set your root logger priority to DEBUG, otherwise it will only send ERROR and FATAL messages to its appenders and your DEBUG and INFO files will be empty.

Categories