previously done a project has such a requirement, in the log management system, some log information needs to be stored in the database for users, administrators to view the analysis. So I took a little time to do this, gentlemen. Absrtact: We know that log4j can provide powerful and configurable logging functions, such as writing files, printing to the console, etc., but sometimes we need it to output the log to the back-end database, Log4j's strong scalability to support this, the following is the specific implementation. keywords: log,log4j, log, java,db, database,slf4jprerequisite: Already configured.slf4j,log4j, can write log to file or console normally. Requirements: Writing logs to the database. Description: Use Log4j-1.2.17.jar,slf4j-api-1.7.5.jar,slf4j-log4j12-1.6.6.jar. Step One:You have to be able to write to the database and write an interface that writes data to the database log table, whether it's a webservice or something, if it's a Java interface.
Log is a defined log class that uses the Logservice object to invoke the logbll. Add (log log) method to add a log message to the database.
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;
... }
|
Step Two:write a inherit fromAppenderskeleton class, and overrides its Append method. In the Append method, the Java interface defined in the previous step is called, andLogbll.add (log) writes a log message to the database.
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);
} }
|
Step Three:
Change the new log4j.properties configuration file, similar to the following.
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 |
The above file mainly increases the configuration of a log output direction, output to the database, and specify the specific processing class. Where output logs are required, normal use:Private Logger Logger = loggerfactory .GetLogger(Springservicesupport.class);Logger. Error (ex);.. Can.
From for notes (Wiz)