Search code examples
loggingfile-descriptorlog4cpp

Have log4cpp create a new log file during execution


I currently use Log4cpp for logging purposes on a Linux RHEL platform.

I continuously log a daemon application.

My problem is that the log file generated by log4cpp may get removed at anytime (with an rm command, a logrotate etc...) and although the file is deleted, the file descriptor is still opened by the daemon. Log4cpp keeps trying to log into the file, but nothing is added, the new log lines are lost until I manually create a new log file.

I addressed the logrotate issue by using a SIGHUP signal in the logrotate configuration so that the daemon reinitis the log4cpp instance at each log rotation. But I wish a smarter way, because a fresh user will not send a SIGHUP to my daemon after swipping out the log directory, and will notice very later that the daemon is not logging.

Any ideas ?


Solution

  • Instead of using logrotate to change the output file, you could use the RollingFileAppender of log4cpp

    An example of log4cpp.properties could be :

    log4cpp.rootCategory=DEBUG, rolling
    log4cpp.appender.rolling=RollingFileAppender
    log4cpp.appender.rolling.maxFileSize=1048576
    log4cpp.appender.rolling.maxBackupIndex=5
    log4cpp.appender.rolling.fileName=output.log 
    log4cpp.appender.rolling.layout=PatternLayout 
    log4cpp.appender.rolling.layout.ConversionPattern=%d [%p] - %m%n