Log4j2 not rolling over log files - java

I am making a game with LWJGL3 and am using log4j2 for logging. I am trying to setup my logger which works however each time the program is executed it just overwrites the lastest.log file instead of compressing it and then overwriting latest.log. I tried setting the minSize of the OnStartupTriggeringPolicy because it defaults to 1 and my file size is currently extremely small but that didn't help. Does anyone have any idea on how to get this working.
log4j2.xml
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%level] [%t] - %msg%n" />
</Console>
<RollingFile name="RollingFile" filename="log/latest.log"
filePattern="${logPath}/%d{YYYYMMddHHmmss}.log.gz"
append="false">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%level] [%t] - %msg%n" />
<Policies>
<OnStartupTriggeringPolicy minSize="0"/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
<DefaultRolloverStrategy max="8"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>

Have you tried adding:
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
Here is an example of log4j2 configuration that I currently used in my projects.
<RollingFile name="file-log" fileName="${log-path}/autodeployer.log"
filePattern="${log-path}/autodeployer-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="50MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>

Related

Delete old Logs with Log4j2.xml is not working

Deleting the 7 days old log files with log4j.xml
I am trying to delete old log files by using DefaultRolloverStrategy but its not working.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">Logs</Property>
<Property name="LOG_PATTERN">
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="${LOG_PATTERN}" />
</Console>
<RollingFile name="InfoFileAppender" fileName="${log-path}/demo-app.log"
filePattern="${log-path}/demo-app-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<!--<SizeBasedTriggeringPolicy size="10 MB" /> -->
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${log-path}/" maxDepth="1">
<IfFileName glob="demo-app-*.log" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="ErrorFileAppender" fileName="${log-path}/demo-err.log"
filePattern="${log-path}/demo-err-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<!-- <SizeBasedTriggeringPolicy size="5 MB"/> -->
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${log-path}/" maxDepth="1">
<IfFileName glob="demo-err-*.log" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="InfoFileAppender" level="info" />
<AppenderRef ref="ConsoleAppender" level="info" />
<AppenderRef ref="ErrorFileAppender" level="error" />
</Root>
</Loggers>
</Configuration>
But its working in my computer but its not working on Linux server. Log files storing path in Linux server is /opt/Logs.
can anyone please help on this.
your file name pattern should be "demo-err-*.log.gz", you missed ".gz".

log4j2 async logging setup

I changed my logging from sync to async but I am not sure how to put policies on it. I want to apply my sync logging settings to async logging. Please see below.
// I switched from this
<RollingFile name="fileLogger" fileName="${logPath}/log.log"
filePattern="${logPath}/log-%d{yyyy-MM-dd-hh}-%i.log">
<PatternLayout>
pattern="${logPattern}"/>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
//to this
<File name="prodLog" fileName="${logPath}/log.log">
<PatternLayout
pattern="${logPattern}"/>
</File>
<Async name="asyncLogger" includeLocation="true">
<AppenderRef ref="prodLog"/>
<ArrayBlockingQueue/>
</Async>
I dont think you can use the same policies for the File appender.
To achieve an asynchronous behavior for a rolling file, you could use the RollingRandomAccessFile, just like the following example:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
<Appenders>
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>>
</PatternLayout>
</Console>
<RollingRandomAccessFile name="Rolling-Random-Access-File-Appender"
fileName="logs/rollingrandomaccessfile.log"
filePattern="archive/logs/rollingrandomaccessfile.log.%d{yyyy-MM-dd-hh-mm}.gz">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncLogger name="guru.springframework.blog.log4j2async" level="debug">
<AppenderRef ref="Rolling-Random-Access-File-Appender"/>
</AsyncLogger>
<Root level="debug">
<AppenderRef ref="Console-Appender"/>
</Root>
</Loggers>
</Configuration>
More on this in this post: https://springframework.guru/asynchronous-logging-with-log4j-2/
Regards

log4j: How can I direct logs into different files for a cron and for a webservice?

