简体   繁体   English

带有log4j2的slf4j创建日志文件,但不写入日志文件

[英]slf4j with log4j2 creates log files but does not write to it

I have a Spring Boot app that I am trying to setup logging using SLF4J and Log4J2. 我有一个Spring Boot应用程序,我正在尝试使用SLF4J和Log4J2设置日志记录。

Here is a snippet from application 这是应用程序的摘录

package hello;
import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController implements IGreetingService {
  //  private Logger logger =    LogManager.getLogger(GreetingController.class.getName());
  private Logger logger = LoggerFactory.getLogger(GreetingController.class.getName());
  private static final String template = "Hello, %s!";
  private final AtomicLong counter = new AtomicLong();

  @Override
  public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
    logger.error("greeting called");
    return new Greeting(counter.incrementAndGet(), String.format(template, name));
   }
}

Here are the contents of log4j2.component.properties file where I define the log4j config file: log4j.configurationFile=c:\\log4j2.xml 这是log4j2.component.properties文件的内容,我在其中定义了log4j配置文件:log4j.configurationFile = c:\\ log4j2.xml

Here is the log4j2.xml file: 这是log4j2.xml文件:

<Configuration>
    <properties>
        <property name="path">logs</property>
    </properties>
<Appenders>
    <Console name="consoleAppender" target="SYSTEM_OUT">
     <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss} %msg"/>
    </Console>
    <File name="myfileAppender" fileName="${path}/file.log" immediateFlush="true" append="true">
        <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
      </File>
      <RollingFile name="rollingAppender" 
        fileName="c:\\roll.log" 
        filePattern="c:\\roll.log\\$${date:yyyy-MM}\\app-%d{MM-dd-yyyy}-%i.log">
           <PatternLayout>
               <pattern>%d %p %C{1.} [%t] %m%n</pattern>
           </PatternLayout>
           <Policies>
              <OnStartupTriggeringPolicy />
              <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
              <SizeBasedTriggeringPolicy size="250 MB"/>
           </Policies>
      </RollingFile>    
  </Appenders>
<Loggers>
<Logger name="hello" level="error">
    <AppenderRef ref="consoleAppender"/>
    <AppenderRef ref="myfileAppender"/>
</Logger>  
<Root level="error">
  <AppenderRef ref="consoleAppender"/>
  <AppenderRef ref="myfileAppender"/>
  <AppenderRef ref="rollingAppender"/>
</Root>

I have three appenders setup for the console, a file and a rolling file. 我为控制台设置了三个附加程序,一个文件和一个滚动文件。 When I run my application, I see logs in the console but they are not formatted like I defined in the configuration. 当我运行应用程序时,我会在控制台中看到日志,但是日志的格式没有我在配置中定义的格式。

2016-09-13 12:41:09.569 ERROR 9892 --- [nio-8888-exec-6] h.GreetingController                     : greeting called

The file.log and the roll.log are created but they do not have any log information in them. 创建了file.log和roll.log,但是它们中没有任何日志信息。

Here is a snippet from the build.gradle 这是build.gradle的片段

configurations {
  compile.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
  compile.exclude group: 'commons-logging'
}

dependencies {
    compile(group: 'org.springframework.boot',  name: 'spring-boot-starter-web')
    compile(group: 'org.springframework.boot',  name: 'spring-boot-starter-log4j2')
    compile(group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka-server')
    compile group: 'com.netflix.eureka',        name: 'eureka-client', version: '1.4.10'
    compile group: 'com.sun.jersey',            name: 'jersey-server', version: '1.19.1'
    compile group: 'com.sun.jersey',            name: 'jersey-core', version: '1.19.1'
    compile group: 'com.sun.jersey',            name: 'jersey-servlet', version: '1.19.1'   
}

Spring boot version: 1.3.6 春季启动版本:1.3.6

Any help figuing out what I am doing wrong would be greatly appreciated. 任何帮助弄清楚我在做什么错的帮助将不胜感激。 Thanks in advance. 提前致谢。

The configuration file as shown doesn't have closing </Loggers and </Configuration > tags. 所示的配置文件没有</Loggers</Configuration >标记。 This may prevent the XML from being parsed, and log4j will install a default configuration that only logs to the console at error level. 这可能会阻止XML的解析,并且log4j将安装默认配置,该配置仅以错误级别记录到控制台。

If the configuration file is not found or processed correctly, you can still enable log4j2 internal status logging by setting system property -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE . 如果找不到或未正确处理配置文件,则仍可以通过设置系统属性-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE来启用log4j2内部状态日志记录。 This may give you insight into what is going wrong. 这可以让您深入了解问题所在。

I figured out how to get it to work. 我想出了如何使其工作。 I needed to add the log4j2.xml file to both the src/main/resources in my project and the file system, in my case c:\\log4j2.xml 我需要将log4j2.xml文件添加到项目和文件系统中的src / main / resources中,在我的情况下为c:\\ log4j2.xml

If one of the files is missing then the logging does not work. 如果缺少其中一个文件,则日志记录将不起作用。 Thank you all for your help. 谢谢大家的帮助。

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

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