简体   繁体   English

log4j日志文件排除日志消息

[英]log4j logging file exclude logs messages

I am building spring boot application version 2.0.2.RELEASE with BOM Finchley.RC2 and trying to write logs to file in using log4j2.xml . 我正在使用BOM Finchley.RC2构建Spring Boot应用程序版本2.0.2.RELEASE ,并尝试使用log4j2.xml将日志写入文件中。 The problem is i want to log only my application messages like: 问题是我只想记录我的应用程序消息,例如:

log.error("this is error");
log.info("this is info");

but the log file is showing other logs as well like Spring logs etc as below: 但是日志文件显示了其他日志以及Spring日志等,如下所示:

29-08-2018 18:01:45,023 [INFO ] [] [main] - this is info
29-08-2018 18:01:45,492 [INFO ] [] [main] - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@b4711e2: startup date [Wed Aug 29 18:01:45 PKT 2018]; root of context hierarchy
29-08-2018 18:01:45,659 [INFO ] [] [main] - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
29-08-2018 18:01:45,687 [INFO ] [] [main] - Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$af5d2aa2] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

How I can customize my log4j2.xml to acheive only my application logging for info/debug/error logs. 我如何自定义log4j2.xml以仅获取应用程序日志以获取信息/调试/错误日志。 Below is my log4j2.xml: 以下是我的log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <RollingFile name="FileAppender" fileName="mylogFile.log" 
        append="true" filePattern="log-%d{MM-dd-yyyy}-%i.log">
      <PatternLayout>
        <pattern>%d{dd-MM-yyyy HH:mm:ss,SSS} [%-5p] [%X{X-B3-TraceId}] [%t] - %m%n</pattern>
      </PatternLayout>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="250 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="20"/>
    </RollingFile>
    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{dd-MM-yyyy HH:mm:ss,SSS} [%-5p] [%X{X-B3-TraceId}] [%t] - %m%n"/>
    </Console>
    <File name="JSONAppender" fileName="\\tmp\\logFile.json.log" append="true">

    <JSONLayout complete="true" charset="UTF-8" compact="true" eventEol="true"/>

    </File>
  </Appenders>
  <Loggers>
    <Logger name="org.apache.logging.log4j.core.config.xml" level="info" >
      <AppenderRef ref="FileAppender"/>
    </Logger>
    <Logger name="guru.springframework.blog.log4j2json" level="debug">
      <AppenderRef ref="JSONAppender"/>
    </Logger>


    <Root level="info">
      <AppenderRef ref="STDOUT" />
      <AppenderRef ref="FileAppender"/>
      <AppenderRef ref="JSONAppender"/>
    </Root>
  </Loggers>
</Configuration>`

Any help please what is wrong with my configration? 任何帮助,请问我的配置有什么问题? With Spring 1.5.9 version it was working fine but as i upgrade to 2.0.2.RELEASE logging to file not working. 使用Spring 1.5.9 version它可以正常工作,但是当我升级到2.0.2.RELEASE记录到文件时无法正常工作。

In order to avoid INFO from external libraries you need to adjust the root log level from INFO to WARN or ERROR . 为了避免来自外部库的INFO ,您需要将根日志级别从INFO调整为WARNERROR

In addition you have to create an explicit logger for the packages/classes you want to write log messages from. 另外,您还必须为要从中写入日志消息的包/类创建一个显式的记录器。

<Logger name="com.my.app" level="info">
  <AppenderRef="STDOUT" />
  ..
</Logger>

I would also recommend that you add the logger ( %c ) to the log pattern. 我还建议您将记录器( %c )添加到日志模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM