日志文件为了方便统计所以就存放在了mysql里,这里只是根据官方的文档做了一个demo。
依赖这几个jar:
log4j-over-slf4j-1.7.7.jar
logback-core-1.1.2.jar
slf4j-api-1.7.7.jar
logback-classic-1.1.2.jar
mysql-connector-java-5.1.33-bin.jar
src根目录放logblock.xml
<?xml version="1.0"encoding="UTF-8"?>
<configuration>
<appendername="DB"class="ch.qos.logback.classic.db.DBAppender">
<connectionSource
class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<driverClass>com.mysql.jdbc.Driver</driverClass>
<url>jdbc:mysql://localhost:3306/log</url>
<user>user</user>
<password>123</password>
</connectionSource>
</appender>
<rootlevel="ALL">
<appender-refref="DB"/>
</root>
</configuration>
现在开始写java测试文件喽
packagecom.log;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
publicclassMainDemo{
finalstaticLogger logger = LoggerFactory.getLogger(MainDemo.class.getName());
publicstaticvoidmain(String[] args){
logger.debug("aaaaaa");
logger.info("aaaaaa");
}
}
这里只输出了debug和info
然后把下面的sql在mysql里执行
DROPTABLEIFEXISTS`logging_event`;
CREATETABLE`logging_event`(
`timestmp`LONGNOTNULL,
`formatted_message`TEXTNOTNULL,
`logger_name`VARCHAR(255)NOTNULL,
`level_string`VARCHAR(255)NOTNULL,
`thread_name`VARCHAR(255)NOTNULL,
`reference_flag`SMALLINTNOTNULL,
`caller_filename`VARCHAR(255)NOTNULL,
`arg0`VARCHAR(255),
`arg1`VARCHAR(255),
`arg2`VARCHAR(255),
`arg3`VARCHAR(255),
`caller_class`VARCHAR(255)NOTNULL,
`caller_method`VARCHAR(255)NOTNULL,
`caller_line`VARCHAR(255)NOTNULL,
`event_id`INTNOTNULLAUTO_INCREMENT,
PRIMARYKEY(event_id)
)ENGINE=InnoDBDEFAULTCHARSET=UTF8;
DROPTABLEIFEXISTS`logging_event_property`;
CREATETABLE`logging_event_property`(
`event_id`INTNOTNULL,
`mapped_key`VARCHAR(255)NOTNULL,
`mapped_value`TEXTNOTNULL
)ENGINE=InnoDBDEFAULTCHARSET=UTF8;
DROPTABLEIFEXISTS`logging_event_exception`;
CREATETABLE`logging_event_exception`(
`event_id`INTNOTNULL,
`i`SMALLINTNOTNULL,
`trace_line`VARCHAR(255)NOTNULL
)ENGINE=InnoDBDEFAULTCHARSET=UTF8;
运行java文件就ok了