12

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.

  1. Should I use log4j only for logging and logrotate for rotation?
  2. Use only log4j for rotation?
  3. Use a mix of both?

What is your experience? What is the best practice?

hnviet
  • 1,747
  • 1
  • 18
  • 22
  • Consider using application server logging mechanism too. It can help you when non of following methods work. – Erfankam Jul 20 '19 at 12:23

2 Answers2

6

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.

Steven Mastandrea
  • 2,752
  • 20
  • 26
  • 2
    Didn't you find such configuration a hassle? You need to keep to configuration set, which are quite dependent. For example rotating rule like size or frequency you put in log4j when how many, compression etc. you kept in logrotate. Have you maybe tried the compression module for log4j? – hnviet Apr 05 '11 at 13:45
  • Awesome answer - I was also trying to justify using both and couldn't come up with a clear "separation of concerns." –  Feb 20 '13 at 18:58
0

There is no equivalent for logrotate on Windows (see https://serverfault.com/questions/358172/equivalent-of-logrotate-for-windows). So if you ever plan to run your Java application on Windows, log4j is the only option you have for configuring log rotation.

Community
  • 1
  • 1
asmaier
  • 11,132
  • 11
  • 76
  • 103