Create a log file with date using log4j

I am writing my log file using the code below, but it stores the file as QueryLog.log . Am I missing something? Check out my log4j.properties file code

 log4j.logger.org.hibernate=INFO log4j.logger.org.hibernate.SQL=DEBUG log4j.logger.org.hibernate.type=TRACE log4j.logger.org.hibernate.hql.ast.AST=info log4j.logger.org.hibernate.tool.hbm2ddl=warn log4j.logger.org.hibernate.hql=debug log4j.logger.org.hibernate.cache=info log4j.logger.org.hibernate.jdbc=debug log4j.rootLogger = DEBUG, FILE log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender log4j.appender.FILE.DatePattern='.'yyyy-MM-dd-a log4j.appender.FILE.File=log4j/QueryLog.log log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.conversionPattern= %d{HH:mm:ss} %-5p %c - %m%n 

The links I used are:

http://www.tutorialspoint.com/log4j/log4j_logging_files.htm

http://www.codejava.net/coding/configure-log4j-for-creating-daily-rolling-log-files

+6
source share
3 answers

As stated in fooobar.com/questions/953565 / ... , RollingFileAppender goal is to automatically create a new log file at a specified interval. In the case of the DailyRollingFileAppender this interval is 12:00 AM every day.

This means that the first file created by log4j will have the file name specified here:

 log4j.appender.FILE.File=log4j/QueryLog.log 

And, from now on, a new log file will be created every day with the date added to it.

To always indicate a file with an added date, you can use DatedFileAppender by Jeff Mottram

+6
source

This line sets the log file name in your log4j properties that you have: log4j.appender.FILE.File = log4j / QueryLog.log

You can see the answer here. Setting the log file name to include the current date in Log4j

+1
source

A solution for directly entering a file with the current active date / time, for example XYZ.log.20150101.log, instead of XYZ.log can be done by simply removing the ActiveFileName property when using the rolling package org.apache.log4j. roll.RollingFileAppender in apache-log4j-extras 1.1 with log4j 1.2.x.

 <appender name="defaultFileAppender" class="org.apache.log4j.rolling.RollingFileAppender"> <param name="append" value="true" /> <param name="Threshold" value="INFO" /> <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy"> <param name="FileNamePattern" value="${catalina.base}/logs/application/custom-application-logger.%d{yyyy-MM-dd_HH_mm}" /> </rollingPolicy> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %-10t [%-40.40c] %x - %m%n" /> </layout> </appender> 
0
source

Source: https://habr.com/ru/post/953564/


All Articles