将log4j的日志写入到mysql

将log4j的日志写入到mysql的配置信息,log4j.properties

log4j.rootLogger=INFO,database,stdout
  
### 把日志信息输出到控制台 ###  
#日志信息将写到控制台  
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#信息打印到System.out上   
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#指定输出格式:显示日期和log信息  
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}  %m%n
  
### 把日志信息写入到数据库###  
log4j.appender.database=org.apache.log4j.ConsoleAppender
log4j.appender.database.layout=org.apache.log4j.PatternLayout
log4j.appender.database.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS}[%p]: %m%n
  
log4j.appender.database=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.database.driver=com.mysql.jdbc.Driver
log4j.appender.database.URL=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
log4j.appender.database.user=root
log4j.appender.database.password=admin
log4j.appender.database.sql=insert into log (class,method,createtime,loglevel,msg) VALUES ('%C','%M','%d{yyyy-MM-dd hh:mm:ss}','%p','%m')
log4j.appender.database.layout=org.apache.log4j.PatternLayout
建表语句:

CREATE TABLE `log4_log` (
  `class` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `method` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `createtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `loglevel` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `msg` varchar(500) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

你可能感兴趣的:(OpsDev,OpsDev运维开发)