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)