当前位置: 首页 > 工具软件 > Mysql4J > 使用案例 >

MySQL之——将log4j的日志配置输出到MySQL中

贲高寒
2023-12-01

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/75040821

log4j可以支持将log输出到文件,数据库,甚至远程服务器,这次给大家分享下如何输出到mysql中。

1、配置log4j.properties文件

log4j.properties的配置内容如下:

log4j.rootLogger=ERROR,appender1,appender2

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.jdbc.JDBCAppender
log4j.appender.appender2.driver=com.mysql.jdbc.Driver
log4j.appender.appender2.URL=jdbc:mysql://localhost:3306/lyz_log4j_write_mysql?useUnicode=true&characterEncoding=UTF-8
log4j.appender.appender2.user=root
log4j.appender.appender2.password=111111
log4j.appender.appender2.sql=insert into zuidaima_log (create_time,log) VALUES ('%d{yyyy-MM-dd hh:mm:ss}', '%c %p %m %n')
log4j.appender.appender2.layout=org.apache.log4j.PatternLayout

2、创建数据数据库表

  create database `lyz_log4j_write_mysql`;  
  use `lyz_log4j_write_mysql`;  
  CREATE TABLE `zuidaima_log` (  
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,  
    `create_time` datetime NOT NULL,  
    `log` text default NULL,  
    PRIMARY KEY (`id`)  
  ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;  

 类似资料: