如何在java中输出日志消息

如何在java中输出日志消息,第1张

配置文件设置如下:

log4j.rootLogger=debug,stdout,logfile  

log4j.appender.stdout=org.apache.log4j.ConsoleAppender  

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n  

  

log4j.appender.logfile=org.apache.log4j.RollingFileAppender  

log4j.appender.logfile.File=logfile.log  

log4j.appender.logfile.MaxFileSize=512KB  

log4j.appender.logfile.MaxBackupIndex=3  

log4j.appender.logfile.layout=org.apache.log4j.PatternLayout  

log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n 测试java 文件

log4j.rootLogger=debug,stdout,logfile  

log4j.appender.stdout=org.apache.log4j.ConsoleAppender  

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n  

  

log4j.appender.logfile=org.apache.log4j.RollingFileAppender  

log4j.appender.logfile.File=logfile.log  

log4j.appender.logfile.MaxFileSize=512KB  

log4j.appender.logfile.MaxBackupIndex=3  

log4j.appender.logfile.layout=org.apache.log4j.PatternLayout  

log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.rootLogger=info, ServerDailyRollingFile, stdout

log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender

log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd

log4j.appender.ServerDailyRollingFile.File=/usr/logs/mylog.log

log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout

log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n

log4j.appender.ServerDailyRollingFile.Append=true

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %p [%c] %m%n

此文档放在项目编译文件.class相同的位置

### set log levels ###log4j.rootLogger = debug , stdout , D### 输出到控制台 ###log4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n### 输出到日志文件 ###log4j.appender.D = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.D.File = logs/log.loglog4j.appender.D.Append = truelog4j.appender.D.Threshold = DEBUGlog4j.appender.D.layout = org.apache.log4j.PatternLayoutlog4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n### 保存异常信息到单独文件 ###log4j.appender.D = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.D.File = logs/error.loglog4j.appender.D.Append = truelog4j.appender.D.Threshold = ERRORlog4j.appender.D.layout = org.apache.log4j.PatternLayoutlog4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

配置获取方法:

PropertyConfigurator.configure( "D:\\Project Files\\myeclipse\\commontest\\bin\\log\\log4j\\log4j.properties" )

Logger log=Logger. getLogger(TestLog4j.class)

log.warn( "嘿嘿")

log.debug( "呜呜")

log.error( "错啦")


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/7937823.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存