首页 > 代码库 > log4j输出模板

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\u5F0Ferrorlog4j.rootLogger=debug,appender1#log4j.rootLogger=info,appender1#log4j.rootLogger=error,appender1#\u8F93\u51FA\u5230\u63A7\u5236\u53F0log4j.appender.appender1=org.apache.log4j.ConsoleAppender#\u6837\u5F0F\u4E3ATTCCLayoutlog4j.appender.appender1.layout=org.apache.log4j.TTCCLayout

控制台结果

[main] INFO Client - 1234567890

无日志文件

2、log4j.properties文件如下

log4j.rootLogger=info, ServerDailyRollingFile, stdoutlog4j.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=truelog4j.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,Clientlog4j.appender.R=org.apache.log4j.RollingFileAppenderlog4j.appender.R.File=./log/server.loglog4j.appender.R.MaxFileSize=5MBlog4j.appender.R.MaxBackupIndex=10log4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=[%5p] %d [%X{userName}] [%X{remoteIp}] %m%nlog4j.logger.Client=INFO,Clientlog4j.additivity.Client = falselog4j.appender.Client=org.apache.log4j.RollingFileAppenderlog4j.appender.Client.File=./log/client.loglog4j.appender.Client.MaxFileSize=5MBlog4j.appender.Client.MaxBackupIndex=10log4j.appender.Client.layout=org.apache.log4j.PatternLayoutlog4j.appender.Client.layout.ConversionPattern=[%5p] %d [%X{userName}] [%X{remoteIp}] %m%n
client.log结果
[ INFO] 2016-09-20 20:38:04,794 [] [] 1234567890

 可以设置日志文件大小

 

log4j输出模板