Log4j daily rolling catalina.out without restarting Tomcat?

房东的猫 提交于 2019-12-06 13:34:18
Augustin Ghauratto

There are three solutions for this problem:

  1. change default tomcat logging façade that writes to catalina.out to for example: slf4j, with all the benefits that comes with using it and log4j.
  2. configure system cron to run logrotate of tomcat log files
  3. change default logging class from ConsoleAppender to FileAppender.

Benefits of solutions:

  1. very flexible as the slf4j offers many options especially with log4j, that you use anyway.
  2. simple and doesn't require touching tomcat configuration.
  3. simple change of configuration that disables console output

Disadvantages:

  1. require additional libraries, affects all applications that tomcat is hosting, requires replacing default configuration with log4j.
  2. cron+logrotate works only in linux; it might be not as simple in windows with scheduler. Requires extra scripting in windows environment.
  3. provides only simple backup with date. Date pattern cannot be set. Does not compress rotated files.

    Solution for First issue, is described here
    Solution for Second issue is described here
    Solution for Third issue is described here

You can as well combine solutions. For example use crontab to gzip files that where created by changing catalina.out to other name. I would also suggest to leave tomcat so it logs to catalina.out, and configure your application to different file with log4j. This way logs from tomcat that are not immaterial won't spam your logs.

Is it even possible to rotate my logfiles without restarting Tomcat?

Yes, if you're willing to work for it.

Your log4j configuration will only end up fighting with the standard shell redirection that bin/catalina.sh uses to redirect stdout to logs/catalina.out. You can't simply use log4j configuration to change how System.out behaves.

If you want to rotate conf/catalina.out you will have to take some alternative measures depending on how to launch Tomcat:

  • If you use jsvc to launch Tomcat and you are using commons-daemon 1.0.4 or later, then you can send SIGUSR1 to the jsvc process to re-open the log files. That will allow you to move the existing log file to another file (which just changes its name and continues to log to the new filename) and then do 'kill SIGUSR1': the original filename will then be re-opened and new logs messages will go to it.
  • If you use bin/catalina.sh to launch Tomcat, you can modify it so that it no longer does redirection and instead pipes output to a rolling-logger process like Apache httpd's rotatelogs or chronolog.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!