Log4j Learning Diary-writing to a database

Source: Internet
Author: User

1. Create the Log database first
It's MySQL.
CREATE TABLE ' Td_log ' (
' Lid ' int (one) not NULL auto_increment,
' lusername ' varchar DEFAULT NULL,
' Lmessage ' varchar ($) DEFAULT NULL,
' lstartdate ' varchar (+) DEFAULT NULL,
' Level ' varchar DEFAULT NULL,
PRIMARY KEY (' lid ')
)

2, the configuration file part of the content:
Log4j.rootlogger=info,jdbc
#直接使用log4j包中的文件
Log4j.appender.jdbc=org.apache.log4j.jdbc.jdbcappender
#与数据库建立连接
Log4j.appender.jdbc.url=jdbc:mysql://localhost:3306/test
Log4j.appender.jdbc.driver=com.mysql.jdbc.driver
Log4j.appender.jdbc.user=root
Log4j.appender.jdbc.password=admin
Log4j.appender.jdbc.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss}%5p%t%c{2}:%l-%m%n

#指定要插入数据库的格式, which corresponds to the database field, does not differ from the general SQL, but adds some configuration parameters to the log4j.
Log4j.appender.jdbc.sql=insert to Td_log (Lusername, lstartdate, level, lmessage) VALUES ('%x{lusername} ', '%d{ Yyyy-mm-dd HH:mm:ss} ', '%p ', '%m ')

3, the Log tool class source code is as follows
/**
* System Log Helper Class
* @author
* @date * *
public class logutil{

/**
* Crash level
* @param lusername
* @param message
*/
public static void Fatal (Logger Logger, String lusername,string message) {
Mdc.put ("Lusername", lusername);
Logger.fatal (message);
}

/**
* ERROR Level
* @param username
* @param message
*/
public static void Error (Logger Logger, String lusername,string message) {
Mdc.put ("Lusername", lusername);
Logger.error (message);
}

/**
* ERROR Level
* @param username
* @param message
*/
public static void Error (Logger Logger, String lusername, Object message) {
Mdc.put ("Lusername", lusername);
Logger.error (message);
}

/**
* Message Level
* @param Logger
* @param username
* @param message
*/
public static void info (Logger Logger, String lusername,string message) {
Mdc.put ("Lusername", lusername);
Logger.info (message);
}

/**
* Warning Level
* @param username
* @param message
*/
public static void Warn (Logger Logger, String lusername,string message) {
Mdc.put ("Lusername", lusername);
Logger.warn (message);
}

/**
* Debug Level
* @param username
* @param message
*/
public static void Debug (Logger Logger, String lusername,string message) {
Mdc.put ("Lusername", lusername);
Logger.debug (message);
}
}

4, the client calls the source code
public class test{
private static Logger Logger = Logger.getlogger (Test.class);

public static void Main (string[] args) {
Logutil.info (Logger, "wyw", "see if the database is written");

}
}

Log4j Learning Diary-writing to a database

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.