Log4j2 stops logging to log file after clearing log file - log4j2

I am running my application on a WebLogic within a Linux environment. after clearing the content of the log file using ">log4j2.log". my application stops logging at all.
I have tried restarting my application, and then the server but still no logs.
also tried deleting the file using rm -f and then doing the restart but still no logs. see the config below.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" monitorInterval="60">
<Appenders>
<RollingFile name="RollingFileAppender"
fileName="./PortalLogs/log4j2.log"
filePattern="./PortalLogs/log4j2.log.%i.%d{yyyy-MM-dd}"
immediateFlush="true">
<PatternLayout>
<Pattern>%d %-5p [%X{messageExtTxt}] [%c] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<RegexFilter regex="*DefaultMessageListenerContainer*" onMatch="DENY" onMismatch="ACCEPT"/>
<DefaultRolloverStrategy max="30"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework.jms.listener.DefaultMessageListenerContainer" level="ERROR" >
<AppenderRef ref="RollingFileAppender"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="RollingFileAppender" level="INFO"/>
</Root>
</Loggers>
</Configuration>
When I tried using delete from WinSCP instead and then restarted the server, the log files were again produced and with logs in it.
My question is that is there a way to clean the log files safely without causing this issue? thanks in advance!
EDIT: Linux's RM on the log file before restarting the server actually works and recreates the log file, but still is there a way to make it work using ">log4j2.log" or after modifying the file? we need this as we have a protocol to do an initial run for automated tests, clear everything and then do an actual run.

Related

log4j2 environment variable substitution not working for filename attribute

I am trying to configure my daily rolling log file with a filename that contains server port number because this is a multi server application and each server will generate it's own log.
I have the following configuration
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="[%d{ISO8601}:$${env:SERVER_PORT}-L1] [th:%t] [%c] [%level] [%logger{36}] - %n[%d{ISO8601}:$${env:SERVER_PORT}-L2]%msg%n"/>
</Console>
<RollingFile name="dailyLog" immediateFlush="false" append="true">
<FileName>logs/rCOI.${env:SERVER_PORT:8090}.log</FileName>
<FilePattern>logs/rCOI.${env:SERVER_PORT:8090}.%d{yyyy-MM-dd}.log.zip</FilePattern>
<PatternLayout
pattern="[%d{ISO8601}:$${env:SERVER_PORT}-L1] [th:%t] [%c] [%level] [%logger{36}] - %n[%d{ISO8601}:$${env:SERVER_PORT}-L2]%msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="dailyLog"/>
</Root>
</Loggers>
</Configuration>
This produces a log called
logs/rCOI.${env:SERVER_PORT:8090}.log
so it's not even using the default. I am sure that SERVER_PORT is defined and exported because I use it in the application with no issues.
I have tried $${env:SERVER_PORT:8090} and just ${SERVER_PORT} as well. Nothing is working.
It would be nice to be able to use an environment variable for the logs/ directory as well.

Can I keep rolling file in log4j2 even though the log is empty

I have a program in java to write log using log4j2 that configured in xml configuration file.
Can I keep rolling file in log4j2 even though the log is empty. I need to use the archived log generated by log4j2. Now the archived log is only created if the log is not empty, if there is no log then the archived is not created. But I want log4j2 to keep generated the log even there is empty log. Any advice?
Here is my code:
<?xml version="1.0" encoding="UTF-8"?>
<Appenders>
<RollingFile
fileName="logs/app-%d{yyyyMMdd}.log"
filePattern="logs/$${date:yyyy-MM-dd}/app-%d{yyyyMMdd}-%i.log.gz"
name="app_file">
<PatternLayout>
<Pattern>%d %p %m %ex%n
</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<Console name="stdout" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %highlight{%class{1.}:%L} [%c] [%p] %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger additivity="false" includeLocation="true"
name="app">
<AppenderRef ref="app_file" level="trace"/>
<AppenderRef ref="stdout" level="trace"/>
</Logger>
<Logger level="error" name="com.ulisesbocchio.jasyptspringboot">
</Logger>
<Logger level="error" name="org.springframework">
</Logger>
<Root includeLocation="true">
<AppenderRef ref="stdout" />
</Root>
</Loggers>
I also added some code below in my java code
System.setProperty("Log4jContextSelector",
"org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
Seems that the file only roll over when something is written into the file. I think that basically you should do some customization, here are 2 options:
Set a timer, log empty string periodic to trigger the roll over.
Override the RollingFile and roll over file whatever you want.

Log4j2 daily log files

I want my log files created by log4j2 to have the date pattern in their file name, including the current active file. That is, if todays date is 2016-12-15, I want the current log-file to be lager-2016-12-15.log. When the date changes, I want a new file to be created named lager-2016-12-16.log.
With RollingFileAppender I am not able to get the current active log file to have date pattern in the filename. My Log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="layoutPattern">%d{ISO8601} [%t] %-5p [%X{REQUEST_ID}] [%X{CLIENT_ID}] [%X{USER_ID}] %c- %m%n</Property>
<Property name="logDir">${sys:catalina.home}/logs/</Property>
<Property name="fileName">${logDir}lager-${date:yyyy-MM-dd-HHmm}.log</Property>
<Property name="filePattern">${logDir}lager-%d{yyyy-MM-dd-HHmm}.log</Property>
</Properties>
<Appenders>
<RollingFile name="LAGER" append="true"
fileName="${fileName}"
filePattern="${filePattern}">
<PatternLayout pattern="${layoutPattern}" charset="UTF-8"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<loggers>
<Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]" level="WARN" additivity="false">
<AppenderRef ref="LAGER" />
<AppenderRef ref="CONSOLE"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="LAGER"/>
<AppenderRef ref="CONSOLE"/>
</Root>
</loggers>
</Configuration>
With this config the copying when it rolls over is messed up. Removing the date pattern from the fileName property fixes this, but the the current file does not have the date in its name.
I am running this on a tomee 7.0.1.
There may already be an outstanding feature request for this. Would this match your requirements? https://issues.apache.org/jira/browse/LOG4J2-1101
If so, please comment on that JIRA ticket. If you're able to contribute a patch (ideally with unit test) it is likely to get resolved quickly.

