一. LogBack配置
-
配置pom.xml
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.13</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.9</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.0.9</version> </dependency>
-
加入logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="all" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>logs/haha.log</file> <!--maxHistory或者maxFileSize或者系统重启都会触发归档--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>logs/haha.log_%d{yyyy-MM-dd}_%i.log.zip</fileNamePattern><!--归档文件名,%i是索引,从0开始递增--> <maxHistory>5</maxHistory><!--保留5天的日志--> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>100</maxFileSize><!--单个文件最大100m,超过此大小就归档,然后重新开一个文件--> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%d [%thread] %level %logger(line:%line) - %msg%n</pattern> </encoder> </appender> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d [%thread] %level %logger(line:%line) - %msg%n</pattern> </encoder> </appender> <logger name="com" level="DEBUG" additivity="false"><!--此处指定com包下的log级别,additivity="false"指定自己的appender--> <appender-ref ref="STDOUT" /> </logger> <root level="DEBUG"> <!--root的level:代码中log的级别大于此处的level时,才触发appender(全局设置)--> <appender-ref ref="STDOUT" /> </root> </configuration>
-
用slf4j调用日志打印组件
(1) slf4j-api这个包调用默认logback的class进行日志打印
(2)slf4j的LoggerFactory.getlogger(),会生成LogBack的LoggerContext,进行日志打印 -
更改logback.xml读取位置
logback默认读取classpath下的logback.xml, 改变文件位置, 在项目启动时执行配置public class LogbackLocation implements InitializingBean { private Logger logger = LoggerFactory.getLogger("LogbackLocation"); public void afterPropertiesSet() throws Exception { new LogBackLocationUtil("etc\logback.xml").configLogger(); logger.info("Logger back file success"); } }
public class LogBackLocationUtil { private String fileLocation; private LoggerContext context; public LogBackLocationUtil(String fileLocation) { this.fileLocation = fileLocation; } public void configLogger( ){ this.context = (LoggerContext) LoggerFactory.getILoggerFactory(); JoranConfigurator joranConfigurator = new JoranConfigurator(); joranConfigurator.setContext(this.context); InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(this.fileLocation); try { if(resourceAsStream != null) joranConfigurator.doConfigure(resourceAsStream); System.out.println("loggerback config file success !!!"); } catch (JoranException e) { e.printStackTrace(); } } public void closeContext(){ if(this.context != null) this.context.stop(); System.out.println("logback context closed success !!!"); } }
二. 配置p6spy
-
添加pom支持
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.18</version> </dependency> <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>1.3</version> </dependency>
-
classpath下加入spy.properties
// p6spy是sqldriver的一种代理,用来获取发往数据库的真是sql,此处要配置真正的数据库代理 realdriver=com.mysql.jdbc.Driver appender=com.mylogger.WithoutResultsetLogger // 自定义sql输出格式(不打印结果集)
-
打印格式类
该类编写参考spy.properties中默认的appender类 (#appender=com.p6spy.engine.logging.appender.Log4jLogger)public class WithoutResultsetLogger extends FormattedLogger implements P6Logger { // protected Level level; protected String lastEntry; private static Logger logger = LoggerFactory.getLogger(WithoutResultsetLogger.class); public WithoutResultsetLogger() { P6SpyProperties properties = new P6SpyProperties(); } public void logException(Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); this.logText(sw.toString()); } public void logText(String text) { // 自定义日志输出格式 if(!text.contains("resultset")) logger.debug(text.substring(text.lastIndexOf("|")+1));//(text.substring(text.lastIndexOf("|")+1)); this.setLastEntry(text); } }