日期:2014-05-16  浏览次数:20518 次

log4j配置与使用(记录到数据库中)
1: action中引用
Logger InfoLogger = Logger.getLogger("loginfo");
InfoLogger.info("查询列表:" + e);


2:配置(log4j.properties)

log4j.logger.loginfo=INFO,DEBUG,ERROR,CONSOLE,DATABASE
log4j.logger.opinfo=INFO,DB
log4j.rootLogger=ERROR
log4j.appender.A1.layout.ConversionPattern=%-4r [%-5p] [%t] %l - %m%n
log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL=jdbc:jtds:sqlserver://10.0.0.0:1433/test
log4j.appender.DATABASE.driver=net.sourceforge.jtds.jdbc.Driver
log4j.appender.DATABASE.user=asia
log4j.appender.DATABASE.password=asia511
log4j.appender.DATABASE.sql=INSERT INTO LogInfo(logTime,logLevel,logClass,logMsg) VALUES ('%d{yyyy-MM-dd HH:mm:ss}', '%p', '%l', '%m')
log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DB.URL=jdbc:jtds:sqlserver://10.0.0.0:1433/test
log4j.appender.DB.driver=net.sourceforge.jtds.jdbc.Driver
log4j.appender.DB.user=asia
log4j.appender.DB.password=asia511
log4j.appender.DB.sql=INSERT INTO SYS_Log(createTime,message) VALUES ('%d{yyyy-MM-dd HH:mm:ss}', '%m')
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
log4j.logger.org.hibernate=debug
log4j.logger.org.hibernate.type=debug
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.A1=org.apache.log4j.ConsoleAppender


3:web.xml中配置启动加载
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- spring的上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>