How to write logs to the database using log4j

Source: Internet
Author: User
Tags log log log4j

A previous project has 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 get this done, so please see.

Absrtact: We know that log4j can provide a powerful configurable logging function, write files, print to the console, and so on, but sometimes we need it to output the log to the backend database, LOG4J's powerful scalability support this, the following is the specific implementation.

Prerequisite: Has been configured well slf4j, log4j, can be normal to file or console write log.

Requirements: Writes 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, write an interface to write data to the database log table, whether it is webservice or what, here is a Java interface.

Log is a defined logging class that uses the Logservice object to invoke the Logbll.add (log log) method to add a log message to the database.

public class Log {
    private Long ID;
    Private String Lognum;
    Private String userId;
    Private Calendar time;
    private int type;
    Private String content;
    ...
}

Step Two:

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/tools/

Write a class that inherits from the Appenderskeleton class and rewrite its Append method. In the Append method, you invoke the Java interface defined in the previous step, Logbll.add (log), and write a log message to the database.

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.

# Root Logger option
Log4j.rootlogger=warn, stdout, file, DB
     
# Direct log messages to stdout
LOG4J.APPENDER.S Tdout=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 files on the main increase configuration of a log output direction, to the database output, and specified a specific processing class.

In the place where the output log is required, normal use:

Private Logger Logger = Loggerfactory.getlogger (Springservicesupport.class);

Logger.error (ex); Can.

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.