在spring boot中没有使用log4j2生成日志文件

idv4meu8  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(632)

我使用log4j2来记录spring引导,但它没有创建日志文件。下面是我对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");
    }
}

启动应用程序后,在控制台中获取登录文件,但文件未生成。

bpzcxfmw

bpzcxfmw1#

请使用下面的配置文件

<?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>
o7jaxewo

o7jaxewo2#

日志文件名 fileName: ${log-path}/logfile.log has log path引用了一个属性,但是没有定义该属性
您需要在log4j文件中添加属性

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

v6ylcynt3#

为了支持yaml格式,需要额外的依赖项。添加这些依赖项之后,它就可以正常工作了。

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

相关问题