1、log4j配置文件java
######################## # JDBC Appender ####################### log4j.appender.database.Threshold = INFO #這裏定義了數據源 log4j.appender.database=org.apache.log4j.jdbc.JDBCAppender # 這玩意就是每次緩存多少條數據而後插入數據庫,爲了演示這裏設置爲1 log4j.appender.database.BufferSize=1 log4j.appender.database.driver=com.mysql.jdbc.Driver log4j.appender.database.URL=jdbc:mysql://localhost:3306/springmvc?useUnicode=true&characterEncoding=UTF-8 log4j.appender.database.user=root log4j.appender.database.password=root log4j.appender.database.sql=insert into log (class,method,create_time,log_level,msg) values ('%C','%M','%d{yyyy-MM-dd hh:mm:ss}','%p','%m') log4j.appender.database.layout=org.apache.log4j.PatternLayout #編碼格式 log4j.appender.database.Encoding=UTF-8
2、使用的是mysql數據庫,須要導入驅動包mysql
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency>
log4j包spring
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
3、測試sql
4 com.test.LogTest addMysql 2018-09-12 05:12:37 INFO 測試日誌記錄數據庫中
ok數據庫