我已经设置了以下log4j.properties文件,以便控制台上显示信息事件,并且警告中的所有内容都会显示在日志文件中。
log4j.rootLogger=info, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%m
log4j.logger.org.company.package=warn, file
log4j.additivity.org.company.package=false
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=SmartBay.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{DATE} [%t] %-5p %c - %m%n
我还希望在控制台上显示致命事件,以便管理员可以轻松看到应用程序已终止,然后查看日志文件以获取详细信息。有什么方法可以干净利落地完成这件事吗?我尝试将以下内容添加到文件中,但根据我的节奏,它会阻止sh
的错误log4j.logger.net.compusult.sb_SensorClient_v1_0_0=fatal, console2, file
log4j.appender.console2=org.apache.log4j.ConsoleAppender
log4j.appender.console2.layout=org.apache.log4j.PatternLayout
log4j.appender.console2.layout.ConversionPattern=%5p: %m
答案 0 :(得分:0)
您可以在日志文件中拥有多个appender,并为其分配不同的日志级别。
log4j.rootLogger = consoleAppender, fileAppender
#Console Appender
log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%m
# Console appender to fatal
log4j.appender.consoleAppender.Threshhold = FATAL
#File Appender
log4j.appender.fileAppender=org.apache.log4j.FileAppender
log4j.appender.fileAppender.File=SmartPay.log
.
.
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%d{DATE} [%t] %-5p %c - %m%n
# File Appender to warn
log4j.appender.fileAppender.Threshold = WARN