spring中配置log4j,并将log信息存储在数据库中(以mysql为例)

来源:互联网 发布:2017怎样养淘宝小号 编辑:程序博客网 时间:2024/04/29 09:04

log4j作用及重要性我就不再重复了,可google一下。

第一例:log4j与mysql的配置

需要的jar为:log4j-1.2.16.jar

                         mysql-connector-java-5.1.22.jar

log4j配置文件:log4j.properties

--------------------------------------------------------------------------------------------------------------------------------------

log4j.rootLogger=INFO,appender1,appender2,appender3


log4j.logger.org.springframework=ERROR


log4j.appender.appender1=org.apache.log4j.ConsoleAppender 
log4j.appender.appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.appender1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS}[%p]: %m%n


log4j.appender.appender2=org.apache.log4j.FileAppender
log4j.appender.appender2.File=log/IceColdMonitor.log
log4j.appender.appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.appender2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS}[%p]: %m%n
#database
log4j.appender.appender3=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.appender3.driver=com.mysql.jdbc.Driver
log4j.appender.appender3.URL=jdbc:mysql://127.0.0.1:3306/test
log4j.appender.appender3.user=test
log4j.appender.appender3.password=test
log4j.appender.appender3.sql=INSERT INTO logging (log_date, log_level, location, message) VALUES ('%d{yyyy-MM-dd hh:mm:ss}','%p','%c','%m')


---------------------------------------------------------------------------------------------------------------------------------------------


测试类:Test

------------------------------------------------------------------------------------

package com.log4j.test;


import org.apache.log4j.Logger;


public class Test {
public static void main(String[] args) {  
Logger logger = Logger.getLogger(Test.class);  
logger.info("test logger info");  
logger.debug("test logger debug");  
logger.error("test logger error");
logger.fatal("test logger fatal");
}  
}

----------------------------------------------------------------------------------------


demo例子:

http://download.csdn.net/detail/frank0417/5321679


数据库脚本:

CREATE TABLE `logging` (
  `log_date` varchar(50) default NULL,
  `log_level` varchar(5) default NULL,
  `location` varchar(100) default NULL,
  `message` varchar(1000) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

原创粉丝点击