log4j简单案例
package log4j; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class Log4jG { // private static Logger log =Logger.getLogger(Log4jG.class); private static Logger log=LogManager.getLogger(Log4jG.class.getName()); private static Logger log1=LogManager.getLogger("myTest1"); private static Logger log2=LogManager.getLogger("myTest2"); private static Logger log3=LogManager.getLogger("myTest3"); public static void main(String[] args){ // BasicConfigurator.configure(); PropertyConfigurator.configure("c:\\log4j.properties"); log.debug("debug message"); log.info("info message"); log.error("error message"); log1.debug("test1 debug"); log1.info("test1 info message"); log1.error("test1 error message"); log2.debug("test2 debug"); log2.info("test2 info message"); log2.error("test2 error message"); log3.debug("test3 debug"); log3.info("test3 info message"); log3.error("test3 error message"); } }log4j.properties
log4j.rootLogger=INFO,A log4j.appender.A=org.apache.log4j.FileAppender log4j.appender.A.file=c:/rootlog.txt log4j.appender.A.layout=org.apache.log4j.PatternLayout log4j.logger.myTest1= DEBUG, test1 log4j.appender.test1=org.apache.log4j.FileAppender log4j.appender.test1.File=c:/test1.log log4j.appender.test1.layout=org.apache.log4j.PatternLayout log4j.appender.test1.layout.ConversionPattern=%d %p [%c] - %m%n log4j.logger.myTest2= DEBUG, test2 log4j.appender.test2=org.apache.log4j.FileAppender log4j.appender.test2.File=c:/test2.log log4j.appender.test2.layout=org.apache.log4j.PatternLayout log4j.appender.test2.layout.ConversionPattern=%d %p [%c] - %m%n log4j.logger.myTest3= DEBUG, R log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File= C:/rolling.log # Control the maximum log file size log4j.appender.R.MaxFileSize=2KB # Archive log files (one backup file here) log4j.appender.R.MaxBackupIndex=1 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j1.2.17:http://logging.apache.org/log4j/1.2/download.html
log4j1.2.17 API:http://logging.apache.org/log4j/1.2/apidocs/
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!