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=you jdbcurl
log4j.appender.DATABASE.driver=you jdbc driver
log4j.appender.DATABASE.user=
log4j.appender.DATABASE.password=
log4j.appender.DATABASE.sql=INSERT INTO YOU_LOG_TABLE VALUES ('%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”一一讲解:
l %d输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyyy-MM-dd HH:mm:ss},输出类似:2005-7-19 17:49:27,刚好适合插入SQLServer;
l %t 产生该日志事件的线程名;
l %p 日志的log_level,如DEBUG、WARN或者INFO;
l %c 输出所属的类目,通常就是所在类的全名,如“iNotes.Default”;
l %m 日志的内容;
l %l 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。如write2database.main(write2database.java:33);
l %n 输出一个回车换行符,Windows平台为“ ”,Unix平台为“ ”
程序代码:
import sun.jdbc.odbc.*;
import java.sql.*;
import org.apache.log4j.Category;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.jdbc.*;
/*
add for log4j: class Logger is the central class in the log4j package.
we can do most logging operations by Logger except configuration.
getLogger(...): retrieve a logger by name, if not then create for it.
*/
static Logger logger = Logger.getLogger
( write2database.class.getName () ) ;
/*
add for log4j: class BasicConfigurator can quickly configure the package.
print the information to console.
*/
PropertyConfigurator.configure ( "log4j2database.properties" ) ;
logger.info ( "just test" ) ;
posted on 2006-03-03 21:31
水煮三国 阅读(2184)
评论(0) 编辑 收藏 所属分类:
Log4J