Log4j Maxfilesize - java

Problem i have with log4j is that when i specify maxfilesize it works properly for the files generated in the src folder of project, but the log files generated in tomcat bin, are not splited into seperate files and then i have a large log file which is not desirable.
The following is log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="fatalFile" class="org.apache.log4j.RollingFileAppender">
<param name="MaxFileSize" value="100KB" />
<param name="MaxBackupIndex" value="5" />
<param name="File" value="logs/pnusn/fatal.log" />
<param name="threshold" value="fatal" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p in Class:%C{2}, Thread:%t, at:%d{ABSOLUTE}:: '%m'%n" />
</layout>
</appender>
<appender name="othersFile" class="org.apache.log4j.RollingFileAppender">
<param name="MaxFileSize" value="100KB" />
<param name="MaxBackupIndex" value="5" />
<param name="File" value="logs/pnusn/others.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p in Class:%C{2}, Thread:%t, at:%d{ABSOLUTE}:: '%m'%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="debug" />
<param name="LevelMax" value="error" />
</filter>
</appender>
<appender name="traceFile" class="org.apache.log4j.RollingFileAppender">
<param name="MaxFileSize" value="200KB" />
<param name="MaxBackupIndex" value="20" />
<param name="File" value="logs/pnusn/trace.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p in Class:%C{2}, Thread:%t, at:%d{ABSOLUTE}:: '%m'%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="trace" />
<param name="LevelMax" value="trace" />
</filter>
</appender>
<root>
<priority value="trace"></priority>
<appender-ref ref="fatalFile" />
<appender-ref ref="othersFile" />
<appender-ref ref="traceFile" />
</root>
</log4j:configuration>
Does anybody know how can i fix it?
Thanks for your consideration.

The log4j log files are appearing in your tomcat's bin directory because you haven't told it to do otherwise.
The tomcat docs here describe how to integrate log4j, including the use of the ${catalina.out} substitution to refer to the tomcat root directory. The instructions refer to the log4j properties file format, too, it's not clear if it'll work for log4j.xml
As for the file size problem, I'm not sure why that's happening. Try using the DailyRollingFileAppender instead, and see if that works.

Related

Disable level log log4j

I have errorAppender and infoAppender to separate in 2 log files.
That is working, but I need to log in info file only info information.
Example:
when I call logger.error("TEST ERROR"), I don't want this showing on info file.
is there any way to disable level logs?
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="errorAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/log/error.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d %c %M - %m%n" />
</layout>
<param name="Threshold" value="ERROR" />
</appender>
<appender name="infoAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/log/success.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d %c %M - %m%n" />
</layout>
<param name="Threshold" value="INFO" />
</appender>
<root>
<appender-ref ref="errorAppender" />
<appender-ref ref="infoAppender" />
</root>
</log4j:configuration>
thanks to #Fildor, it is working now using LevelRangeFilter
<appender name="infoAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/log/success.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d %c %M - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
<param name="AcceptOnMatch" value="true" />
</filter>
<param name="Threshold" value="INFO" />
</appender>

Log4j: Logger stops working after load third party jar

Logger suddenly stops working after call a method of third party jar. I have checked code of jar, they are also using log4j with log4j.xml. I am configuring log4j by using the properties file.
My log4j configuration
log4j.rootLogger=DEBUG, stdout, R
#log4j.rootLogger=ERROR, stdout, R
log4j.appender.R.File=/logs/app_test.log
log4j.appender.R.MaxFileSize=10MB
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.MaxBackupIndex=175
log4j.appender.R.layout.ConversionPattern=%d %p %t %c - %m%n
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.R=org.apache.log4j.RollingFileAppender
Third party logs configuration (log4j.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!--Pattern used: Date Thread_Name Full_Class_Name.Method_Name Level:Line
no -->
<!-- %-23d{ISO8601} [THREAD:%t] [%-2c.%M():%L] [%p] - %m%n -->
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-23d{ISO8601} [%-5p] [%-22t] [%-30C{1}:%4L] - %m%n" />
</layout>
</appender>
<appender name="errorAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File"
value="${catalina.base}/logs/metascan/metsascanWrapperError.log" />
<param name="maxFileSize" value="10MB" />
<param name="Threshold" value="ERROR" />
<param name="maxBackupIndex" value="2" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy MM dd HH:mm:ss:SSS} [%-40t] %5p - %m %n" />
</layout>
</appender>
<appender name="infoAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File"
value="${catalina.base}/logs/metascan/metsascanWrapperInfo.log" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="2" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-23d{ISO8601} [%-5p] [%-22t] [%-30C{1}:%4L] - %m%n" />
</layout>
</appender>
<appender name="debugAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File"
value="${catalina.base}/logs/metascan/metsascanWrapperDebug.log" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="2" />
<param name="Threshold" value="DEBUG" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-23d{ISO8601} [%-5p] [%-22t] [%-30C{1}:%4L] - %m%n" />
</layout>
</appender>
<category name="com.xerox.metascan" additivity="true">
<priority value="ERROR" />
<appender-ref ref="infoAppender" />
<appender-ref ref="debugAppender" />
<appender-ref ref="consoleAppender" />
</category>
<!-- <category name="org.springframework"> <priority value="error" /> <appender-ref
ref="infoAppender90" /> <appender-ref ref="errorAppender90" /> </category> -->
<root>
<priority value="error" />
<appender-ref ref="errorAppender" />
<appender-ref ref="consoleAppender" />
</root>
</log4j:configuration>
I have read the configuration on log4j website, preference will always go to log4j.xml over properties file. So, I don't want to change in the third party jar. I am assuming that they are overriding my log4j configuration but I don't know the exact issue because even system.out.println doesn't work.
So how we I resolve the issue, is there any way I can ignore the third party logs configuration?
Any suggestions will be helpful, Thanks.
No, You cannot ignore the third party logs, You can override them by loading externally log4j configuration.
PropertyConfigurator.configure("log4j.properties");
but ideally, this is not a good practice. You should tell them who provided you the jar and they should fix that because any third party jar should not affect your configuration.

