Delete old Logs with Log4j2.xml is not working - java

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

Related

log4j2 - DefaultRolloverStrategy max value given 1000 but making at max 100

I have used DefaultRolloverStrategy for rolling files in which if I am giving max value less than 100 it is working ax expected that if assigned 50 it is making at max 50 files but when max set to greater than 100 like 200 or 1000 it is making at max 100 files.
Like in this case i have set max file size 1 kb so it will rollover when it file exceeds 1 kb and log.info.file.pattern is %d{yyyy-MM-dd}-%03i.log.gz so it is day based and max in defaultrolloverstrategy is 1000
<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.example">
<Properties>
<!-- <property name="FILE_SIZE">${bundle:application:max.file.size}</property>-->
<property name="FILE_SIZE">${sys:max.file.size:-1 KB}</property>
<property name="infoLogFileName">${sys:log.info.file.name:-./logs/app-all.log}</property>
<property name="warnLogFileName">${sys:log.warn.file.name:-./logs/app-warn.log}</property>
<property name="kafkaLogFileName">${sys:log.kafka.file.name:-./logs/app-kafka.log}</property>
<property name="infoLogFilePattern">${sys:log.info.file.pattern}</property>
<property name="warnLogFilePattern">${sys:log.warn.file.pattern}</property>
<property name="kafkaLogFilePattern">${sys:log.kafka.file.pattern}</property>
<property name="consolePatternLayout">${sys:console.pattern:-}</property>
<property name="numberOfRolloverFiles">${sys:rollover.files:-1000}</property>
<property name="logLevel">${sys:log.level:-INFO}</property>
``
</Properties>
<ThresholdFilterCustom />
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="${consolePatternLayout}" />
</Console>
<RollingFile name="infoLogFile"
fileName="${infoLogFileName}"
filePattern="${infoLogFilePattern}">
<LevelRangeFilter minLevel="FATAL" maxLevel="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%t] %logger{36} : %msg%n</pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="${FILE_SIZE}" />
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="1000"/>
</RollingFile>
<RollingFile name="warnLogFile"
fileName="${warnLogFileName}"
filePattern="${warnLogFilePattern}">
<LevelRangeFilter minLevel="FATAL" maxLevel="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%t] %logger{36} : %msg%n</pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="${FILE_SIZE}" />
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="${numberOfRolloverFiles}"/>
</RollingFile>
<RollingFile name="kafkaLogFile"
fileName="${kafkaLogFileName}"
filePattern="${kafkaLogFilePattern}">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%t] %logger{36} : %msg%n</pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="${FILE_SIZE}" />
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="${numberOfRolloverFiles}"/>
</RollingFile>
</Appenders>
<Loggers>
<!-- LOG everything at INFO level -->
<Root level="${logLevel}">
<AppenderRef ref="Console" />
<AppenderRef ref="infoLogFile" />
<AppenderRef ref="warnLogFile" />
</Root>
<!-- LOG "com.baeldung*" at TRACE level. This has no effect, as we're doing from command line -->
<Logger name="org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer" level="ERROR" ></Logger>
<Logger name="org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient" level="ERROR"></Logger>
<Logger name="org.springframework.kafka.support.LoggingProducerListener" level="INFO" additivity="false">
<appender-ref ref="kafkaLogFile" />
</Logger>
</Loggers>
</Configuration>

Log4j2 not rolling over log files

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>

Log4j2.xml configuration file not found after configuration change

I'm having a problem with the log4j2.xml configuration file not being found after a change to the configuration file was made. Before the change the file was found and the simple logging was working. I haven't used log4j in a few years and have been making use of the java.util logging for application logging on the weblogic server so I decided to put log4j to use again.
I first create a simple configuration file, tested it out and everything was working just fine.
The original test configuration.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="filename">/opt/www/log</Property>
</Properties>
<Appenders>
<RollingFile name="StipLog" fileName="${filename}/stip-log.log" filePattern="${filename}/stip-log-%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" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="stip-debug" level="debug" additivity="true">
<appender-ref ref="StipLog" level="debug"/>
</Logger>
<Root level="debug">
<AppenderRef ref="StipLog"/>
</Root>
</Loggers>
<Configuraiton>
Runing this code snippet in a backing bean for a page worked just fine with the original configuration file.
LOGGER.info("APP Home Page: User: ".concat(userInfoBean.getUserName()));
LOGGER.debug("DEBUG message!");
LOGGER.error("ERROR Message", new NullPointerException("I AM NULL"));
That all went well so I decided to try and configure different appenders for each log level I wanted to use. I changed the original configuration to the following.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<!-- For local logging change this porperty to a local directory. ex: C:\\Public\\log -->
<Property name="LOGGING_ROOT">C:/Public/log</Property>
</Properties>
<Appenders>
<!-- file appenders -->
<RollingFile name="debugLog" fileName="${LOGGING_ROOT}/app-debug.log" filePattern="${LOGGING_ROOT}/app-debug-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="DEBUG" maxLevel="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="infoLog" fileName="${LOGGING_ROOT}/app-info.log" filePattern="${LOGGING_ROOT}/app-info-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="errorLog" fileName="${LOGGING_ROOT}/app-error.log" filePattern="${LOGGING_ROOT}/app-error-%d{yyyy-MM-dd}-%i.log">
<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<!-- console appender -->
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="debug-logger" level="debug" additivity="true">
<appender-ref ref="debugLog"/>
</Logger>
<Logger name="info-logger" level="info" additivity="true">
<appender-ref ref="infoLog"/>
</Logger>
<Logger name="error-logger" level="error" additivity="true">
<appender-ref ref="errorLog"/>
</Logger>
<Root level="all">
<AppenderRef ref="console"/>
<AppenderRef ref="debugLog"/>
<AppenderRef ref="infoLog"/>
<AppenderRef ref="errorLog"/>
</Root>
</Loggers>
</Configuration>
After changing the configuration file I am now getting the following error message when the application is deploying.
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.
The configuration file is in the same location as it was when the loggin was working, it's in the source default package. I moved it WEB-INF to see if that would make a difference but the same problem remains. I even changed it back to the original configuration and still have the same problem.

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