【问题标题】:log file is not generating using log4j2 in spring boot在 Spring Boot 中未使用 log4j2 生成日志文件
【发布时间】:2021-04-24 14:14:13
【问题描述】:

我正在使用 Log4j2 通过 Spring Boot 进行日志记录,但它没有创建日志文件。下面给出的是我对 Log4j2 的配置和我添加的依赖项。我尝试了所有可能的解决方案。 Log4j2 配置 -

Configuration:
  name: Default
  Properties:
    Property:
      name: log-path
      value: "logs"

  Appenders:
    Console:
      name: Console_Appender
      target: SYSTEM_OUT
      PatternLayout:
        pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
    File:
      name: File_Appender
      fileName: ${log-path}/logfile.log
      PatternLayout:
        pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"

  Loggers:
    Root:
      level: debug
      AppenderRef:
        - ref: Console_Appender
    Logger:
      - name: com.example
        level: debug
        AppenderRef:
          - ref: File_Appender
            level: error

build.gradle 文件中使用的依赖项:

configurations {
    all*.exclude group:'org.springframework.boot', module: 'spring-boot-starter-logging'
    all*.exclude module: 'logback-classic'
    compileOnly {
        extendsFrom annotationProcessor
    }
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-log4j2'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'

    }

申请文件:

@SpringBootApplication
public class DemoApplication {

    private static final Logger logger = LogManager.getLogger(DemoApplication.class);

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

        logger.debug("This is a debug message");
        logger.info("This is an info message");
        logger.warn("This is a warn message");
        logger.error("This is an error message");
        logger.fatal("This is a fatal message");
    }
}

启动应用程序文件后登录控制台但未生成文件。

【问题讨论】:

    标签: spring-boot logging log4j2


    【解决方案1】:

    请使用下面的配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN" monitorInterval="30">
        <Properties>
            <Property name="basePath">{base_path}</Property>
        </Properties>
    
        <Appenders>
            <RollingFile name="fileLogger"
                fileName="${basePath}/LogFileName.log"
                filePattern="${basePath}/LogFileName_%d{yyyy-MM-dd}.log">
                <PatternLayout>
                    <pattern>%d{yyyy-MMM-dd HH:mm:ss.SSS} %5p [%50.50c] : %M - %m%n
                    </pattern>
                </PatternLayout>
                <Policies>
                    <TimeBasedTriggeringPolicy interval="1"
                        modulate="true" />
                </Policies>
            </RollingFile>
    
            <Console name="console" target="SYSTEM_OUT">
                <PatternLayout
                    pattern="%d{yyyy-MMM-dd HH:mm:ss.SSS} %5p [%50.50c] : %M - %m%n" />
            </Console>
        </Appenders>
        <Loggers>
            <Logger name="{your_package}" level="debug" additivity="true">
                <appender-ref ref="fileLogger" level="debug" />
            </Logger>
            <Logger name="org.springframework" level="info"
                additivity="true">
                <appender-ref ref="fileLogger" level="info" />
            </Logger>
            <Root level="info" additivity="false">
                <appender-ref ref="console" />
            </Root>
        </Loggers>
    </Configuration>
    
    
    

    【讨论】:

      【解决方案2】:

      您的日志文件名 fileName: ${log-path}/logfile.log 的日志路径引用了一个属性,但该属性未定义

      你需要在 log4j 文件中添加属性

       <Properties>
              <Property name="log-path">{log-path}</Property>
       </Properties>
      

      【讨论】:

      • 在配置文件第6行定义。
      • 这个应用在哪里运行,是windows还是linux?
      • 在windows上运行
      • 如果未创建日志文件夹,请在从属性读取之前尝试使用文件的绝对路径一次
      • 已经尝试在 D 盘中生成,但也无法正常工作。即使文件夹存在仍然无法正常工作
      【解决方案3】:

      要支持yaml格式,需要额外的依赖。添加这些依赖后,它工作正常。

      compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'
      compile 'com.fasterxml.jackson.core:jackson-databind'
      

      【讨论】:

        猜你喜欢
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        • 2019-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-06
        • 2022-01-22
        相关资源
        最近更新 更多