日志配置ogback slf4j
日志配置
有日志然后可以打印一切(包括sql,异常,普通信息) logback取代log4j的理由
logback可以在里面切换log4j,slf4j内含log4j(默认就是这个),集成了slf4j之后就用slf4j(根据你加入的jar包自动识别切换)
logback日志配置:
1,下载jar
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
</dependency>
2,web.xml配置
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>classpath:config_log/logback.xml</param-value>
</context-param>
<listener>
<listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
</listener>
3,logback.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true">
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!-- <pattern>%.-1level|%-40.40logger{0}|%msg%n</pattern> -->
<!--<pattern>%d{yyyy/MM/dd-HH:mm:ss} %-5level [%thread] %class{5}:%line>>%msg%n</pattern> -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/esteelds/esteelLogFile_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>100</maxIndex>
<maxHistory>30</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<!--日志文件最大的大小-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.logicalcobwebs" level="ERROR" />
<logger name="com.esteel" level="INFO" />
<logger name="org.springframework.web" level="INFO" />
<logger name="org.springframework.jdbc" level="INFO" />
<logger name="org.mybatis" level="INFO" />
<logger name="org.springframework.beans" level="INFO" />
<logger name="com.esteel.mybatis" level="INFO" />
<!-- <logger name="org.apache.ibatis" level="DEBUG">///////////////////////mysql打印日志
<appender-ref ref="STDOUT"/>
</logger>
<logger name="java.sql" level="debug">
<appender-ref ref="STDOUT"/>
</logger> -->
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
4,应用:
(1)打印sql
logback.xml加:
<logger name="org.apache.ibatis" level="DEBUG">///////////////////////mysql打印日志
<appender-ref ref="STDOUT"/>
</logger>
<logger name="java.sql" level="debug">
<appender-ref ref="STDOUT"/>
</logger>
mybatis-config.xml加
<setting name="logImpl" value="STDOUT_LOGGING"/>///其实这个即可
(2)在Java中硬编码打印
package com.stu.system.action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BlogAction{
//定义一个全局的记录器,通过LoggerFactory获取
private final static Logger logger = LoggerFactory.getLogger(BlogAction.class);
/**
* @param args
*/
public static void main(String[] args) {
logger.info("logback 成功了");
logger.error("logback 成功了");
}
}
参考:
http://blog.csdn.net/conquer0715/article/details/9365899
http://www.jb51.net/article/71692.htm
http://www.myexception.cn/sql/1735534.html
http://superich2008.iteye.com/blog/2063349
上一篇: ArrayList 迭代器模式
下一篇: slf4j使用 日志