I am using log4j1.2.17 and apache-extras-log4j--1.2.17.jar for logging.
My purpose is to roll and archive the file based on size.Below is the 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="R" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="File" value="logger.log"/>
<param name="append" value="true"/>
<param name="encoding" value="UTF-8"/>
<rollingPolicy class="org.apache.log4j.rolling.FixedWindowRollingPolicy">
<param name="FileNamePattern" value="logger.log%i.gz" />
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="${LOG_FILESIZE_LIMIT}"/>
</triggeringPolicy>
<layout class="org.apache.log4j.TTCCLayout">
<param name="ContextPrinting" value="true"/>
<param name="ThreadPrinting" value="true"/>
<param name="DateFormat" value="MM/dd/yyyy HH:mm:ss"/>
</layout>
</appender>
<root>
<priority value ="DEBUG"/>
<appender-ref ref="R"/>
</root>
</log4j:configuration>
${LOG_FILESIZE_LIMIT} is 100MB.
But its not setting the MaxFileSize to 100MB and gives below message.
log4j:WARN Failed to set property [maxFileSize] to value "100MB"
Its taking default maxfilesize which is 10MB and archiving the file once it reaches to 10MB.
Thanks,
Anjali
Try setting the ${LOG_FILESIZE_LIMIT} placeholder to the value 104857600 (the equivalent of 100MB in bytes), as the MaxFileSize param accepts a long value.
Check that there are no other versions of log4j on the classpath, I had this problem before where another third party set of jars also had an older version of log4j - removing it fixed the problem.
Related
Im trying to create log4j appender which:
-Creates new log file every day
-Creates new log file every application restart
-Creates new log file every 500mb
-Saves old log files to zip
I tried different configurations, below is last one. But this one also does not work. Is it even possible to do what i need?
<appender name="default" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="log.log" />
<param name ="DatePattern" value="'.'yyyyMMdd" />
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="activeFileName" value="log.log" />
<param name="FileNamePattern" value="log_%d{yyyyMMdd}.log.%i.zip"/>
<param name="minIndex" value="1"/>
<param name="maxIndex" value="10"/>
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="500 MB" />
</triggeringPolicy>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<OnStartupTriggeringPolicy/>
<layout class="mycustomlayout">
</layout>
</appender>
I have a question on the log4j XML configuration. I want to have a HTMLLayout. But in the generated HTML file, there is no Line No. category. But I want to see it. I searched and it seems like to set LocationInfo to be true. But I don't know how to modify my XML.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="log" class="org.apache.log4j.FileAppender">
<param name="File" value= "log4j.html"/>
<param name="Append" value= "false" />
<layout class="org.apache.log4j.HTMLLayout"/>
</appender>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%06r] [%F:%L] %-10c %x %m%n" />
</layout>
</appender>
<root>
<priority value="debug"/>
<appender-ref ref="log"/>
<!--appender-ref ref="stdout"/-->
</root>
</log4j:configuration>
In the javadoc 1 of org.apache.log4j.HTMLLayout states:
The LocationInfo option takes a boolean value. By default, it is set to false which means there will be no location information output by this layout. If the the option is set to true, then the file name and line number of the statement at the origin of the log statement will be output.
So, you just need to set it to true. Therefore, the configuration of your appender could be similar to:
<appender name="log" class="org.apache.log4j.FileAppender">
<param name="file" value="log4j.html"/>
<param name="threshold" value="debug"/>
<param name="immediateFlush" value="true"/>
<param name="append" value="false"/>
<layout class="org.apache.log4j.HTMLLayout">
<param name="Title" value="false"/>
<param name="LocationInfo" value="true"/>
</layout>
</appender>
Notes
https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/HTMLLayout.html
I am using log4j for application logging. And my application is deployed in weblogic server. I use RollingFileAppender for rotating logs on daily basis. But,the log file overrides and create app.log,app.log.1 for every 2MB and date based log rotation is not working. Any Idea how to resolve the issue. Please find my log4j.xml below
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="MESSAGEDEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="DEBUG"/>
<param name="File" value="/u02/appdata/logs/app.log"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%5p] %c{2} %m%n" />
</layout>
</appender>
<logger name="MessageLogger">
<level value="debug"/>
<appender-ref ref="MESSAGEDEBUG"/>
</logger>
</log4j:configuration>
Go into yours log4j.properties and set :
log4j.appender.file.MaxFileSize=10MB // File max size
log4j.appender.file.MaxBackupIndex=5 // Max number of files.
About namming your log files, try check this thread :
log4j log file names?
And 1 more link that u can find useful :
https://logging.apache.org/lo/org/ag4j/1.2/apidocspache/log4j/RollingFileAppender
Edit #2
I guess now u want to add :
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<!-- The file to roll to, this is a fairly intelligent parameter, if the file
ends in .gz, it gzips it, based on the date stamp it rolls at that time,
default is yyyy-MM-dd, (rolls at midnight)
See: http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html -->
<param name="FileNamePattern" value="/applogs/myportal/portal.%d.log.gz" />
</rollingPolicy>
Log4j wiki
DailyRollingFileAppender can be used to generate new log files daily.RollingFileAppender has the implementation to create new log files when the size of the file exceeds a given value.However I need to implement both of these together using log4j.
This is the XML configuration file I've used to generate daily logs.How can I modify it to include the maxsize also??
<?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="fileAppender1" class="org.apache.log4j.DailyRollingFileAppender">
<param name="append" value="true"/>
<param name="file" value="D:/calculator/logs/log"/>
<param name="DatePattern" value="'_'yyyy-MM-dd" />
<param name="immediateFlush" value="true"/>
<param name="threshold" value="info"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n%n"/>
</layout>
</appender>
<root>
<priority value="info"></priority>
<appender-ref ref="fileAppender1" />
</root>
</log4j:configuration>
If you want to roll a file after reaches 100KB (e.g.), try to add:
<param name="MaxFileSize" value="100KB"/>
I just created log4j.xml file like ,
<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ALL" />
<param name="MaxFileSize" value="512KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="F:/Core_logs/application_log.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n"/>
</layout>
</appender>
<!--sets the priority log level for org.springframework -->
<logger name="org.springframework">
<level value="info" />
</logger>
<!--sets the default priority log level -->
<root>
<priority value="all"></priority>
<appender-ref ref="fileAppender" />
</root>
</log4j:configuration>
But I have the exception as ,
java.io.FileNotFoundException: F:\Spring_Core_logs\pointel_Aop.log (The system cannot find the path specified)
If I created a folder Core_logs manually in the particular location means, it works fine and log file created.
How to create the folder , if the folder is not exist in a particular location?
EDIT:
This here could also help you/looks like the best solution for you:
Configuring Java FileHandler Logging to create directories if they do not exist
It seems like log4j version 1.2.15 does it. Look for an answer below from Arun P Johny, he posted a piece of code from the log4j sourcecode. I overlooked it because it was not accepted as an answer.
Log4j.xml file for creating in your eclipse
<?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="fileAppender" class="org.apache.log4j.FileAppender">
<param name="Threshold" value="INFO" />
<param name="Append" value="true" />
<param name="File" value="logfile.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" />
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="fileAppender"/>
</root>
</log4j:configuration>"UTF-8"?>