Use log4j to save logs to the database

Source: Internet
Author: User

 

This function of log4j is good and practical, and needs to be used recently. It has never been used before. You can search for it online, and there are still many such functions.ArticleLater, the company found the training materials for developers some time ago. Now, I will record these materials for the emergency!

 

 

1. Create a log table

 

Create Table [DBO]. [loginfo] (
[Guid] [int] identity (1, 1) not null, -- sequential number
[Date] [datetime] Null, -- Time
[Thread] [varchar] (200) Collate chinese_prc_ci_as null, -- current thread
[Level] [varchar] (200) Collate chinese_prc_ci_as null, -- current level

[Class] [varchar] (200) Collate chinese_prc_ci_as null, -- current JavaProgram/Method

[Messages] [varchar] (2000) Collate chinese_prc_ci_as null --- current output information
)

 

2. Configure log4j. Properties

 

Log4j. rootlogger = debug, console, database

Log4j.addivity.org. Apache = true 

# JDBC appender 

Log4j. appender. database. Threshold = info

Log4j. appender. Database = org. Apache. log4j. JDBC. jdbcappender

# Log4j. appender. database. buffersize = 10

Log4j. appender. database. url = JDBC: Microsoft: sqlserver: // localhost; databasename = loginfotest

Log4j. appender. database. Driver = com. Microsoft. JDBC. sqlserver. sqlserverdriver

Log4j. appender. database. User = sa

Log4j. appender. database. Password = 1234

Log4j. appender. database. SQL = insert into you_log_tableValues ('% d {yyyy-mm-dd hh: mm: SS}', '% t',' % P', '% l',' % m ')

Log4j. appender. database. layout = org. Apache. log4j. patternlayout

 

 

% D % T % P % L % m % N description:

 

1)% DThe date or time of the log output time point,

2)% TThe thread name that generates the log event.;

3) % P log log_level , for example, debug , warn or info ;

4) % C the category to which the output belongs, usually the full name of the class , for example, " iNotes. default ";

5) % m log content;

6) % L location where the log event is generated, including the category name and thread, and the number of rows in Code .

7) % n outputs a carriage return line break, Windows the platform is" ", UNIX the platform is" "

 

Program code: omitted (believe in your own capabilities, these should be able to be written)

 

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.