implement log4j to generate new logs daily with fixed size - java

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"/>

Related

Spring Boot Admin: Error while reading logfile: -

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.

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

XML configuration for log4j in order to show line No. in HTMLLayout

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

unable to set MaxFileSize to value "100MB" in log4j

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.

how to Create a folder for log file using log4j.xml

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"?>

Categories