问题
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