I have a Cron and a Webservice, both implemented using spring. The cron and the webservice use a set of classes A, B and C to achieve their objective.
In each class, I use log4j 2 as the logging mechanism as so:
Logger log = LogManager.getLogger(A.class.getName());
In the log4j.xml, I have a single RollingAppender which logs to a file.
Now, I would like the Cron to log to a different file i.e. use a different appender. But if I set the category for the cron to use a different appender, that still doesn't cause the logs from A, B and C to go into that appender.
Update: log4j configuration:
<Configuration status="warn" name="mylogger" packages="">
<Properties>
<Property name="baseDir">/var/log/tomcat</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${baseDir}/app.log"
filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd}.log.gz">
<PatternLayout><Pattern>%5p %d{ISO8601} [%t][%x] %c - %m%n</Pattern></PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
You can use below mentioned configuration if you want to log into different files using same class.
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="rollingFileAppender"
fileName="/data/abc.log"
filePattern="/data/abc-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
</RollingFile>
<RollingFile name="rollingFilesAppender"
fileName="/data/cde.log"
filePattern="/data/fgh-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="CONSOLE" />
</Root>
<Logger name="rollingFilesLogger" additivity="false" level="WARN">
<AppenderRef ref="rollingFilesAppender" />
</Logger>
<Logger name="com.log4jtest" additivity="false" level="INFO">
<AppenderRef ref="rollingFileAppender" />
</Logger>
</Loggers>
In Java file you can use like :
private static final Logger LOGGER = LogManager.getLogger(Hello.class);
private static final Logger SECOND_LOGGER = LogManager.getLogger("rollingFilesLogger");
Using this you will be able to send logs in two different files.
Ref : https://github.com/ragnar-lothbrok/log4j2-example

log4j2 xml configuration log to 2 files

can anyone see what's is wrong with this xml configuration? It's to setup 2 different log files then log trace to 1 file and info to another:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingRandomAccessFile name="LogTrace" fileName="../logs/Trace.log" filePattern="../logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="25 MB"/>
</Policies>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="LogInfo" fileName="../logs/Info.log" filePattern="../logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="25 MB"/>
</Policies>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Logger name="monitor" level="info">
<AppenderRef ref="LogInfo" level="info"/>
</Logger>
<Root level="trace">
<AppenderRef ref="LogTrace" level="trace"/>
</Root>
</Loggers>
</Configuration>
The LogTrace is working but not the LogInfo...
In your code, do you use the monitor logger like this:
Logger logger = LogManager.getLogger("monitor");
logger.info("test info message");
The above should work since your config declares the info level logger with name "monitor".
Using the configuration below along with the code supplied by Remko in his answer did exactly what I am looking for:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingRandomAccessFile name="LogTrace" fileName="../logs/Trace.log" filePattern="../logs/$${date:yyyy-MM}/tracelog-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="25 MB"/>
</Policies>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="LogInfo" fileName="../logs/Info.log" filePattern="../logs/$${date:yyyy-MM}/infolog-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="25 MB"/>
</Policies>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Logger name="monitor" level="all">
<AppenderRef ref="LogInfo" level="info"/>
<AppenderRef ref="LogTrace" level="trace"/>
</Logger>
</Loggers>
</Configuration>
It's about as simple as it can be... once you have spent a few DAYS mulling over the various configuration options!

How to write different logs in different files with log4j2 (MDC in xml)?

Now I'm using structure like this:
Appender:
<RollingFile name="user.log" append="true" fileName="users/%MDC{USERNAME}.txt"
filePattern="users/archive/%MDC{USERNAME}-%d{MM-dd-yyyy}-%i.txt.gz">
<PatternLayout pattern="%-5p %d{MMMM-dd HH:mm:ss} %X: %c - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
</RollingFile>
Logger:
<appender-ref ref="user.log">
<ThreadContextMapFilter onMatch="ACCEPT" onMismatch="DENY" operator="or">
<KeyValuePair key="USERNAME" value="%X{USERNAME}"/>
<KeyValuePair key="IP" value="%X{IP}"/>
</ThreadContextMapFilter>
</appender-ref>
But it doesn't work with MDC keys. How could I use MDC keys in xml to config RollingFileAppender?
Take a look at RoutingAppender. Maybe this can get you started:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="DEBUG" name="MyApp" packages="">
<appenders>
<Routing name="Routing">
<Routes pattern="$${ctx:USERNAME}">
<Route>
<RollingFile name="user.log" append="true" fileName="users/${ctx:USERNAME}.txt"
filePattern="users/archive/${ctx:USERNAME}-%d{MM-dd-yyyy}-%i.txt.gz">
<PatternLayout>
<pattern>%d{ISO8601} [%t] %p %c %L - %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
</RollingFile>
</Route>
</Routes>
</Routing>
</appenders>
<loggers>
<root level="TRACE">
<appender-ref ref="Routing" level="DEBUG" />
</root>
</loggers>
</configuration>

Categories

Resources