• java日志管理


    1 . 概述

    1.1  日志框架实现

    • log4j是apache实现的一个开源日志组件;
    • logback同样是由log4j的作者设计完成的,拥有更好的特性,用来取代log4j的一个日志框架,是slf4j的原生实现;
    • log4j2是log4j 1.x和logback的改进版,据说采用了一些新技术(无锁异步、等等),使得日志的吞吐量、性能比log4j 1.x提高10倍,并解决了一些死锁的bug,而且配置更加简单灵活,官网地址: http://logging.apache.org/log4j/2.x/manual/configuration.html

    1.2  日志接口

      slf4j是对所有日志框架制定的一种规范、标准、接口,并不是一个框架的具体的实现,需要和具体的日志框架实现配合使用(如log4j、logback)。

      使用日志接口便于更换为其他日志框架。

      

    2. 使用log4j2

    使用maven配置log4j2,pom.xml中引入log4j-api 和 log4j-core即可。

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>tdtech.practice</groupId>
        <artifactId>achitechture</artifactId>
        <version>1.0-SNAPSHOT</version>
        <properties>
            <log4j.version>2.8.2</log4j.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>${log4j.version}</version>
            </dependency>
    
        </dependencies>
    </project>
    View Code

    java代码示例:

    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    public class LogUtil {
       //引入的是log4j自身的包
        static Logger log = LogManager.getLogger();
    
        public static void main(String[] args) {
            String param1 = "string1";
            String param2 = "string2";
    
            log.error("--占位符的方式拼接效率更高-- {} -- {}", param1, param2);
        }
    }
    
    output:
    00:10:12.063 [main] ERROR eagle.log.LogUtil - --占位符的方式拼接效率更高-- string1 -- string2
    View Code

     3. 使用slf4j+log4j2接口

    pom.xml中除了引入上述log4j2的包,还需引入slf4j的包:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>tdtech.practice</groupId>
        <artifactId>achitechture</artifactId>
        <version>1.0-SNAPSHOT</version>
        <properties>
            <slf4j.version>1.7.25</slf4j.version>
            <log4j.version>2.8.2</log4j.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>${log4j.version}</version>
            </dependency>
    
            <!-- slf4j核心包-->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${slf4j.version}</version>
                <scope>runtime</scope>
            </dependency>
    
            <!--slf4j绑定log4j实现-->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j-impl</artifactId>
                <version>${log4j.version}</version>
            </dependency>
    
        </dependencies>
    </project>
    View Code

     java代码示例:

    package eagle.log;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class LogUtil {
        //引入的是slf4j接口的包
        static Logger log = LoggerFactory.getLogger(LogUtil.class);
    
        public static void main(String[] args) {
            String param1 = "string1";
            String param2 = "string2";
    
            log.error("--占位符的方式拼接效率更高-- {} -- {}", param1, param2);
        }
    }
    View Code

    4. log4j2的配置

    classpath下添加log4j2.xml,示例:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="INFO" monitorInterval="300">
        <Appenders>
            <Console name="console" target="SYSTEM_OUT">
                <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%t] [%file:%line] - %msg%n"/>
            </Console>
    
            <RollingRandomAccessFile name="gatekeeperFile"
                                     fileName="${bundle:gatekeeper:LOG_HOME}/gatekeeper.log"
                                     filePattern="${bundle:gatekeeper:LOG_HOME}/$${date:yyyy-MM-dd}/gatekeeper-%d{yyyy-MM-dd HH-mm}-%i.log.xz"
                                     immediateFlush="false">
                <PatternLayout
                        pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%t] [%file:%line] - %msg%n"/>
                <Policies>
                    <TimeBasedTriggeringPolicy interval="60"/>
                    <SizeBasedTriggeringPolicy size="20 MB"/>
                </Policies>
                <DefaultRolloverStrategy max="10">
                    <!--
                    Nested conditions: the inner condition is only evaluated on files
                    for which the outer conditions are true.
                    -->
                    <Delete basePath="${bundle:gatekeeper:LOG_HOME}" maxDepth="2">
                        <IfFileName glob="*/gatekeeper-*.log.xz">
                            <IfLastModified age="30d">
                            </IfLastModified>
                        </IfFileName>
                    </Delete>
                </DefaultRolloverStrategy>
            </RollingRandomAccessFile>
        </Appenders>
    
    
        <Loggers>
            <AsyncLogger name="org.springframework" level="info" additivity="false" includeLocation="true">
                <AppenderRef ref="springFile"/>
            </AsyncLogger>
            <AsyncLogger name="com.tdtech.eplatform.gatekeeper" level="info" additivity="false" includeLocation="true">
                <AppenderRef ref="gatekeeperFile"/>
            </AsyncLogger>
            <AsyncRoot level="info" includeLocation="false">
                <AppenderRef ref="console"/>
            </AsyncRoot>
      </Loggers>
    </Configuration>
    View Code

    https://blog.csdn.net/NEWCIH/article/details/76216606

  • 相关阅读:
    layer子层给父层页面元素赋值,以达到向父层页面传值的效果
    根据HttpServletRequest获取用户真实IP地址
    KVM--安装及初步使用
    eslint 踩坑 -- error '***' is assigned a value but never used no-unused-vars
    eslint 效验规则
    leetcode407接雨水II
    leetcodej剑指offer41.数据流中的中位数
    leetcode44通配符匹配
    leetcode955K连续位的最小反转次数
    leetcode638大礼包
  • 原文地址:https://www.cnblogs.com/eaglediao/p/6914091.html
Copyright © 2020-2023  润新知