slf4j的使用(本項(xiàng)目是用maven進(jìn)行管理的)
相關(guān)的jar包依賴(lài)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zh</groupId>
<artifactId>log4j-test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
log4j.properties
的配置文件
# 日記級(jí)別(單個(gè)級(jí)別) 文件/控制臺(tái)
log4j.rootLogger=debug, stdout,file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Rirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=test.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Log4jTest.java
public class Log4jTest {
private static final Logger logger = LoggerFactory.getLogger(Log4jTest.class);
public static void main(String[] args) {
logger.debug("debug");
logger.warn("warm");
logger.error("error");
}
}
控制臺(tái)打印的信息如下:
將日志文件寫(xiě)入數(shù)據(jù)庫(kù)
在上面項(xiàng)目的基礎(chǔ)上加上連接數(shù)據(jù)庫(kù)的jar包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
在log4j.xml
的配置文件中添加將日志文件插入數(shù)據(jù)庫(kù)的配置,同時(shí)需要將第一行配置文件改為:
# 日記級(jí)別(單個(gè)級(jí)別) 文件/控制臺(tái)
log4j.rootLogger=debug, stdout,file,datasource
#下面是配置將日志信息插入數(shù)據(jù)庫(kù),
#配置輸出目標(biāo)為數(shù)據(jù)庫(kù)(假如要將日志在控制臺(tái)輸出怎爵,配置為log4j.appender. stdout =org.apache.log4j.ConsoleAppender特石;將日志寫(xiě)入文件,配置為log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
#這樣的配置在許多地方都要有鳖链,需要可查有關(guān)資料),當(dāng)然你也可以自己擴(kuò)展org.apache.log4j.jdbc.JDBCAppender這個(gè)類(lèi)姆蘸,只需要在這里配置就可以了例如我們配置我自己擴(kuò)展的MyJDBCAppender,配置為#log4j.appender.db=com.neam.commons.MyJDBCAppender
log4j.appender.datasource=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.datasource.layout=org.apache.log4j.PatternLayout
log4j.appender.datasource.driver=com.mysql.jdbc.Driver
#定義什么級(jí)別的錯(cuò)誤將寫(xiě)入到數(shù)據(jù)庫(kù)中
log4j.appender.datasource.BufferSize=1
#設(shè)置緩存大小,就是當(dāng)有1條日志信息是才忘數(shù)據(jù)庫(kù)插一次逞敷,我設(shè)置的數(shù)據(jù)庫(kù)名和表名均為user
log4j.appender.datasource.URL=jdbc\:mysql\://localhost\:3306/user?characterEncoding\=UTF8&zeroDateTimeBehavior\=convertToNull
log4j.appender.datasource.user=root
log4j.appender.datasource.password=root
log4j.appender.datasource.sql=insert into user (class,method,create_time,log_level,log_line,msg) values ('%C','%M','%d{yyyy-MM-dd HH:mm:ss}','%p','%l','%m')