log4j配置与使用(记录到数据库中)

1:action中引用

LoggerInfoLogger=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=INSERTINTOLogInfo(logTime,logLevel,logClass,logMsg)VALUES('%d{yyyy-MM-ddHH: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=INSERTINTOSYS_Log(createTime,message)VALUES('%d{yyyy-MM-ddHH: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中配置启动加载

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="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>

相关推荐