Logback是一个开源日志组件,性能比log4j号官方网站: http://logback.qos.ch。它分为下面下个模块:
- logback-core:其它两个模块的基础模块
- logback-classic:是log4j的一个改良版本,同时它完整实现了slf4j API使你可以很方便地更换成其它日志系统如log4j或JDK14 Logging
- logback-access:访问模块与Servlet容器集成提供通过Http来访问日志的功能
本文演示logback的使用、配置详解、以及与lombok插件一起使用。
在配置logback之前,请配置lombok日志打印工具,也可使用Logger工具
lombok相关配置
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>org.logback-extensionsgroupId>
<artifactId>logback-ext-springartifactId>
<version>0.1.4version>
dependency>
dependencies>
<configuration scan="true" debug="false">
<property name="path" value="../logs/demo-springmvc"/>
<property name="CONSOLE_LOG_PATTERN" value="%highlight(%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] [line%4line] %logger{50} : %msg %n)"/>
<appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [line%4line] %logger{50} : %msg%npattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${path}/info/info-%d{yyyy-MM-dd}.%i.logfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>64 MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFOlevel>
filter>
<prudent>falseprudent>
appender>
<appender name="FILE_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [line%4line] %logger{50} : %msg%npattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${path}/warn/warn-%d{yyyy-MM-dd}.%i.logfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>64 MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARNlevel>
filter>
<prudent>falseprudent>
appender>
<appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [line%4line] %logger{50} : %msg%npattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${path}/error/error-%d{yyyy-MM-dd}.%i.logfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>64 MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERRORlevel>
filter>
<prudent>falseprudent>
appender>
<appender name="FILE_DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [line%4line] %logger{50} : %msg%npattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${path}/debug/debug-%d{yyyy-MM-dd}.%i.logfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>64 MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUGlevel>
filter>
<prudent>falseprudent>
appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
${CONSOLE_LOG_PATTERN}
pattern>
<charset>UTF-8charset>
encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUGlevel>
filter>
appender>
<root name="dao" level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE_WARN"/>
<appender-ref ref="FILE_INFO"/>
<appender-ref ref="FILE_ERROR"/>
<appender-ref ref="FILE_DEBUG"/>
root>
configuration>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>demo-springmvcdisplay-name>
<servlet>
<servlet-name>mvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:xml/spring-*.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>mvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<context-param>
<param-name>logbackConfigLocationparam-name>
<param-value>classpath:logback.xmlparam-value>
context-param>
web-app>
说明一下,web.xml与之前配置的xml内容有所不同,如此改变时为了解决引入logback.xml报红色,虽运行程序无影响,老感觉程序报错
将原来文件中的头部
<web-app>
替换为现在
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
@Override
public Object select(Integer id) {
TUser user = tUserMapper.selectByPrimaryKey(id);
log.debug(JSON.toJSONString(user));
log.info(JSON.toJSONString(user));
log.warn(JSON.toJSONString(user));
log.error(JSON.toJSONString(user));
return user == null ?
RestResult.fail("查询失败") : RestResult.ok("查询成功", user);
}
日志级别设置成debug级别,可打印SQL语句
该章节logback.xml可直接复制粘贴在自己项目中,修改日志存放路径和日志级别,可直接使用。
IDEA整合SSM框架教程
写在最后
人这一生也没有多少时间可以挥霍,
踏实一点,务实一些,
做自己想做的事,
如此简单!