将log4j的日志配置输出到mysql中

jopen的头像 jopen 1016 2015-01-19 11:38 0

 基本信息

× 1   

浏览数: 7631

分享时间: 3 年 前

3

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

(1)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/zuidaima_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)mysql建表语句

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


  • opennejack的头像 opennejack 2017-08-12 09:58 代码数:0

    好好好,!!!

  • zzxzhy的头像 zzxzhy 2017-12-21 05:57 代码数:0

    学习性很高,值的收藏

  • ossaa的头像 ossaa 2018-06-10 18:48 代码数:0

    Great write-up, I am a big believer in commenting on blogs to inform the blog writers know that they’ve added something worthwhile to the world wide web!..

    Ukrayna Kiev Taras Shevchenko Üniversitesi

您的评论: