We are using log4j2 on Window 7 Enterprise.
JBoss Developer Studio 8(this really doesn't matter)
RollingFileAppender rolls over the log files properly, however the original log file keep old logs and continues to increase in size. There is a JIRA bug (LOG4J2-904) related to this . I followed some of the options like
fileIndex="min" or fileIndex="max"
Using RollingRandomAccessFile instead of RollingFile.
Some comments indicated problem fixed as of log4j 2.4.1 version. However, I am still seeing the same problem in log4j 2.5.
I tried logging to both RollingFile and RollingRandomAccessFile appenders at the same time. An image of the log folder in included Here is the image of Log folder from rollover
Here is my log4j2.xml
<Configuration>
<Properties>
<property name="appname">myapp</property>
<Property name="log-path">${server.dir}/myapp</Property>
</Properties>
<Appenders>
<RollingFile name="DATED_ROLLING_FILE" fileName="${log-path}/${myapp}.log" filePattern="${log-path}/${myapp}_%d{MM-dd-yyyy}-%i.log" maxFileSize="40 KB">
<PatternLayout>
<Pattern>%d: %-5p [%c{1}]:%L - %M %m%n</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="40 KB" />
</Policies>
<DefaultRolloverStrategy fileIndex="min" max="100" />
</RollingFile>
<RollingRandomAccessFile name="DATED_RAC_APPENDER" fileName="${log-path}/epermitsrac.log"
filePattern="${log-path}/epermitsrac_%d{MM-dd-yyyy}-%i.log" >
<PatternLayout>
<Pattern>%d: %-5p [%c{1}]:%L - %M %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="40 KB" />
</Policies>
<DefaultRolloverStrategy fileIndex="min" max="100" />
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="DATED_ROLLING_FILE" />
<AppenderRef ref="DATED_RAC_APPENDER" />
</Root>
</Loggers>
</Configuration>
Here is the image of Log folder from rollover. As you can see it does not clear the original File.
Did anyone else experienced same problem and is there a fix for this.
I have tried with below configuration and it is working fine, except that the latest file will be with out date.
the log4j2.properties configuration
rootLogger.level = all
appenders = rolling, console
status = warn
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d [%-5p] (%F:%M:%L) %m%n
# Rolling file based on date for day wise
appender.rolling.type = RollingFile
appender.rolling.name = rolling
#if i use below file name the rollover will not happenning as expected
#appender.rolling.fileName = E:/LOGS/${date:yyyyMMdd}_test.log
#with below filename name the rollover will happen as expected
appender.rolling.fileName = E:/LOGS/test.log
appender.rolling.filePattern = E:/LOGS/%d{yyyyMMdd}_test.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d [%-5p] [%F:%M:%L] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
#use below for day wise roll over
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
rootLogger.appenderRefs = rolling, console
rootLogger.appenderRef.console.ref = console
rootLogger.appenderRef.rolling.ref = rolling
rootLogger.appenderRef.stdout.ref = console, rolling
Related
please help me debug something-- I am getting an empty log file when I use log4j2.
Here is my log4j2.properties file:
# Root logger option
name=PropertiesConfig
property.filename = logs
appenders = stdout, file
# stdout will print logs on console
appender.stdout.name = consoleLogger
appender.stdout.type = Console
appender.stdout.target = System.out
appender.stdout.layout.type = PatternLayout
appender.stdout.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# file will print logs in file
appender.file.name = fileLogger
appender.file.type = File
appender.file.fileName=ProtScannerLog.log
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
loggers=file
logger.file.name = log4j2.properties
logger.file.level = info
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = fileLogger
rootLogger.level = info
rootLogger.appenderRefs = stdout, file
rootLogger.appenderRef.stdout.ref = consoleLogger
rootLogger.appenderRef.file.ref = fileLogger
# added for debugging
# logs the SQL statements
log4j.logger.org.hibernate.SQL=DEBUG
# Logs the JDBC parameters passed to a query
log4j.logger.org.hibernate.type=TRACE
log4j.appender.hb=org.apache.log4j.ConsoleAppender
log4j.appender.hb.layout=org.apache.log4j.PatternLayout
And my log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<File name="File" fileName="ProtScannerLog.log" append="true">
<PatternLayout pattern="%d %t %-5p %c{2} - %m%n"/>
</File>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" level="info" />
<AppenderRef ref="File" level="error" />
</Root>
</Loggers>
</Configuration>
When I run I get the log file in the directory I am running my code from, but it is blank. I see the correct messages in the console. I also would like to have this log file placed in C:\Users\username, but I am having trouble specifying that location. Any help would be much appreciated. Thanks in advance.
It supposed to be comment, but I don't have a reputation so have to post it this way :)
I've just copy pasted your config and it generated the ProtScannerLog.log file with several lines in.
So it definitely not log4j2.properties config issue.
Regarding the location of the file I can suggest to replace:
property.filename = logs
by
property.filename = C:\\Users\\username
And then refer to the property in "appender.file.fileName" definition:
appender.file.fileName=${filename}\\ProtScannerLog.log
It would create the file under any folder you specify.
I have simple question.
How to have log file name dynamically supplied from code?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="${logfilename}.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true"/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
Here file name is logs/app.log.
How to make it dynamic with date and time appended as well in file name? Though pattern is applied but it doesn't work.
Here logs is the directory which log4j library automatically creates, can it be dynamic as well?
EDIT In parameter fileName I have placed ${logfilename}.log and setting system property as below:
System.setProperty("logfilename", "a_cool_logname");
Now it is creating file with name ${logfilename}.log which is definitely not required.
Thanks
**Property File Look like this **
log4j.rootLogger=DEBUG, FA
#Console Appender WE WRE NOT USING IT
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d{dd MM yy HH:mm} %-5p %m%n
#File Appender
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=${fName}
#log4j.appender.FA.MaxFileSize=5MB
#log4j.appender.FA.MaxBackupIndex=3
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%d{dd MM yy HH:mm} %8p %10m%n
# Set the logger level of File Appender to WARN
log4j.appender.FA.Threshold = INFO
log4j.appender.FA.Threshold = DEBUG
**And set Properties in JAVA **
System.setProperty("fName", "d:\\siemens\\" + getDateTime() + c.getSimpleName() +".log");
PropertyConfigurator.configure("log4j.properties");
let me know if u need further query thanks
Sometimes, while rolling log files, there seems to be some issue occurring like: 'Unable to delete log file', 'Unable to move log file'.
Error: java.nio.file.FileSystemException a.log -> directory\a-2011-08-09-2.log: The process cannot access the file because it is being used by another process.
Following is the log4j configuration:
<RollingFile name="a" fileName="${sys:catalina.base}/logs/a.log" append="true" createOnDemand="true" filePattern="${sys:catalina.base}/logs/a-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601}|%-5p|%i|%T|%s|%R|%t|%c{1} - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<DefaultRolloverStrategy max="25"/>
</RollingFile>
We have WAR deployments running on Tomcat containers which are using log4j 2.5 for logging events. We have now amended the deployments' log4j2.xml configuration to have the log files roll over every 24 hours but, with this new configuration, the rollover of files are not taking place as we would expect.
Sample configuration:
<RollingFile name="file"
fileName="${sys:catalina.base}/logs/${web:contextPath}.log"
filePattern="${sys:catalina.base}/logs/${web:contextPath}-%d{dd-MMM-yyyy}.log"
append="true">
<PatternLayout pattern="%d{dd-MMM-yyyy HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" >
<header>LOG START DATE=${date:dd-MMM-yyyy HH:mm:ss.SSS} APP=${web:contextPath} TOMCAT=${env:HOSTNAME}:${env:CONNECTOR_PORT}${sys:line.separator}</header>
<footer>LOG END DATE=${date:dd-MMM-yyyy HH:mm:ss.SSS} APP=${web:contextPath} TOMCAT=${env:HOSTNAME}:${env:CONNECTOR_PORT}${sys:line.separator}</footer>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
</RollingFile>
Any ideas why the rollover is not taking place?
NOTE: The same configuration but with a <CronTriggeringPolicy schedule="0 0 0 * * ?" /> instead of TimeBasedTriggeringPolicy does rollover but, in this case, the rolled over files get created with today's date in the filename and NOT yesterday's date.
NOTE2: We have other deployments with similar configuration that do rollover every 24 hours but those configurations have the filename hardcoded instead of using ${web:contextPath}. Could this lookup have something to do with why RollingFile might not work?
--- EDIT ---
UPDATE: We are able to get TimeBasedTriggeringPolicy to rollover files using above configuration when the Tomcat instance is running on Windows but NOT when the Tomcat instance is running on Linux.
There is nothing wrong with your configuration snippet as I get the desired behaviour of time based rolling.
To test, I changed the dd-MMM-yyyy to dd-MMM-yyyy-HH-mm and my log file rolls every minute.
It must be something else that is preventing you from achieving the desired behaviour.
My setup #1:
Log4j2 v2.8.2
Apache Tomcat 8.5.13
Windows 7 Enterprise SP1
My setup #2:
Log4j2 v2.5
Apache Tomcat 7.0.77
CentOS 7 (1611)
I have the following 3 JARs in WEB-INF/lib:
log4j-api-2.5.jar
log4j-core-2.5.jar
log4j-web-2.5.jar
Here is my complete log4j2.xml for your reference:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Appenders>
<RollingFile name="RollingFileAppender"
fileName="${sys:catalina.base}/logs/${web:contextPath}.log"
filePattern="${sys:catalina.base}/logs/${web:contextPath}-%d{dd-MMM-yyyy-HH-mm}.log"
append="true">
<PatternLayout pattern="%d{dd-MMM-yyyy HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" >
<header>LOG START DATE=${date:dd-MMM-yyyy HH:mm:ss.SSS} APP=${web:contextPath} TOMCAT=${env:HOSTNAME}:${env:CONNECTOR_PORT}${sys:line.separator}</header>
<footer>LOG END DATE=${date:dd-MMM-yyyy HH:mm:ss.SSS} APP=${web:contextPath} TOMCAT=${env:HOSTNAME}:${env:CONNECTOR_PORT}${sys:line.separator}</footer>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="root" level="debug" additivity="false">
<appender-ref ref="RollingFileAppender" level="debug"/>
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFileAppender"/>
</Root>
</Loggers>
</Configuration>
I have this appender
<RollingFile name="Application" fileName="/home/user/log/abc.log" filePattern="/var/log/mylog/abc-%d{yyy-MM-dd}-%i.log.gz">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} abc %-10level %class{36} %L %M - %msg%xEx%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="5 MB" />
</Policies>
<DefaultRolloverStrategy max="9999"/>
</RollingFile>
when the 5 MB are reached and there is no space on /var/log/mylog I get the error "ERROR renaming file from xx to yy If I make space afterwards, the logger does not try again to rename the file. Any ideas how I could solve that?
I use log4j2 2.2
This is not an answer. I don't have enough reputation to make a comment.
SUGGESTION: Log4j2 allows you to automatically delete files older then X days. I've been struggling with this some time ago. The issue has been resolved here:
Log4j2 - Configure RolloverStrategy to delete old log files
Maybe this will be helpful for you.
If you configure a FailoverAppender and put your RollingFileAppender within it then you can configure the FailoverAppender to retry at an interval you configure. In the meantime, the log events can be routed to a secondary appender that can do something else. See http://logging.apache.org/log4j/2.x/manual/appenders.html#FailoverAppender.
try removing ":" here {yyyy-MM-dd HH:mm:ss.SSS}
something like below
<PatternLayout>
<Pattern>%d{yyyy-MM-dd} abc %-10level %class{36} %L %M - %msg%xEx%n</Pattern>
</PatternLayout>