log4j输出模板
Posted 蓝豆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了log4j输出模板相关的知识,希望对你有一定的参考价值。
logger.info("1234567890");
1、log4j.properties文件如下
# Configure logging for testing: optionally with log file
#\u53EF\u4EE5\u8BBE\u7F6E\u7EA7\u522B\uFF1Adebug>info>error
#debug:\u53EF\u4EE5\u663E\u5F0Fdebug,info,error
#info:\u53EF\u4EE5\u663E\u5F0Finfo,error
#error:\u53EF\u4EE5\u663E\u5F0Ferror
log4j.rootLogger=debug,appender1
#log4j.rootLogger=info,appender1
#log4j.rootLogger=error,appender1
#\u8F93\u51FA\u5230\u63A7\u5236\u53F0
log4j.appender.appender1=org.apache.log4j.ConsoleAppender
#\u6837\u5F0F\u4E3ATTCCLayout
log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
控制台结果
[main] INFO Client - 1234567890
无日志文件
2、log4j.properties文件如下
log4j.rootLogger=info, ServerDailyRollingFile, stdout
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ServerDailyRollingFile.DatePattern=‘.‘yyyy-MM-dd
log4j.appender.ServerDailyRollingFile.File=logs/notify-subscription.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
控制台结果
2016-09-20 20:31:37 INFO [Client] 1234567890
log文件结果
2016-09-20 20:31:37,986 - 1234567890
红色为日志路径
3、log4j.properties文件如下
log4j.rootLogger=INFO,R,Client
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=./log/server.log
log4j.appender.R.MaxFileSize=5MB
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%5p] %d [%X{userName}] [%X{remoteIp}] %m%n
log4j.logger.Client=INFO,Client
log4j.additivity.Client = false
log4j.appender.Client=org.apache.log4j.RollingFileAppender
log4j.appender.Client.File=./log/client.log
log4j.appender.Client.MaxFileSize=5MB
log4j.appender.Client.MaxBackupIndex=10
log4j.appender.Client.layout=org.apache.log4j.PatternLayout
log4j.appender.Client.layout.ConversionPattern=[%5p] %d [%X{userName}] [%X{remoteIp}] %m%n
client.log结果
[ INFO] 2016-09-20 20:38:04,794 [] [] 1234567890
可以设置日志文件大小
以上是关于log4j输出模板的主要内容,如果未能解决你的问题,请参考以下文章