I have searched everywhere for the answer. I have to delete files after the number of file archived is more than 10.
This is my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="C:/temp/logs/app.log"
filePattern="C:/temp/logs/app.log.%i">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1000 kb" />
</Policies>
<DefaultRolloverStrategy max="10">
<Delete basePath="C:/temp/logs/" maxDepth="1">
<IfFileName glob="app*.log*">
<IfLastModified age="2m">
<IfAny>
<IfAccumulatedFileCount exceeds="11" />
</IfAny>
</IfLastModified>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
But it does not delete all the files in the folder. For example in the screenshot below, it does not delete the old log file:
logs
I have gone through all the documentation and lots of discussions but I couldn't solve this. Any help would be appreciated.
This is a similar question to one asked and might provide some help on what you are trying to do = Log4j2 - Configure RolloverStrategy to delete old log files.
The comments should explain what each condition under Delete would do. In your example, if you like app-log-12-20* to be deleted, then you need to either remove either the IfLastModified or IfAccumulatedFileCount - you will see the file getting deleted
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="C:/temp/logs/app.log"
filePattern="C:/temp/logs/app.log.%i">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1000 kb" />
</Policies>
<DefaultRolloverStrategy max="10">
<Delete basePath="C:/temp/logs/" maxDepth="1">
<IfAll>
<!-- Looks for file names starting with app*.log*. Any file including app.log.1 will satisfy this condition. Could delete current log app.log -->
<IfFileName glob="app*.log*" />
<!-- Looks for files that are older than 2 mins -->
<IfLastModified age="2m" />
<!-- This means there need to be 11 fails that satisfy the above conditions, that i.e. their name is app*log* and have time stamp greater than 2 mins. Keeps the most recent files that satisfy the condition -->
<IfAccumulatedFileCount exceeds="11" />
</IfAll>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
Related
<maxHistory>30</maxHistory>
I want to delete a log file that is over some number of days old in log4j2 xml so for that I was using maxHistory tag defined above and giving it a value of 30 days. But the following tags seems to be invalid and showing the following error.
Policies contains an invalid element or attribute "maxHistory
Any thoughts why this property is not working.
<RollingFile name="AppFile"
fileName="/app/logs/test.log"
filePattern="/app/logs/archive/test-%d{yyyy-MM-dd-HH}-%i.log">
<PatternLayout>
<pattern>%d [%t] %p %c - %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="500 MB" />
<maxHistory>30</maxHistory>
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com" level="INFO" additivity="false">
<AppenderRef ref="AppFile" />
</Logger>
<Root level = "info">
<AppenderRef ref = "CONSOLE" />
</Root>
</Loggers>
You need to use a custom delete action:
<RollingFile ... >
...
<DefaultRolloverStrategy>
<Delete basePath="${baseDir}" maxDepth="2">
<IfLastModified age="30d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
See https://logging.apache.org/log4j/2.x/manual/appenders.html#CustomDeleteOnRollover for full documentation.
Adding to the first answer, DefaultRolloverStrategy also provides a max field whose description is given below which helps you achieve deleting older logs.
<DefaultRolloverStrategy max="10" />
max : The maximum value of the counter. Once this values is reached older archives will be deleted on subsequent rollovers. The default value is 7.
Here counter corresponds to %i which is given in file-pattern and through this counter parameter deletion happens.
filePattern="/app/logs/archive/test-%d{yyyy-MM-dd-HH}-%i.log">
The above answer which uses <Delete> helps to give you more control of what files are deleted, but the documentation also points to use the <Delete> with care.
For more info https://logging.apache.org/log4j/2.x/manual/appenders.html
Can someone explain me why Log4j2 is saving logs into .gz file? Why not .txt?
I've changed configuration in .xml file to save in .txt and it works, but I'm not sure if it's proper way to use logs?
EDIT
My log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" disableAnsi="false" />
</Console>
<RollingFile name="RollingFile"
fileName="./logs/trade-system-logger-log4j2.log"
filePattern="./logs/$${date:yyyy-MM}/trade-system-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<!-- rollover on startup, daily and when the file reaches
10 MegaBytes -->
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="10 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- LOG everything at INFO level -->
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
<!-- LOG "com.baeldung*" at TRACE level -->
<Logger name="com.tradesystem" level="trace"></Logger>
</Loggers>
You are using a RollingFile Appender and this will archive the logs in a compressed file.
Please read the documentation: https://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender
because of your filePattern=....log.gz . rolling file will save in .gz file.
try this
filePattern="./logs/$${date:yyyy-MM}/trade-system-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log">
the rolling file will save with extension '.log'
reference: http://logging.apache.org/log4j/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html
I'm using log4j2 xml configuration with a Routing Appender and a wrapper. I'm passing different appenders to log to different files for different parts of the project. My configuration looks something like this.
<Configuration status="WARN">
<Appenders>
<Routing name="RoutingAppender">
<Routes pattern="${ctx:logFileName}">
<Route>
<RollingFile name="Rolling-${ctx:logFileName}"
fileName="logs/${ctx:logFileName}"
filePattern="logs/${ctx:logFileName}.%i.log.gz">
<PatternLayout pattern="d{ABSOLUTE} %level{length=5} [%thread] %logger{10}.%method:%line-%msg%n"/>
<SizeBasedTriggeringPolicy size="100 MB" />
</RollingFile>
</Route>
<!-- This route is chosen if ThreadContext has no value for key logFileName} -->
<Route key="$${ctx:logFileName}">
<RollingFile name="Rolling-default" fileName="logs/WITHOOUT-THREAD-CONTEXT.log"
filePattern="./logs/${date:yyyy-MM}/default-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<pattern>%d{ABSOLUTE} %level{length=5} [%thread] %logger{10}.%method:%line-%msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true" />
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
</RollingFile>
</Route>
</Routes>
</Routing>
</Appenders>
The problem is, I'm getting external library logs(those without appenders) together with my project logs in the second route, although I want only external logs.
I would suggest to use configuration something like below -
<Configuration status="WARN">
<Appenders>
<RollingFile name="RollingFile"
fileName="logs/${ctx:logFileName}"
filePattern="logs/${ctx:logFileName}.%i.log.gz">
<PatternLayout pattern="d{ABSOLUTE} %level{length=5} [%thread] %logger{10}.%method:%line-%msg%n"/>
<SizeBasedTriggeringPolicy size="100 MB" />
</RollingFile>
<RollingFile name="Rolling-default" fileName="logs/WITHOOUT-THREAD-CONTEXT.log"
filePattern="./logs/${date:yyyy-MM}/default-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<pattern>%d{ABSOLUTE} %level{length=5} [%thread] %logger{10}.%method:%line-%msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true" />
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="application.package" level="info" additivity="false">
<appender-ref ref="RollingFile" />
</Logger>
<Root level="info" additivity="false">
<appender-ref ref="Rolling-default" />
</Root>
</Loggers>
</Configuration>
In this configuration, there are 2 loggers. One logger is for your application having root package name - application.package. This package should contain all sub-packages and classes of your application. It will use RollingFile Appender for logging.
Another is Root logger which can be used for logging external libraries i.e. classes which are not in application.package. It will use Rolling-default Appender.
In above configuration, you are setting fileName through context lookup so you have to set context value in the beginning otherwise you will get error.
This is my scenario:
For my website I have used log4j2 for rolling log files. currently I am generating 3 log file which consist of 2 rolling file (trace & error) and 1 routing appender.
1) Trace log of entire date (rolling file appender)
2) Error log of entire date (rolling file appender)
3) logged users activity log for entire day (routing file appender)
Below is my log4j2.xml using for the above scenario, which works exactly as per the requirement.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="WARN" name="mywebsite" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%logger{1}] - %msg%n" />
</Console>
<RollingFile name="error-log" append="true"
fileName="D:/client/error [${date:yyyy-MM-dd}].log" filePattern="D:/lient/error-%d{yyyy-MM-dd}.log">
<PatternLayout
pattern="[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%logger{1}] - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<SizeBasedTriggeringPolicy size="250 MB" />
</Policies>
</RollingFile>
<RollingFile name="trace-log" append="true"
fileName="D:/client/trace [${date:yyyy-MM-dd}].log" filePattern="D:/client/trace-%d{yyyy-MM-dd}.log">
<PatternLayout
pattern="[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%logger{1}] - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<SizeBasedTriggeringPolicy size="250 MB" />
</Policies>
</RollingFile>
<Routing name="RoutingAppender">
<Routes pattern="$${ctx:logFileName}">
<Route>
<RollingFile name="Rolling-${ctx:logFileName}" append="true"
fileName="D:/userlog/${ctx:logFileName}~${date:yyyy-MM-dd}.log"
filePattern="D:/userlog/${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout
pattern="[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%logger{1}] - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<SizeBasedTriggeringPolicy size="512 MB" />
</Policies>
</RollingFile>
</Route>
<Route ref="Console" key="${ctx:logFileName}" />
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="trace" additivity="false">
<Appender-Ref ref="Console"/>
<Appender-Ref ref="trace-log" level="trace" />
<Appender-Ref ref="error-log" level="error" />
<Appender-Ref ref="RoutingAppender" />
</Root>
</Loggers>
</Configuration>
I am new to log4j, I somehow managed to configure the log4j xml from the sources in internet.
Rolling file were created for every day and file append-er created for each user and catches the respective events in the respective files for the entire day, until a new file rolls out on next day.
Issue is :
When a new file rolls on, few events of current day were added in the previous day log file.
For example the trace.log file of 1st November has the few log events(say around 5-10 lines) entries of 2nd November.
This happens for file append-er too .
For example file append-er will generate log file based on user log's in say log file named john-01-112015.log was created yesterday. and same user log's in 2nd November a new file will be rolled out as per the configuration. but the few logs of 2nd November is added in the john-01-112015.log as said in the previous scenario.
Is there anything wrong in the log4j2.xml ? guys help me to solve this issue.
I was using log4j2 and it was logging statements for me with no issues. I might have made some changes (moved from info to debug and back) but its quite possibly I might have messed up the config in some other fashion. I am copying my config file below (I have not moved any log4j2 and slf4j jar files from my project). Any thoughts?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="TestApp" packages="">
<Appenders>
<RollingRandomAccessFile name="RollingRandomAccessFile" fileName="logs/test.log" immediateFlush="false" append="false"
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="500 MB"/>
</Policies>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncLogger name="FATAL_LOGGER" level="fatal" includeLocation="true" additivity="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</AsyncLogger>
<Root level="debug" includeLocation="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</Root>
</Loggers>
</Configuration>
I tested the config in a test project and it works without any issues. Ensure that you do not have any locks on the file and have correct read/write privileges on the file/directory.