Log4j2 RollingFile appenders clashing

Log4j2 RollingFile appenders clashing
Below is a simplified debug version of our log4j2 configuration file (We rollover nightly not every minute!).This configuration, instead of it creating a Rollover file each minute (as per theTimeBasedTriggeringPolicy) will create one rollover file (non-tarred), containing JSON formatted logging, which will be overwritten every 20KB(although it will end up being slightly greater than 20KB (See Screenshot).We also get the following errors (abbreviated with "..."):-
2016-10-07 08:47:34,433 default-workqueue-4 ERROR Unable to copy file /.../logs/logFile-2016-10-07-08:47:11.log to /.../logs/logFile-2016-10-07-08:47:11.log: java.nio.file.NoSuchFileException /.../logs/logFile-2016-10-07-08:47:11.log
If we switch the order of the timeBasedRollingFileJsonLayout appender and the sizeBasedRollingFilePatternLayoutWithZippedArchive appender then no rollover occurs at all.
If we remove the sizeBasedRollingFilePatternLayoutWithZippedArchive appender then the timeBasedRollingFileJsonLayout appender works as expected.
We have the two different appenders for different environments, where the logs may or may not be hooked up to ELK.In our real log4j2 config file we use properties to select the appropriate appender for the environment.I have removed the properties from this file for clarity and to rule them out as a possible cause of the problem.
We are using log4j 2.6.2.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %logger{36} - %msg %n" />
</Console>
<RollingFile name="timeBasedRollingFileJsonLayout" append="true" fileName="logs/logFile.log" filePattern="logs/logFile-%d{yyyy-MM-dd-HH:mm}.log">
<JSONLayout properties="true" compact="true" eventEol="true" />
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
<RollingFile name="sizeBasedRollingFilePatternLayoutWithZippedArchive" append="true" fileName="logs/logFile.log" filePattern="logs/logFile-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %logger{36} - %msg %n" />
<Policies>
<SizeBasedTriggeringPolicy size="20KB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<logger name="logger.one" level="info" additivity="false">
<AppenderRef ref="timeBasedRollingFileJsonLayout" />
</logger>
<Logger name="logger.two" level="info" additivity="false">
<AppenderRef ref="timeBasedRollingFileJsonLayout" />
</Logger>
<Logger name="logger.three" level="info" additivity="false">
<AppenderRef ref="timeBasedRollingFileJsonLayout" />
</Logger>
<Root level="info">
<AppenderRef ref="timeBasedRollingFileJsonLayout" level="all" />
</Root>
</Loggers>
</Configuration>
I am at a loss to understand why you think this should work. You have two appenders trying to write to the same file trying to rollover based on different criteria and rollover to files with different names. It is no surprise that you are getting the file is in use error since two things have it open at once.

Karaf 4.0.5 with log4j2: my bundle log output always in console but should be in file

I configured Karaf 4.0.5 in order to fix this issue, but log output from my bundles is shown only in karaf console, not in the file. It works in Karaf 4.0.3.
Any ideas why the output from my bundles is present only in Karaf console? The changes that I made to configure log4j2:
startup.properties (corresponding jars are in ${karaf.system} folder):
mvn:org.ops4j.pax.logging/pax-logging-api/1.8.5 = 8
(this line is commented) mvn:org.ops4j.pax.logging/pax-logging-service/1.8.5 = 8
mvn:org.ops4j.pax.logging/pax-logging-log4j2/1.8.5 = 8
mvn:com.lmax/disruptor/3.3.2 = 8
org.ops4j.pax.logging.cfg:
org.ops4j.pax.logging.log4j2.config.file = ${karaf.etc}/log4j2.xml
org.ops4j.pax.logging.log4j2.async = true
system.properties
log4j.configurationFile=file:${karaf.etc}/log4j2.xml
org.ops4j.pax.logging.DefaultServiceLog.level = DEBUG
Log4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ALL">
<Appenders>
<RollingRandomAccessFile name="oapiserver" fileName="data/log/log4j2.log" filePattern="data/log/oapi-%d_%i.log.gz" immediateFlush="false">
<ThresholdFilter level="DEBUG"/>
<PatternLayout>
<pattern>%level{length=1} %date{MMdd-HHmm:ss,SSS} %logger{1.} %message [%thread]%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<DefaultRolloverStrategy max="10000"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Root level="DEBUG">
<AppenderRef ref="oapiserver"/>
</Root>
</Loggers>
For karaf-4.0.4 :
see Release note for 4.0.5 - [KARAF-4278] - clean not working.
So Either delete data directory from karaf server after the configuration for log4j2.
For karaf-4.0.5:
Run karaf clean after configuration

Resources