spring boot log4j2 配置

【传送门】:log4j官网配置文件详解

1. 排除 spring boot 自带的  spring-boot-starter-logging 依赖

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
            <exclusions>
                
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>

2. 添加spring-boot-log4j2 依赖

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
        dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.dataformatgroupId>
            <artifactId>jackson-dataformat-yamlartifactId>
            <version>2.9.7version>
        dependency>

3. log4j.xml配置示例

xml version="1.0" encoding="UTF-8"?>


<configuration status="INFO">
    
    
    <Properties>
        <Property name="pattern">[%p] %d{yyyy-MM-dd HH:mm:ss,SSS} [%t] %l : %m%nProperty>
        <Property name="logHome">logsProperty>
        <Property name="displayName">monitor-serverProperty>
    Properties>

    <Appenders>
        
        <Console name="console" target="SYSTEM_OUT" follow="true">
            <PatternLayout>
                <pattern>${pattern}pattern>
            PatternLayout>
        Console>
        
        
        <File name="error" fileName="${loghome}/${displayName}_error.log" append="false">
            
            <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout>
                <pattern>${pattern}pattern>
            PatternLayout>
        File>
        
        
        <RollingFile name="rollingFile" fileName="${logHome}/${displayName}.log" filePattern="${logHome}/${displayName}_%d{yyyy-MM-dd}.log">
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout>
                <pattern>${pattern}pattern>
            PatternLayout>
            
            <Policies>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            Policies>
        RollingFile>  
    Appenders>

    <Loggers>
        <Logger name="org.springframework" level="WARN" additivity="false">
            <AppenderRef ref="console"/>
        Logger>

        <Logger name="com.skd.server" level="info" additivity="false">
            <AppenderRef ref="console">AppenderRef>
            <AppenderRef ref="error">AppenderRef>
            <AppenderRef ref="rollingFile">AppenderRef>
        Logger>
        
        <Root level="debug">
        Root>
    Loggers>
configuration>

你可能感兴趣的:(spring boot log4j2 配置)