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.
Related
Okay, this is driving me nuts. I have an app that uses log4j 1.2.16, my configuration file is properly picked up and log4j writes to the file that I want, BUT it only logs error level.
Code:
import org.apache.log4j.Logger;
- (... code omitted)
private static final Logger LOGGER = Logger.getLogger(PostLoginController.class);
LOGGER.debug("TOTO debug");
LOGGER.info("TOTO Info");
LOGGER.warn("TOTO warn");
LOGGER.error("TOTO error");
XML:
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/jboss/jboss-eap-6.4/standalone/log/myLogFile.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p [%t:%c] : %m%n"/>
</layout>
</appender>
<!-- Root Logger -->
<root>
<priority value ="debug" />
<appender-ref ref="fileAppender"/>
</root>
When my app runs myLogFile is created and I can see the error log in it, but not the other ones:
2019-06-06 19:37:31 ERROR [http-/0.0.0.0:8080-2:my.classpath.PostLoginController] : TOTO error
Any idea what's wrong here?
You have to use like this for logging debug to error.
<log4j:configuration debug="false">
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="target" value="System.out" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.FileAppender">
<param name="file" value="/jboss/jboss-eap-6.4/standalone/log/myLogFile.log"/>
<param name="append" value="false" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<root>
<priority value="info" />
<appender-ref ref="default.console" />
<appender-ref ref="default.file" />
</root>
</log4j:configuration>
Okay, figured it out. I had to add
<category name="my.classpath">
<priority value="all" />
<appender-ref ref="fileAppender" />
</category>
because somehow the rootLogger level can't be altered.
When I'm tapping on the log button corresponding to my application of spring boot admin dashboard I'm getting Error while reading logfile: - error. Does somebody faced this already?
I'm using log4j with such configuration:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ABSOLUTE}] [%-5p] [%c{1}] [%t] [%m]%n"/>
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="10KB" />
<param name="maxBackupIndex" value="5" />
<param name="file" value="service-rest.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ABSOLUTE}] [%-5p] [%c{1}] [%t] [%m]%n"/>
</layout>
</appender>
<root>
<priority value="DEBUG"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="file" />
</root>
</log4j:configuration>
My application.yml file has such logging section:
logging:
file: "service-rest.log"
level:
org:
springframework:
boot:
autoconfigure:
logging: DEBUG
Also I could get that logfile from app directly http://url/actuator/logfile
The problem was the slightly wrong format of logs that has been pushed to service-rest.log file. The right one appender for file is
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="5" />
<param name="file" value="tvpp-rest.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] [%t] [%m]%n"/>
</layout>
</appender>
Without that brackets around time and log level.
I have below log4j configuration. when file reaches its max size , it is not rolling but when I restart server then rolling is working.
Please let me know what I am wrong in my configuration.
<appender name="ALog" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true" />
<param name="maxFileSize" value="10KB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="logs/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>
<root>
<priority value ="debug"/>
<appender-ref ref="ALog"/>
</root>
Thanks.
You have to do :
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<appender-ref ref="file"/>
In log4j there are level from trace at the lowest, debug, info, warn, error, and fatal.
I used level info to log my web application with this code
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="abclog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/data/abc/abc.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<logger name="com.myapp">
<level value="info" />
<appender-ref ref="abclog"/>
</logger>
<logger name="com.myapp.controller">
<level value="info" />
<appender-ref ref="abclog"/>
</logger>
</log4j:configuration>
It'd produce abc.log contained log from level info to fatal.
Then I tried to move the error log only to another file, error.log.
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="errorlog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/data/abc/error.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<appender name="abclog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/data/abc/abc.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<logger name="com.myapp">
<level value="error" />
<appender-ref ref="errorlog"/>
</logger>
<logger name="com.myapp.controller">
<level value="error" />
<appender-ref ref="errorlog"/>
</logger>
<logger name="com.myapp">
<level value="info" />
<appender-ref ref="abclog"/>
</logger>
<logger name="com.myapp.controller">
<level value="info" />
<appender-ref ref="abclog"/>
</logger>
</log4j:configuration>
But didn't work, all logger still in abc.log. I found that the logger outputs all those messages equal to that level and also all greater levels than it, that's why the log still in abc.log.
When I deleted abclog and log level info, it worked. It'd show logger from level error (and maybe fatal error if any) on error.log.
But why when I tried to lower the log info to trace / debug without level info or error, it didn't come up with anything, no logger created.
When I put the lowest log4j level, it should show all the log from its level and above like it supposed to do at info level (show log from info to fatal).
Why could it show up with nothing when the level change into trace / debug instead of level info?
Does it really no other way to separate error log to another file from other log?
Try to use the category with additivity set to false. I have not tested, but something like the following should work:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="errorlog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="ERROR"/>
<param name="File" value="/data/abc/error.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<appender name="abclog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/data/abc/abc.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<appender name="console" 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{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<!-- all error from com.myapp to errorlog only -->
<category name="com.myapp" additivity="false">
<priority value="error" />
<appender-ref ref="errorlog"/>
</category>
<!-- all other log from com.myapp to abclog only -->
<category name="com.myapp" additivity="false">
<priority value="info" />
<appender-ref ref="abclog"/>
</category>
<root>
<!-- all other log to console -->
<appender-ref ref="console"/>
</root>
</log4j:configuration>
If you want to separate com.myapp and com.myapp.controller, you can declare more categories. But you should declare com.myapp.controller before com.myapp.
UPDATE : you can try with filter perhaps is what you need, after that, I have no more idea:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="errorlog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="ERROR"/>
<param name="File" value="/data/abc/error.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="ERROR" />
<param name="levelMax" value="FATAL" />
</filter>
</appender>
<appender name="abclog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="INFO"/>
<param name="File" value="/data/abc/abc.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="INFO" />
<param name="levelMax" value="WARN" />
</filter>
</appender>
<appender name="console" 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{ISO8601}] [%-5p] [%t] {%F:%M:%L} - %m%n"/>
</layout>
</appender>
<!-- all log from com.myapp to special log file -->
<category name="com.myapp" additivity="false">
<appender-ref ref="errorlog"/>
<appender-ref ref="abclog"/>
</category>
<root>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
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>