I have a Spring project using log4j on Linux (Debian, Ubuntu and RHEL). Now I would like to implement best practice logs rotation for the log4j generated logs.
I have previously used logrotate.d for other non-java components and it worked great for me. I needed rotating by size/time and compressing the old logs and logrotate could do all of this.
I am new to log4j and now wonder how should I configure log rotation.
What is your experience? What is the best practice?
You can roll and delete log4j log files by time intervals or by file size. File size log rotation is controlled by CompositeRollingAppender. Time interval log rotation is controlled by CompositeRollingAppender and a Java property in the common services start file.
logrotate is a log managing command-line tool in Linux. The administrators write the rules and policies for handling different log files into configuration files. Through the configuration file, logrotate will execute the appropriate function to manage the matching log files.
How do I rotate log files under Linux operating system? A. You need use tool called logrotate, which is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files.
Logrotate runs the postrotate script each time it rotates a log specified in a configuration block. You usually want to use this script to restart an application after the log rotation so that the app can switch to a new log. >/dev/null tells logrotate to pipe the command's output to nowhere.
In general, I usually use log4j for logging and initial rotations, and I use logrotate for compression and archiving. The reason is that I try and keep the logging rules, including which file(s) to log, size, naming convention, etc. to be associated with the application. I then try and put the sysadmin type configurations, like how many to keep, when to compress, when to move to backup directory in the logrotate.
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