zoukankan      html  css  js  c++  java
  • spring boot整合log4j2

    POM配置

    <?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>com.roncoo.education</groupId>
        <artifactId>spring-boot-demo-05-2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>spring-boot-demo-05-2</name>
        <description>日志配置-log4j2</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.0.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <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>
            <!-- 使用log4j2 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
    
    
            <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>42.1.4</version>
            </dependency>
    
            <dependency>
                <groupId>com.oracle</groupId>
                <artifactId>ojdbc6</artifactId>
                <version>11.2.0</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>

    log4j2配置

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <properties>
            <!-- 文件输出格式 -->
            <property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level
                [%thread] %c [%L] -| %msg%n</property>
        </properties>
    
        <appenders>
            <Console name="CONSOLE" target="system_out">
                <PatternLayout pattern="${PATTERN}" />
            </Console>
    
            <File name="log" fileName="log/test.log" append="false">
                <PatternLayout
                    pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" />
            </File>
    
    
            <!-- 添加过滤器ThresholdFilter,可以有选择的输出某个级别以上的类别 onMatch="ACCEPT" onMismatch="DENY"意思是匹配就接受,否则直接拒绝 -->
            <File name="ERROR" fileName="logs/error.log">
                <ThresholdFilter level="error" onMatch="ACCEPT"
                    onMismatch="DENY" />
                <PatternLayout
                    pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n" />
            </File>
    
            <!-- 这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 -->
            <RollingFile name="RollingFile" fileName="logs/web.log"
                filePattern="logs/$${date:yyyy-MM}/web-%d{MM-dd-yyyy}-%i.log.gz">
                <PatternLayout
                    pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n" />
                <SizeBasedTriggeringPolicy size="1K" />
            </RollingFile>
            
            <!-- 配置日志写入postgresql -->
            <JDBC name="databaseAppender" tableName="logger_info">
                <ConnectionFactory class="com.roncoo.education.database.ConnectionFactory" method="getDatabaseConnection"/>
                <Column name="thread_name" pattern="%t"/>
                <Column name="class_name" pattern="%C"/>
                <Column name="method_name" pattern="%method"/>
                <Column name="begin_time" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS}"/>
                <Column name="logger_level" pattern="%level"/>
                <Column name="logger_name" pattern="%c"/>
                <Column name="logger_message" pattern="%m"/>
                <Column name="logger_throwable" pattern="%throwable{3}"/>
                <Column name="create_time" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS}"/>
            </JDBC>
            
            <!-- 异步写入数据库 -->
            <Async name="AsyncDatabaseAppender">
                <appender-ref ref="databaseAppender"/>
            </Async>
            
        </appenders>
    
        <loggers>
            <logger name="com.roncoo.education" level="debug" />
            <root level="info">
                <appenderref ref="CONSOLE" />
                <appenderref ref="log" />
                <appenderref ref="ERROR" />
                <appenderref ref="RollingFile" />
                <appenderref ref="databaseAppender" />
            </root>
        </loggers>
    
    </configuration>

    注意点

    暂不支持postgresql,所以使用oracle替代

  • 相关阅读:
    Docker 网络
    Dockerfile你值得拥有
    Spring boot配置成功后,出现Whitelabel Error Page的解决办法
    ubuntu 创建快捷方式
    Ubuntu Make来安装Visual Studio Code
    ubuntu16.04开机自动挂载Windows分区
    ubuntu下使用罗技Unifying
    酷开电视升级后无法启动
    ArcGIS 10.2安装错误,Error: Internal Error 2753. The File is not marked for installation
    ubuntu16.04下使用wine 运行微信开发小程序
  • 原文地址:https://www.cnblogs.com/lilei2blog/p/8602274.html
Copyright © 2011-2022 走看看