# logback的介绍 >Logback是由log4j创始人设计的又一个开源日志组件。logback当前分成三个模块:logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块。logback-classic是log4j的一个 改良版本。此外logback-classic完整实现[SLF4J API](http://www.oschina.net/p/slf4j)使你可以很方便地更换成其它日志系统如log4j或JDK14 Logging。logback-access访问模块与Servlet容器集成提供通过Http来访问日志的功能。 Logback是要与SLF4J结合起来用,spring boot使用Logback作为日志实现的框架 ## 打印方法与基本的选择规则 ![](https://box.kancloud.cn/e0ac43c3521a94a427131e232ca9439c_492x173.png) 打印方法决定记录请求的级别。例如,如果 L 是一个 logger 实例,那么,语句 L.info("..")是一条级别为 INFO 的记录语句。记录请求的级别在高于或等于其 logger 的有效级别时被称为被启用,否则,称为被禁用。记录请求级别为 p,其logger的有效级别为 q,只有则当 p>=q时,该请求才会被执行 级别排序为: TRACE < DEBUG < INFO < WARN < ERROR ## logback 日志标准格式定义 ### logback-spring.xml配置 ```` <?xml version="1.0" encoding="UTF-8"?> <configuration> <contextName>${APP_NAME}</contextName> <springProperty name="APP_NAME" scope="context" source="spring.application.name"/> <springProperty name="LOG_FILE" scope="context" source="logging.file" defaultValue="../logs/${APP_NAME}"/> <springProperty name="LOG_POINT_FILE" scope="context" source="logging.file" defaultValue="../logs/point"/> <springProperty name="LOG_MAXFILESIZE" scope="context" source="logback.filesize" defaultValue="50MB"/> <springProperty name="LOG_FILEMAXDAY" scope="context" source="logback.filemaxday" defaultValue="7"/> <springProperty name="ServerIP" scope="context" source="spring.cloud.client.ip-address" defaultValue="0.0.0.0"/> <springProperty name="ServerPort" scope="context" source="server.port" defaultValue="0000"/> <!-- 彩色日志 --> <!-- 彩色日志依赖的渲染类 --> <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" /> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /> <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" /> <!-- 彩色日志格式 --> <property name="CONSOLE_LOG_PATTERN" value="[${APP_NAME}:${ServerIP}:${ServerPort}] %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%level){blue} %clr(${PID}){magenta} %clr([%thread]){orange} %clr(%logger){cyan} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" /> <property name="CONSOLE_LOG_PATTERN_NO_COLOR" value="[${APP_NAME}-${ServerIP}-${ServerPort}] %d{yyyy-MM-dd HH:mm:ss.SSS} %level ${PID} [%thread] %logger %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" /> <!-- 控制台日志 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <withJansi>true</withJansi> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- 按照每天生成常规日志文件 --> <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_FILE}/${APP_NAME}-error.log</file> <!-- 基于时间的分包策略 --> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_FILE}/${APP_NAME}-error.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxFileSize>100MB</maxFileSize> <!--保留时间,单位:天--> <maxHistory>60</maxHistory> </rollingPolicy> <encoder> <pattern>${CONSOLE_LOG_PATTERN_NO_COLOR}</pattern> <charset>UTF-8</charset> </encoder> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>100MB</MaxFileSize> </triggeringPolicy> <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 --> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- 按照每天生成常规日志文件 --> <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_FILE}/${APP_NAME}-info.log</file> <!-- 基于时间的分包策略 --> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_FILE}/${APP_NAME}-info.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxFileSize>100MB</maxFileSize> <!--保留时间,单位:天--> <maxHistory>60</maxHistory> </rollingPolicy> <encoder> <pattern>${CONSOLE_LOG_PATTERN_NO_COLOR}</pattern> <charset>UTF-8</charset> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- <appender name="point_log" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_POINT_FILE}/point.log</file> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss}|${APP_NAME}|%msg%n</pattern> <charset>UTF-8</charset> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_POINT_FILE}/point.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxHistory>${LOG_FILEMAXDAY}</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${LOG_MAXFILESIZE}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> </filter> </appender> <appender name="point_log_async" class="ch.qos.logback.classic.AsyncAppender"> <discardingThreshold>0</discardingThreshold> <appender-ref ref="point_log"/> </appender> <logger name="com.open.capacity.log.monitor" level="info" addtivity="false"> <appender-ref ref="point_log_async" /> </logger> --> <root level="INFO"> <appender-ref ref="STDOUT"/> <appender-ref ref="ERROR"/> <appender-ref ref="INFO"/> </root> </configuration> `````````` 如何使用 ![](https://box.kancloud.cn/c9a74b959bb363a00eda5a1e58605e2a_1758x738.png)