最新消息:20210917 已从crifan.com换到crifan.org

【已解决】java中log4j2中配置日志的level不同:console是INFO而file是DEBUG

日志 crifan 983浏览 0评论
折腾:
【未解决】给Spring Boot的java项目中加上输出日志到文件中
期间,希望console中不要输出这么多日志,只需要info就够了
然后file中可以是debug
spring boot log4j2 config
Spring boot log4j2.xml example – HowToDoInJava
How to use Log4j 2 with Spring Boot | CalliCoder
SpringBoot + Log4j2使用配置 – 简书
Spring Boot 使用 Log4j2 – 田园里的蟋蟀 – 博客园
Spring Boot Log4j 2 example – Mkyong.com
https://mkyong.com/spring-boot/spring-boot-log4j-2-example/
Logging in Spring Boot | Baeldung
log4j2 console info file debug
log4j – log4j2 xml configuration – Log to file and console (with different levels) – Stack Overflow
好像可以用:
    <root level="debug">
      <appender-ref ref="Console" level="info"/>
      <appender-ref ref="MyFile" level="error"/>
    </root>
去试试
  <Loggers>
    <Root level="debug">
      <AppenderRef ref="FILEOUT" level="debug"/>
      <AppenderRef ref="STDOUT" level="info"/>
    </Root>
  </Loggers>
结果:
终于实现了对应的效果:
  • file:debug的level
  • console:info的level
后来也在官网中
Log4j – Custom Log Levels
看到了对应的用法举例:
  <Loggers>
    <Root level="trace">
      <!-- Only events at DIAG level or more specific are sent to the console. -->
      <AppenderRef ref="Console" level="diag" />
      <AppenderRef ref="MyFile" level="trace" />
    </Root>
  </Loggers>
聊一聊log4j2配置文件log4j2.xml – 阿豪聊干货 – 博客园
Log4j2 properties file example – HowToDoInJava
Frequently Asked Questions – Apache Log4j 2
“How do I send log messages with different levels to different appenders?
You don’t need to declare separate loggers to achieve this. You can set the logging level on the AppenderRef element.
  <Loggers>
    <Root level=”trace”>
      <AppenderRef ref=”file” level=”DEBUG”/>
      <AppenderRef ref=”STDOUT” level=”INFO”/>
    </Root>
  </Loggers>
Log4j – Configuring Log4j 2
Log4j – Custom Log Levels
后来也去回复了帖子:
log4j – log4j2 xml configuration – Log to file and console (with different levels) – Stack Overflow
【总结】
最后用配置:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <File name="FILEOUT" fileName="xxx.log" append="false">
      <PatternLayout>
        <Pattern>%d{yyyyMMdd HH:mm:ss} %-5p [%t] %C{2} %F%L - %m%n</Pattern>
      </PatternLayout>
    </File>


    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p %F:%L - %m%n"/>
    </Console>
  </Appenders>


  <Loggers>
    <Root level="debug">
      <AppenderRef ref="FILEOUT" level="debug"/>
      <AppenderRef ref="STDOUT" level="info"/>
    </Root>
  </Loggers>
</Configuration>
核心部分是:
    <Root level="debug">
      <AppenderRef ref="FILEOUT" level="debug"/>
      <AppenderRef ref="STDOUT" level="info"/>
    </Root>
分别指定了不同level和实现对应效果:
  • file的level是debug
    • debug及之上,如info等,都可以显示
  • console的level是info
    • info之下的,如debug等(日志太多)就不显示,只显示info及以上的日志
附录:
关于日志level:
  • 共有8个级别
    • 按照从低到高为:All < Trace < Debug < Info < Warn < Error < Fatal < OFF.
具体解释:
  • All:最低等级的,用于打开所有日志记录.
  • Trace:是追踪,就是程序推进以下,你就可以写个trace输出,所以trace应该会特别多,不过没关系,我们可以设置最低日志级别不让他输出.
  • Debug:指出细粒度信息事件对调试应用程序是非常有帮助的.
  • Info:消息在粗粒度级别上突出强调应用程序的运行过程.
  • Warn:输出警告及warn以下级别的日志.
  • Error:输出错误信息日志.
  • Fatal:输出每个严重的错误事件将会导致应用程序的退出的日志.
  • OFF:最高等级的,用于关闭所有日志记录.

转载请注明:在路上 » 【已解决】java中log4j2中配置日志的level不同:console是INFO而file是DEBUG

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.187 seconds, using 23.39MB memory