log4j:Appender to print the log Statements

£可爱£侵袭症+ 提交于 2020-01-05 11:47:14

问题


Hello sir: When server is started ,the log files print the output statement..how can i print the log files in my own file?


回答1:


Configure a log4j properties file with file appender. Something like this -

log4j.rootLogger=WARN, fileAppender
log4j.logger.myPackage=DEBUG, fileAppender
log4j.additivity.myPackage=false
log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.fileAppender.File= C://Myfile.log
log4j.appender.fileAppender.MaxFileSize=1024KB 
log4j.appender.fileAppender.MaxBackupIndex=5 
log4j.appender.fileAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%d [%t] %-5p %c - %m%n



回答2:


You can try using a properties file

log4j.rootLogger=INFO, ERRORFILE

#ERRORFILE - used to log error messages
log4j.appender.ERRORFILE=org.apache.log4j.RollingFileAppender
log4j.appender.ERRORFILE.Threshold=ERROR
log4j.appender.ERRORFILE.File=errorLog.log
log4j.appender.ERRORFILE.MaxFileSize=200KB
log4j.appender.ERRORFILE.MaxBackupIndex=5
log4j.appender.ERRORFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ERRORFILE.layout.ConversionPattern=%d{DATE} %t - %m%n

In this example, I configured a properties file to log error messages to my own file, errorLog.log. Here's another sample.



来源:https://stackoverflow.com/questions/6124148/log4jappender-to-print-the-log-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!