Write logs to the database

Source: Internet
Author: User
Tags log log log4j

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.
123456789 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.
12345678910111213141516 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.

1234567891011121314151617 # Root logger optionlog4j.rootLogger=WARN, stdout, file, db# Direct log messages to stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%nlog4j.appender.file = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.file.File = logs/log.loglog4j.appender.file.Append = truelog4j.appender.file.Threshold = ERRORlog4j.appender.file.layout = org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%nlog4j.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. </span

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.