标签:style blog http java color 使用
之前做的一个项目有这么个要求,在日志管理系统里,需要将某些日志信息存储到数据库里,供用户、管理员查看分析。因此我就花了点时间搞了一下这一功能,各位请看。
1
2
3
4
5
6
7
8
9
|
public class Log { private Long id; private String logNum; private String userId; private Calendar time; private int type; private String content; ... } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class DBAppender extends AppenderSkeleton { private LogService logBLL = new LogService(); @Override protected void append(LoggingEvent arg0) { if (!arg0.getLoggerName().startsWith(Constants.ProjetNS)) return ; Log log = new Log(); log.setType(arg0.getLevel().toInt()); log.setTime(Calendar.getInstance()); log.setUserId( "system" ); log.setContent(arg0.getRenderedMessage()); logBll.add(log); } } |
改新log4j.properties配置文件,类似如下所示。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# Root logger option log4j.rootLogger=WARN, stdout, file, db # Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.appender.file = org.apache.log4j.DailyRollingFileAppender log4j.appender.file.File = logs/log.log log4j.appender.file.Append = true log4j.appender.file.Threshold = ERROR log4j.appender.file.layout = org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n log4j.appender.db = com.aitanjupt.angel.log.DBAppender |
标签:style blog http java color 使用
原文地址:http://www.cnblogs.com/mingf123/p/3845030.html