Log4j - Have multiple appenders write to the same file with one that always logs

后端 未结 4 1238
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 21:43

I have a log4j appender defined like:

log4j.logger.com.example = DEBUG, filelog

log4j.appender.filelog=org.apache.log4j.DailyRollingFileAppender
log4j.appen         


        
4条回答
  •  独厮守ぢ
    2020-12-09 22:06

    I don't think log4j really supports two appenders writing to the same file because of synchronization issues. Your best bet would be, if possible, to make a switch to slf4j using Logback as your backend logging system since Logback is log4j's successor and also supports multiple appenders writing to one file. Check out FileAppender and it's prudent mode.

    Update: from your description, it seems you should also check out markers. You only need one logger instance, and you can get more fine grained control using markers, because with them you basically say "this log statement has a special purpose, and has to be logged using an appropriate appender". You can also check log appender additivity, which is usually used when you use two or more appenders for one log statement.

提交回复
热议问题