我贴一份自己的log4j的配置详情,具体还得还根据你们的实际情况来配置,不过基本很全了
# DEBUG < INFO < WARN < ERROR < FATAL
#log4j.rootLogger=WARN, stdout, file
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=
#
## Output to the File
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=../logs/my/project.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH:mm:ss}%n[%p]-[Thread: %t]-[%C.%M()]: %m%n
# Log4j提供的layout有以下几种:
#1)org.apache.log4j.HTMLLayout(以HTML表格形式布局)
#2)org.apache.log4j.PatternLayout(可以灵活地指定布局模式)
#3)org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串)
#4)org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
#5)org.apache.log4j.xml.XMLLayout(以XML形式布局)
# 将需要开启的卸载rootLogger里即可
# systemOut,logFile,logDailyFile,logRollingFile,logMail,logDB
log4j.rootLogger = ${log4j.rootLogger}
# 输出到控制台
log4j.appender.systemOut = org.apache.log4j.ConsoleAppender
# org.apache.log4j.PatternLayout(可以灵活地指定布局模式)
log4j.appender.systemOut.layout = org.apache.log4j.PatternLayout
# %n%-d{yyyy-MM-dd HH:mm:ss}%n[%p]-[Thread: %t]-[%C.%M()]: %m%n
log4j.appender.systemOut.layout.ConversionPattern = [%-5p][%-22d{yyyy-MM-dd HH:mm:ssS}]%m%n
# 指定日志消息的输出最低层次
log4j.appender.systemOut.Threshold = INFO
# TRUE:默认值是true,所有的消息都会被立即输出
log4j.appender.systemOut.ImmediateFlush = TRUE
# System.err:默认值System.out,输出到控制台(err为红色,out为黑色)
log4j.appender.systemOut.Target = System.err
#输出到文件
log4j.appender.logFile = org.apache.log4j.FileAppender
log4j.appender.logFile.layout = org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern = [%-5p][%-22d{yyyy-MM-dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.logFile.Threshold = WARN
log4j.appender.logFile.ImmediateFlush = TRUE
# FALSE:默认值true,将消息追加到指定文件中,false指将消息覆盖指定的文件内容
log4j.appender.logFile.Append = TRUE
# C:\log4j.log:指定消息输出到C:\log4j.log文件
log4j.appender.logFile.File = ../logs/my/project.log
# Encoding 编码方式
log4j.appender.logFile.Encoding = UTF-8
#按DatePattern输出到文件
log4j.appender.logDailyFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.logDailyFile.layout = org.apache.log4j.PatternLayout
log4j.appender.logDailyFile.layout.ConversionPattern = [%-5p][%-22d{yyyy-MM-dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.logDailyFile.Threshold = WARN
log4j.appender.logDailyFile.ImmediateFlush = TRUE
log4j.appender.logDailyFile.Append = TRUE
log4j.appender.logDailyFile.File = ../logs/my/project.log
# '.'yyyy-ww:每周滚动一次文件,即每周产生一个新的文件 , 还可以按用以下参数:
# '.'yyyy-MM:每月
# '.'yyyy-ww:每周
# '.'yyyy-MM-dd:每天
# '.'yyyy-MM-dd-a:每天两次
# '.'yyyy-MM-dd-HH:每小时
# '.'yyyy-MM-dd-HH-mm:每分钟
log4j.appender.logDailyFile.DatePattern = '../logs/my/sys_'yyyy-MM-dd'.log'
log4j.appender.logDailyFile.Encoding = UTF-8
#设定文件大小输出到文件
log4j.appender.logRollingFile = org.apache.log4j.RollingFileAppender
log4j.appender.logRollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.logRollingFile.layout.ConversionPattern = [%-5p][%-22d{yyyy-MM-dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.logRollingFile.Threshold = WARN
log4j.appender.logRollingFile.ImmediateFlush = TRUE
log4j.appender.logRollingFile.Append = TRUE
log4j.appender.logRollingFile.File = ../logs/my/project.log
# 100KB:后缀可以是KB,MB,GB.在日志文件到达该大小时,将会自动滚动.如:log4j.log.1
log4j.appender.logRollingFile.MaxFileSize = 20MB
# 2:指定可以产生的滚动文件的最大数
log4j.appender.logRollingFile.MaxBackupIndex = 100
log4j.appender.logRollingFile.Encoding = UTF-8
#用Email发送日志
log4j.appender.logMail = org.apache.log4j.net.SMTPAppender
log4j.appender.logMail.layout = org.apache.log4j.HTMLLayout
#log4j.appender.logMail.layout=org.apache.log4j.PatternLayout
log4j.appender.logMail.layout.LocationInfo = TRUE
log4j.appender.logMail.layout.Title = Log4j Log Messages
log4j.appender.logMail.Threshold = ERROR
log4j.appender.logMail.SMTPDebug = FALSE
log4j.appender.logMail.SMTPHost = ${logMail.SMTPHost}
log4j.appender.logMail.From = ${logMail.From}
log4j.appender.logMail.To = ${logMail.To}
log4j.appender.logMail.SMTPUsername = ${logMail.SMTPUsername}
log4j.appender.logMail.SMTPPassword = ${logMail.SMTPPassword}
log4j.appender.logMail.Subject = ${logMail.Subject}
# 日志的格式
#log4j.appender.logMail.layout.ConversionPattern=[ErrorMessage] %d - %c -%-4r [%t] %-5p %c %x - %m%n
# 缓存文件大小,日志达到512K时发送Email
#log4j.appender.logMail.BufferSize = 512K
#log4j.appender.logMail.SMTPAuth = TRUE
#将日志写入到MySQL数据库 (注意不要有空格)
log4j.appender.logDB =org.apache.log4j.jdbc.JDBCAppender
log4j.appender.logDB.layout =org.apache.log4j.PatternLayout
log4j.appender.logDB.Threshold = ERROR
log4j.appender.logDB.Driver =${logDB.Driver}
log4j.appender.logDB.URL =${logDB.URL}
log4j.appender.logDB.User =${logDB.User}
log4j.appender.logDB.Password =${logDB.Password}
log4j.appender.logDB.Sql =${logDB.Sql}
#CREATE TABLE `t_log4j` (
#`id` int(11) NOT NULL AUTO_INCREMENT ,
#`project_id` int(2) NULL DEFAULT 0 COMMENT '项目名称, 外键ID' ,
#`level` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '日志级别' ,
#`category` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '列出logger名字空间的全称,如果加上{<层数>}表示列出从最内层算起的指定层数的名字空间' ,
#`file_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '显示调用logger的源文件名' ,
#`thread_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '产生该日志事件的线程名' ,
#`line` int(10) NULL DEFAULT NULL COMMENT '显示调用logger的代码行' ,
#`all_category` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数' ,
#`message` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '显示输出消息' ,
#`add_time` timestamp NULL DEFAULT NULL COMMENT '添加时间' ,
#PRIMARY KEY (`id`)
#)
#ENGINE=InnoDB
#DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
#AUTO_INCREMENT=1
#ROW_FORMAT=COMPACT;
版权属于: 技术客
原文地址: https://www.sunjs.com/article/detail/b9286a049e8149209a7496e94630a60b.html
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。