zoukankan      html  css  js  c++  java
  • springboot logback + log4j2日志管理

    springboot的web项目中自带了日志组件:

    我们看一下,springboot中找到日志组件。

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>

    进入starter-web:找到spring-boot-starter

        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
          <version>2.1.1.RELEASE</version>
          <scope>compile</scope>
        </dependency>

    进入spring-boot-starter 找到:

     <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
          <version>2.1.1.RELEASE</version>
          <scope>compile</scope>
        </dependency>

    进入spring-boot-starter-logging,找到:

      <dependencies>
        <dependency>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
          <version>1.2.3</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-to-slf4j</artifactId>
          <version>2.11.1</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>jul-to-slf4j</artifactId>
          <version>1.7.25</version>
          <scope>compile</scope>
        </dependency>
      </dependencies>

    所以我们看到其实它的日志底层用的是log4j slf4j 和logback这们是什么判断呢:

    slf4j是一套日志接口,就是我们用的jdbc一样,可以连接不同的日志组件,只要符合接口原则就可以。

    log4j是java最经典的日志组件了。

    log4j已经很多年了,作者不甘寂寞,所以又写了日志组件,就是logback了,logback是基于log4j修改而来,可以说是升级版。

    logback的官方地址:https://logback.qos.ch/

    官方介绍:

    Logback旨在作为流行的log4j项目的后续版本,从而恢复log4j离开的位置

    Logback的体系结构足够通用,以便在不同情况下应用。目前,logback分为三个模块:logback-core,logback-classic和logback-access。

    logback-core模块为其他两个模块奠定了基础。logback-classic模块可以被同化为log4j的显着改进版本。此外,logback-classic本身实现了SLF4J API,因此您可以在logback和其他日志框架(如log4j或java.util.logging(JUL))之间来回切换。

    logback-access模块​​与Servlet容器(如Tomcat和Jetty)集成,以提供HTTP访问日志功能。请注意,您可以在logback-core之上轻松构建自己的模块。

    在springboot中使用logback非常简单:

    直接在类中定义一个logger

    @SpringBootApplicationpublic class MedicareApplication {
    
        //定义一个全局的记录器,通过LoggerFactory获取
        private final static Logger logger = LoggerFactory.getLogger(MedicareApplication.class);
    
        public static void main(String[] args) {
    
            logger.info("项目启动成功!!!!!!!!");
    
            SpringApplication.run(MedicareApplication.class, args);
        }
    
    }

    运行项目,就可以在控制台看到日志了。开发环境下,真的是可以说做到0配置了。

    在每个类中都定义 logger的静态字段,很烦人,可以引用一个组件解决这个问题:lombok

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.6</version>
            </dependency>

    然后就可以修改成:引用注解Slf4j (还需要安装lombok插件)

    @SpringBootApplication
    @Slf4j
    public class MedicareApplication {
    public static void main(String[] args) {
    
            log.info("项目启动成功!!!!!!!!");
    
            SpringApplication.run(MedicareApplication.class, args);
        }
    
    }

    代码干净了很多 。

    有了logback, 还有更好的选择就是log4j2.

    官网地址:http://logging.apache.org/log4j/2.x/

    官方介绍:Apache Log4j 2是对Log4j的升级,它比其前身Log4j 1.x提供了重大改进,并提供了Logback中可用的许多改进,同时修复了Logback架构中的一些固有问题。

    上面的代码如果想切换到log4j2需要大的改动吗,答案是不需要,只需要改一处:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>

    排除自带的loggin,引入log4j2的starter就可以了。其它不变。

    如果我们要把日志输出到指定目录的文件,调整日志输出格式,可以添加配置文件以log4j2的配置文件为例:在resources目录下创建log4j2-spring.xml

    <Configuration status="INFO" monitorInterval="30">
        <Properties>
            <Property name="logpath">/home/logs/log/dev</Property>
        </Properties>
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" />
            </Console>
            <RollingFile name="debug" fileName="${logpath}/debug/erp_debug.log"
                         filePattern="${logpath}/debug/erp_debug_%d{yyyy-MM-dd}-%i.log">
                <Filters>
                    <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
                    <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
                </Filters>
                <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" />
                <Policies>
                    <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                    <SizeBasedTriggeringPolicy size="50 MB"/>
                </Policies>
                <DefaultRolloverStrategy max="30">
                    <Delete basePath="${logpath}/debug" maxDepth="1">
                        <IfFileName glob="erp_debug_*.log"/>
                        <IfLastModified age="15d"/>
                    </Delete>
                </DefaultRolloverStrategy>
            </RollingFile>
            <RollingFile name="info" fileName="${logpath}/info/erp_info.log"
                         filePattern="${logpath}/info/erp_info_%d{yyyy-MM-dd}-%i.log">
                <Filters>
                    <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
                    <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
                </Filters>
                <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" />
                <Policies>
                    <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                    <SizeBasedTriggeringPolicy size="50 MB"/>
                </Policies>
                <DefaultRolloverStrategy max="30">
                    <Delete basePath="${logpath}/info" maxDepth="1">
                        <IfFileName glob="erp_info_*.log"/>
                        <IfLastModified age="15d"/>
                    </Delete>
                </DefaultRolloverStrategy>
            </RollingFile>
            <RollingFile name="warn" fileName="${logpath}/warn/erp_warn.log"
                         filePattern="${logpath}/warn/erp_warn_%d{yyyy-MM-dd}-%i.log">
                <Filters>
                    <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
                    <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
                </Filters>
                <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" />
                <Policies>
                    <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                    <SizeBasedTriggeringPolicy size="50 MB"/>
                </Policies>
                <DefaultRolloverStrategy max="30">
                    <Delete basePath="${logpath}/warn" maxDepth="1">
                        <IfFileName glob="erp_warn_*.log"/>
                        <IfLastModified age="15d"/>
                    </Delete>
                </DefaultRolloverStrategy>
            </RollingFile>
            <RollingFile name="error" fileName="${logpath}/error/erp_error.log"
                         filePattern="${logpath}/error/erp_error_%d{yyyy-MM-dd}-%i.log">
                <Filters>
                    <ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
                    <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
                </Filters>
                <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n" />
                <Policies>
                    <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                    <SizeBasedTriggeringPolicy size="50 MB"/>
                </Policies>
                <DefaultRolloverStrategy max="30">
                    <Delete basePath="${logpath}/error" maxDepth="1">
                        <IfFileName glob="erp_error_*.log"/>
                        <IfLastModified age="15d"/>
                    </Delete>
                </DefaultRolloverStrategy>
            </RollingFile>
    
        </Appenders>
        <Loggers>
            <Root level="info">
                <AppenderRef ref="Console"/>
                <AppenderRef ref="debug"/>
                <AppenderRef ref="info"/>
                <AppenderRef ref="warn"/>
                <AppenderRef ref="error"/>
            </Root>
        </Loggers>
    </Configuration>

    该配置文件摘自地址:http://syllabus.lianmengtu.top/  一份史诗级配置

    重新配置一下文件地址,就可以运行,并看到自己的日志了。

     为了调试方便,可以给控制台增加彩色的输出,

    先安装插件:Grep Console

  • 相关阅读:
    协议分析 - DHCP协议解码详解
    在SqlServer2000的视图中小心使用*符号
    sql 将 varchar 值转换为数据类型为 int 的列时发生语法错误 的解决办法
    LECCO SQL Expert工具之优化sql语句
    css+js简单应用
    对任何一天是星期几算法的实现
    asp.net ajax 1.0 出现"sys"未定义解决方法
    js日历脚本
    在ASP.NET中重写URL
    在ASP.NET中重写URL (续篇)
  • 原文地址:https://www.cnblogs.com/hankuikui/p/10154659.html
Copyright © 2011-2022 走看看