Spring Boot 应用系列 5 -- Spring Boot 2 整合logback
2021-06-27 08:03
标签:pack seconds 自动生成 auth 环境 ade 选择 company 来讲 上一篇我们梳理了Spring Boot 2 整合log4j2的配置过程,其中讲到了Spring Boot 2原装适配logback,并且在非异步环境下logback和log4j2的性能差别不大,所以对于那些日志量不算太高的项目来说,选择logback更简单方便。 1. pom.xml pom.xml不需要添加任何依赖。 2. logback的配置文件 系统启动时,logback按照下列顺序加载第一个找到的配置文件: (1) classpath: logback-test.xml (2) classpath: logback.groovy (3) classpath: logback.xml (4) 查询com.qos.logback.classic.spi.Configurator接口的实现 (5) 如果以上均未找到,则加载默认配置。 但是与Spring Boot集成时,上面的配置文件加载较早,Spring Boot为logback提供了很多扩展(比如 3. 配置目标 类似上一篇整合log4j2时的情况,我们希望达成以下目标: (1) 所有级别的日志均可以通过控制台打印; (2) 日志的存储目录格式为“/yyyy-MM/dd/”(“年-月/日/”),日志文件名称包含小时; (3) error级别的日志存储在“/yyyy-MM/dd/app-error-{HH}.log”中,其中HH是日志发生的小时; (4) 其他级别的日志存储在“/yyyy-MM/dd/app-other-{HH}.log”中; (5) 所有级别的日志同时以html格式存储备份; (6) 所有日志文件按照小时归档,一个小时一套文件(三个具体文件error,other); (7) 设置日志文件的size上限,如果某一小时出现的日志特别多,超过size limit之后自动生成带数字后缀的文件。 4. logback-spring.xml (1) logback的日志等级跟slf4j保持一致包括TRACE, DEBUG, INFO, WARN 和 ERROR; (2) (3) (4) 节点中的 [%d{yyyy-MM-dd HH:mm:ss.SSS}]: 时间格式; [%thread]: 输出日志的线程; %-5level: 日志级别; %logger: 日志调用者; %msg: 日志内容; %n: 换行符。 (5) 此外,prudent功能还有两个限制: a. 日志文件不能被压缩; b. 不能在 (6) 关于onMatch和onMismatch,请看logback源码: 简单来讲onMatch和onMismatch的取值都有三个分别是DENY(拒绝),NEUTRAL(进入下一个filter),ACCEPT(接受,跳过所有剩下的filter)。 (7) rollingPolicy节点配置日志文件的存档策略,本例后三个appender都是每分钟(不超过10MB)存储一个文件; 注意如果存档日志文件数量超过maxHistory值,最老的日子文件会被删除。 (8) 最后说一下 继承该接口你就可以自定义自己的layout: 然后使用你自定义的layout: 本例使用了logback自带的HTMLLayout。 5. logback不同环境不同配置 有两种实现方式: (1) 按环境将配置文件拆分为logback-production.xml和logback-dev.xml等,然后在application.properties里面配置: (2) 第二种是使用 同时也有两种方式指定使用的环境: a. 在application.properties里配置: b. 启动时指定: 6. 应用 Spring Boot 应用系列 5 -- Spring Boot 2 整合logback 标签:pack seconds 自动生成 auth 环境 ade 选择 company 来讲 原文地址:https://www.cnblogs.com/eagle6688/p/9635175.htmlxml version="1.0" encoding="UTF-8"?>
configuration debug="true">
property name="Log_Home" value="logs" />
property name="Log_Pattern" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %-5level %logger - %msg%n" />
appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
encoder>
Pattern>${Log_Pattern}Pattern>
charset>UTF-8charset>
encoder>
appender>
appender name="RollingFile_Error" class="ch.qos.logback.core.rolling.RollingFileAppender">
prudent>trueprudent>
encoder>
pattern>${Log_Pattern}pattern>
charset>UTF-8charset>
encoder>
filter class="ch.qos.logback.classic.filter.LevelFilter">
level>ERRORlevel>
onMatch>ACCEPTonMatch>
onMismatch>DENYonMismatch>
filter>
rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
FileNamePattern>${Log_Home}/%d{yyyy-MM}/%d{dd}/app-error-%d{HH-mm}.%i.logFileNamePattern>
maxFileSize>10MBmaxFileSize>
maxHistory>10080maxHistory>
rollingPolicy>
appender>
appender name="RollingFile_Other" class="ch.qos.logback.core.rolling.RollingFileAppender">
prudent>trueprudent>
encoder>
pattern>${Log_Pattern}pattern>
charset>UTF-8charset>
encoder>
filter class="ch.qos.logback.classic.filter.LevelFilter">
level>ERRORlevel>
onMatch>DENYonMatch>
onMismatch>NEUTRALonMismatch>
filter>
rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
FileNamePattern>${Log_Home}/%d{yyyy-MM}/%d{dd}/app-other-%d{HH-mm}.%i.logFileNamePattern>
maxFileSize>10MBmaxFileSize>
maxHistory>10080maxHistory>
rollingPolicy>
appender>
appender name="RollingFile_HTML" class="ch.qos.logback.core.rolling.RollingFileAppender">
prudent>trueprudent>
encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
layout class="ch.qos.logback.classic.html.HTMLLayout">
pattern>%relative%thread%mdc%level%logger%msgpattern>
layout>
encoder>
rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
FileNamePattern>${Log_Home}/%d{yyyy-MM}/%d{dd}/app-%d{HH-mm}.%i.htmlFileNamePattern>
maxFileSize>10MBmaxFileSize>
maxHistory>10080maxHistory>
rollingPolicy>
appender>
root level="info">
appender-ref ref="Console" />
appender-ref ref="RollingFile_Error" />
appender-ref ref="RollingFile_Other" />
appender-ref ref="RollingFile_HTML" />
root>
configuration>
statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
In prudent mode, FileAppender will safely write to the specified file, even in the presence of other FileAppender instances running in different JVMs, potentially running on different hosts. The default value for prudent mode is false.
Prudent mode can be used in conjunction with RollingFileAppender although some restrictions apply.
Prudent mode implies that append property is automatically set to true.
Prudent more relies on exclusive file locks. Experiments show that file locks approximately triple (x3) the cost of writing a logging event. On an "average" PC writing to a file located on a local hard disk, when prudent mode is off, it takes about 10 microseconds to write a single logging event. When prudent mode is on, it takes approximately 30 microseconds to output a single logging event. This translates to logging throughput of 100‘000 events per second when prudent mode is off and approximately 33‘000 events per second in prudent mode.
1 /**
2 * Logback: the reliable, generic, fast and flexible logging framework.
3 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4 *
5 * This program and the accompanying materials are dual-licensed under
6 * either the terms of the Eclipse Public License v1.0 as published by
7 * the Eclipse Foundation
8 *
9 * or (per the licensee‘s choosing)
10 *
11 * under the terms of the GNU Lesser General Public License version 2.1
12 * as published by the Free Software Foundation.
13 */
14 package ch.qos.logback.core.filter;
15
16 import ch.qos.logback.core.spi.FilterReply;
17
18 public abstract class AbstractMatcherFilter
1 /**
2 * Logback: the reliable, generic, fast and flexible logging framework.
3 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4 *
5 * This program and the accompanying materials are dual-licensed under
6 * either the terms of the Eclipse Public License v1.0 as published by
7 * the Eclipse Foundation
8 *
9 * or (per the licensee‘s choosing)
10 *
11 * under the terms of the GNU Lesser General Public License version 2.1
12 * as published by the Free Software Foundation.
13 */
14 package ch.qos.logback.core.spi;
15
16 /**
17 *
18 * This enum represents the possible replies that a filtering component
19 * in logback can return. It is used by implementations of both
20 * {@link ch.qos.logback.core.filter.Filter Filter} and
21 * ch.qos.logback.classic.turbo.TurboFilter abstract classes.
22 *
23 * Based on the order that the FilterReply values are declared,
24 * FilterReply.ACCEPT.compareTo(FilterReply.DENY) will return
25 * a positive value.
26 *
27 * @author Sébastien Pennec
28 */
29 public enum FilterReply {
30 DENY, NEUTRAL, ACCEPT;
31 }
public interface Layout
package chapters.layouts;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.LayoutBase;
public class MySampleLayout extends LayoutBase
configuration>
appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
layout class="chapters.layouts.MySampleLayout" />
encoder>
appender>
root level="DEBUG">
appender-ref ref="STDOUT" />
root>
configuration>
logging.config: classpath:logback-dev.xml
springProfile name="dev">
root level="DEBUG">
appender-ref ref="CONSOLE" />
root>
springProfile>
springProfile name="prod">
root level="INFO">
appender-ref ref="STDOUT" />
root>
springProfile>
spring.profiles.active: dev
spring.profiles.active: prod
java -jar xxx.jar --spring.profiles.active=dev
package devutility.test.log.logback.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class BaseController {
protected static Logger logger = LoggerFactory.getLogger(BaseController.class);
}
上一篇:SpringAOP面向切面编程
下一篇:排序算法小结:C++实现
文章标题:Spring Boot 应用系列 5 -- Spring Boot 2 整合logback
文章链接:http://soscw.com/index.php/essay/98337.html