BufferSize location in log4j xml configuration

I have the following in my log4j.xml configuration:
<appender name="FileAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${MDCapture.logfile.dir}/${APP_NAME}.log"/>
<param name="BufferedIO" value="false"/>
<param name="DatePattern" value="'.'yyyy-MMM-dd"/>
<layout class="org.apache.log4j.TTCCLayout">
<param name="DateFormat" value="ISO8601"/>
</layout>
</appender>
<appender name="AsyncAppenders" class="org.apache.log4j.AsyncAppender">
<appender-ref ref="FileAppender"/>
<param name="BufferSize" value="1000" />
</appender>
When I run it, I get the following warning:
08-Apr-2013 14:22:15
log4j:WARN Continuable parsing error 18 and column 13
log4j:WARN The content of element type "appender" must match "(errorHandler?,param*,rollingPolicy?,triggeringPolicy?,connectionSource?,layout?,filter*,appender-ref*)".
Do I need to move as follows:
<appender name="FileAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${MDCapture.logfile.dir}/${APP_NAME}.log"/>
<param name="BufferedIO" value="false"/>
<param name="DatePattern" value="'.'yyyy-MMM-dd"/>
<!-- HERE -->
<param name="BufferSize" value="1000" />
<!-- END HERE -->
<layout class="org.apache.log4j.TTCCLayout">
<param name="DateFormat" value="ISO8601"/>
</layout>
</appender>
Well, I only see 13 lines of code, but
<appender name="AsyncAppenders" class="org.apache.log4j.AsyncAppender">
<appender-ref ref="FileAppender"/>
<param name="BufferSize" value="1000" />
</appender>
has <appender-ref> and <param> tags in the wrong order.
WARN The content of element type "appender" must match "(errorHandler?,param*,rollingPolicy?,triggeringPolicy?,connectionSource?,layout?,filter*,appender-ref*)".
to use BufferSize, you must have log4j v1.2.17 at least. Please check the jar version that you are using.

Spring WebFlow Project. I can't stop log4j from printing debug statements

I am working on a Spring WebFlow Project. I can't stop log4j from printing debug statements. can someone please tell me how to get it to stop putting debug statments on the console and log.
I see this warn as my server starts:
log4j:WARN Continuable parsing error 42 and column 11
log4j:WARN The content of element type "logger" must match "(level?,appender-ref*)".
Below is my log4j.xml file:
<?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">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c - %m%n" />
</layout>
</appender>
<appender name="logfile" class="org.apache.log4j.FileAppender">
<param name="File" value="logs/disater_relief.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c - %m%n" />
</layout>
</appender>
<appender name="rollinglogfile" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="logs/school_visit.log" />
<param name="immediateFlush" value="true" />
<param name="append" value="true" />
<param name="maxFileSize" value="1MB" />
<param name="maxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c - %m%n" />
</layout>
</appender>
<appender name="dailyrollinglogfile" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="./logs/school_visit.log" />
<param name="immediateFlush" value="true" />
<param name="append" value="true" />
<param name="datePattern" value=" '.' yyyy-MM-dd " />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c - %m%n" />
</layout>
</appender>
<appender name="email" class="org.apache.log4j.net.SMTPAppender">
<param name="BufferSize" value="512" />
<param name="SMTPHost" value="xxx" />
<param name="From" value="xx" />
<param name="To" value="xx" />
<param name="Subject" value="School Visit Web - SYSTEM ERROR" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<root>
<priority value="WARN" />
<appender-ref ref="email" />
<appender-ref ref="console" />
<appender-ref ref="dailyrollinglogfile" />
</root>
</log4j:configuration>
This is just a log4j issue. It appears that <priority> is not a valid sub-element of the logger element <root>. Rather, you should have <level> instead.

Email log4j errors to different groups

Am using log4j for my error reporting. Ive set my log 4j to also append errors to emails. However i need it to send emails to separate groups depending on the level of the message. For example
info messages go to management- john#management.com, peter#management.com, simon#management.com
debug messages go to programmers - paul#programmer.com, matt#programmer.com
warn messages go to administrator - admin#admin.com
In such a fashion, is it possible? And has anyone implemented said procedure (a sample properties file would be appreciated)
Is should be easy to configure something like this using the SMTPAppender from log4j. But be aware, that emailing logs takes quite a bit of load, especially if you consider mailing DEBUG reports to developers.
You can use a log4j Filter to do this (http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/spi/Filter.html)
The example below writes messages to different files according to the log level.
This can be easily adapted to an SMTPAppender instead of a FileAppender.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="management" class="org.apache.log4j.FileAppender">
<param name="File" value="management.log" />
<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" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="programmers" class="org.apache.log4j.FileAppender">
<param name="File" value="programmers.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="admin" class="org.apache.log4j.FileAppender">
<param name="File" value="admin.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<category name="a">
<level value="ALL" />
<appender-ref ref="management"/>
<appender-ref ref="programmers"/>
<appender-ref ref="admin"/>
</category>
</log4j:configuration>

Categories

Resources