I have the following log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?> <configuration status="OFF"> <appenders> <RollingFile name="testLog" fileName="test.log" filePattern="" append="false"> <PatternLayout pattern="[%t] %-5level - %msg%n%n"/> <SizeBasedTriggeringPolicy size="5mb" /> </RollingFile> </appenders> <loggers> <logger name="TestsLogger" level="trace" additivity="false"> <appender-ref ref="testLog"/> </logger> <root level="debug"> <appender-ref ref="testLog"/> </root> </loggers> </configuration>
How can I modify this configuration such that
How can I limit the number of partial log files created in 1.? What I want to achieve is a scheme like the following:
creating test1.log [present log files: test1.log] test1.log - 5mb limit reached creating test2.log [present log files: test1.log, test2.log] test2.log - 5mb limit reached creating test3.log [present log files: test2.log, test3.log] test3.log - 5mb limit reached creating test4.log [present log files: test3.log, test4.log] and so on
Does anyone know, how to achieve something like that? Of course it would be nice, if something like that was possible with log4j2 alone. But maybe there is a way to combine log4j2 with some kind of external program that would run alongside the main Java application and delete superfluous log files, while keeping the two last log files intact. So if anyone has at least a suggestion for 1., it may be already, what I'm looking for. Because I may be able to write a program for the 2nd part. Of course it would be awesome, if the 2nd part could be done with log4j2 as well.
No more than 2 or 3 entries per user action though, unless you are doing batch operations. Don't put more than 2MB in a file, so the user can email it you. Don't keep more than 50MB of logs, because it's probably not your space you are wasting here.
Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.
Note that level names are case sensitive.
Log file rolling offers the following benefits: It defines an interval over which log analysis can be performed. It keeps any single log file from becoming too large and assists in keeping the logging system within the specified space limits.
I never used log4j2 so far but the documentation of the RollingFileAppender gives you a lot of configuration examples.
Interesting for you seams to be something like this (using DefaultRolloverStrategy
):
<RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz"> <PatternLayout> <pattern>%d %p %C{1.} [%t] %m%n</pattern> </PatternLayout> <Policies> <SizeBasedTriggeringPolicy size="5 MB"/> </Policies> <DefaultRolloverStrategy max="20"/> </RollingFile>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With