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

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

我使用log4j2来记录spring引导,但它没有创建日志文件。下面是我对log4j2的配置和我添加的依赖项。我尝试了所有可能的解决办法。log4j2配置-

  1. Configuration:
  2. name: Default
  3. Properties:
  4. Property:
  5. name: log-path
  6. value: "logs"
  7. Appenders:
  8. Console:
  9. name: Console_Appender
  10. target: SYSTEM_OUT
  11. PatternLayout:
  12. pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
  13. File:
  14. name: File_Appender
  15. fileName: ${log-path}/logfile.log
  16. PatternLayout:
  17. pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
  18. Loggers:
  19. Root:
  20. level: debug
  21. AppenderRef:
  22. - ref: Console_Appender
  23. Logger:
  24. - name: com.example
  25. level: debug
  26. AppenderRef:
  27. - ref: File_Appender
  28. level: error

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

  1. configurations {
  2. all*.exclude group:'org.springframework.boot', module: 'spring-boot-starter-logging'
  3. all*.exclude module: 'logback-classic'
  4. compileOnly {
  5. extendsFrom annotationProcessor
  6. }
  7. developmentOnly
  8. runtimeClasspath {
  9. extendsFrom developmentOnly
  10. }
  11. }
  12. dependencies {
  13. compile 'org.springframework.boot:spring-boot-starter'
  14. compile 'org.springframework.boot:spring-boot-starter-web'
  15. compile 'org.springframework.boot:spring-boot-starter-log4j2'
  16. developmentOnly 'org.springframework.boot:spring-boot-devtools'
  17. }

应用程序文件:

  1. @SpringBootApplication
  2. public class DemoApplication {
  3. private static final Logger logger = LogManager.getLogger(DemoApplication.class);
  4. public static void main(String[] args) {
  5. SpringApplication.run(DemoApplication.class, args);
  6. logger.debug("This is a debug message");
  7. logger.info("This is an info message");
  8. logger.warn("This is a warn message");
  9. logger.error("This is an error message");
  10. logger.fatal("This is a fatal message");
  11. }
  12. }

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

bpzcxfmw

bpzcxfmw1#

请使用下面的配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="WARN" monitorInterval="30">
  3. <Properties>
  4. <Property name="basePath">{base_path}</Property>
  5. </Properties>
  6. <Appenders>
  7. <RollingFile name="fileLogger"
  8. fileName="${basePath}/LogFileName.log"
  9. filePattern="${basePath}/LogFileName_%d{yyyy-MM-dd}.log">
  10. <PatternLayout>
  11. <pattern>%d{yyyy-MMM-dd HH:mm:ss.SSS} %5p [%50.50c] : %M - %m%n
  12. </pattern>
  13. </PatternLayout>
  14. <Policies>
  15. <TimeBasedTriggeringPolicy interval="1"
  16. modulate="true" />
  17. </Policies>
  18. </RollingFile>
  19. <Console name="console" target="SYSTEM_OUT">
  20. <PatternLayout
  21. pattern="%d{yyyy-MMM-dd HH:mm:ss.SSS} %5p [%50.50c] : %M - %m%n" />
  22. </Console>
  23. </Appenders>
  24. <Loggers>
  25. <Logger name="{your_package}" level="debug" additivity="true">
  26. <appender-ref ref="fileLogger" level="debug" />
  27. </Logger>
  28. <Logger name="org.springframework" level="info"
  29. additivity="true">
  30. <appender-ref ref="fileLogger" level="info" />
  31. </Logger>
  32. <Root level="info" additivity="false">
  33. <appender-ref ref="console" />
  34. </Root>
  35. </Loggers>
  36. </Configuration>
展开查看全部
o7jaxewo

o7jaxewo2#

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

  1. <Properties>
  2. <Property name="log-path">{log-path}</Property>
  3. </Properties>
v6ylcynt

v6ylcynt3#

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

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

相关问题