码迷,mamicode.com
首页 > 编程语言 > 详细

springboot log4j2

时间:2020-05-02 17:09:35      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:mod   control   rgs   lse   div   http   return   request   level   

添加maven依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>
    <artifactId>springboot-log4j2</artifactId>
    <name>springboot-log4j2</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.4.2</version>
        </dependency>
    </dependencies>
</project>

添加application.properties并添加配置

spring.profiles.active=dev
logging.config=classpath:log4j2-spring-${spring.profiles.active}.xml

添加log4j2-spring-dev.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <Properties>
        <Property name="filename">/opt/logs/demo-$${sd:type}.log</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <RollingFile name="RollingFile" fileName="/opt/logs/demo.log"
                     filePattern="/opt/logs/$${date:yyyy-MM}/demo-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
                <SizeBasedTriggeringPolicy size="250 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="14"/>
        </RollingFile>
    </Appenders>
    <loggers>
        <AsyncLogger name="com.springboot.demo.log4j2" level="INFO" additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="RollingFile"/>
        </AsyncLogger>
        <asyncRoot level="ERROR">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="RollingFile"/>
        </asyncRoot>
    </loggers>
</configuration>

添加demo程序

@SpringBootApplication
@RestController
public class SpringbootLog4jApplication {

    private static Logger logger = LoggerFactory.getLogger(SpringbootLog4jApplication.class);

    @RequestMapping("/")
    public String index() {
        logger.info("access to url /index");
        return "index";
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringbootLog4jApplication.class, args);
    }

}

springboot log4j2

标签:mod   control   rgs   lse   div   http   return   request   level   

原文地址:https://www.cnblogs.com/yytxdy/p/12818385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!