简体   繁体   中英

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.

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

Here is the log4j2.xml file:

<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.

Here is a snippet from the 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

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. This may prevent the XML from being parsed, and log4j will install a default configuration that only logs to the console at error level.

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 . 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

If one of the files is missing then the logging does not work. Thank you all for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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