logback(3):输出日志到数据库

参考logback官方文档,地址为https://logback.qos.ch/manual/configuration.html;
一、用IntelliJ IDEA新建maven项目;
二、导入jar包:
logback需要的jar包

    
      ch.qos.logback
      logback-core
      1.2.3
    

    
      ch.qos.logback
      logback-classic
      1.2.3
      test
    

    
        org.slf4j
        slf4j-api
       1.7.26
    

mysql需要的jar包

   
    mysql
    mysql-connector-java
    8.0.15
    

    
      com.mchange
      c3p0
      0.9.5.3
    

三、resources目录下新建logback-text.xml文件,内容如下:



    
        
            
                com.mysql.cj.jdbc.Driver
                jdbc:mysql://127.0.0.1:3306/logback
                your mysql userName
                your mysql password
            
        
    

    
        
    

四、java目录下新建java类:Foo.java,MyApp1.java

package chapters.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Foo {
      static final Logger logger = LoggerFactory.getLogger(Foo.class);
      public void doIt() {
          logger.debug("Did it again!");
     }
}

package chapters.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyApp1 {
       final static Logger logger = LoggerFactory.getLogger(MyApp1.class);
       public static void main(String[] args) {
                logger.info("Entering application.");
                Foo foo = new Foo();
                foo.doIt();
                logger.info("Exiting application.");
         }
}

五、在mysql下建库建表
mysql新建database,命名为logback,需要新建三张表,分别为logging_event, logging_event_property and logging_event_exception,这三张表的运行脚本可以在logback-classic/src/main/java/ch/qos/logback/classic/db/script文件夹下找到。

CREATE DATABASE IF NOT EXISTS logback default charset utf8 COLLATE utf8_general_ci;

BEGIN;
DROP TABLE IF EXISTS logging_event_property;
DROP TABLE IF EXISTS logging_event_exception;
DROP TABLE IF EXISTS logging_event;
COMMIT;


BEGIN;
CREATE TABLE logging_event 
  (
    timestmp         BIGINT NOT NULL,
    formatted_message  TEXT NOT NULL,
    logger_name       VARCHAR(254) NOT NULL,
    level_string      VARCHAR(254) NOT NULL,
    thread_name       VARCHAR(254),
    reference_flag    SMALLINT,
    arg0              VARCHAR(254),
    arg1              VARCHAR(254),
    arg2              VARCHAR(254),
    arg3              VARCHAR(254),
    caller_filename   VARCHAR(254) NOT NULL,
    caller_class      VARCHAR(254) NOT NULL,
    caller_method     VARCHAR(254) NOT NULL,
    caller_line       CHAR(4) NOT NULL,
    event_id          BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY
  );
COMMIT;

BEGIN;
CREATE TABLE logging_event_property
  (
    event_id	      BIGINT NOT NULL,
    mapped_key        VARCHAR(254) NOT NULL,
    mapped_value      TEXT,
    PRIMARY KEY(event_id, mapped_key),
    FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
  );
COMMIT;

BEGIN;
CREATE TABLE logging_event_exception
  (
    event_id         BIGINT NOT NULL,
    i                SMALLINT NOT NULL,
    trace_line       VARCHAR(254) NOT NULL,
    PRIMARY KEY(event_id, i),
    FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
  );
COMMIT;

五、运行MyApp1.java,控制台跟数据库表的运行结果如下
logback(3):输出日志到数据库_第1张图片在这里插入图片描述
六、运行过程报的异常
异常1:
Caused by: java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support
解决方法:mysql执行set global time_zone="+8:00";

异常2:
IDEA控制台显示结果
IDEA控制台报错mysql数据库表logging_event显示结果
logback(3):输出日志到数据库_第2张图片解决方法:src\main\resources目录下新建mchange-log.properties,文件内容如下

com.mchange.v2.log.MLog=com.mchange.v2.log.FallbackMLog
com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL=OFF

你可能感兴趣的:(logback)