PropertyConfigurator.configure不适用于spring

时间:2017-08-06 11:07:32

标签: java spring maven logging log4j

我尝试使用spring IOC初始化log4j logger并使用PropertyConfigurator.configure配置logger,但PropertyConfigurator.configure不起作用。  使用默认配置创建记录器,不会创建记录文件。

我的问题是如何将配置(log4j.properties)传递给Logger类?

的pom.xml

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
          <version>1.5.5.RELEASE</version>
    </dependency>
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
          <version>1.5.5.RELEASE</version>
    </dependency> 

Confugration class

@Configuration 
public class CommonConfig {

@Autowired
private Environment env;

@PostConstruct
public void init() {
   PropertyConfigurator.configure("log4j.properties");
}

@Bean(name = "debugLogger")
public Logger debugLogger() {
    return Logger.getLogger("regDebug");
}

@Bean(name = "infoLogger")
public Logger infoLogger() {
    return Logger.getLogger("regOperational");
}

@Bean(name = "errorLogger")
public Logger errorLogger() {
  ic Logger infoLogger() {
    return Logger.getLogger("regError");
}}

log4j.properties

log4j.logger.regDebug=DEBUG , stdout, regDebug
log4j.logger.regOperational=INFO, stdout , regOperational
log4j.logger.regError=ERROR , stdout , regError
log4j.rootLogger = INFO, console, regDebug, regOperational, regErro
log4j.appender.fileAppener=org.apache.log4j.RollingFileAppender

# STDOUT appender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

#DEBUG LEVEL
log4j.appender.regDebug.Append=true
log4j.appender.regDebug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.regDebug.Append=true
log4j.appender.regDebug.File=D:/Code/log/COMMON_DEBUG.log
log4j.appender.regDebug.MaxFileSize=10MB
log4j.appender.regDebug.MaxBackupIndex=10
log4j.appender.regDebug.DatePattern='.'yyyy-MM-dd-HH'.log'
log4j.appender.regDebug.layout=org.apache.log4j.PatternLayout
log4j.appender.regDebug.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss.SSS} || %5p || %t || %C{1} || %M:%L || %m%n

#INFO LEVEL

log4j.appender.regOperational=org.apache.log4j.DailyRollingFileAppender
log4j.appender.regOperational.Append=true
log4j.appender.regOperational.File=D:/Code/log/COMMON_ERROR.log
log4j.appender.regOperational.DatePattern='.'yyyy-MM-dd-HH'.log'
log4j.appender.regOperational.layout=org.apache.log4j.PatternLayout
log4j.appender.regOperational.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss.SSS} || %5p || %t || %m%n

#ERROR LEVEL

log4j.appender.regError=org.apache.log4j.DailyRollingFileAppender
log4j.appender.regError.Append=true
log4j.appender.regError.File=D:/Code/log/COMMON_ERROR.log
log4j.appender.regError.DatePattern='.'yyyy-MM-dd-HH'.log'
log4j.appender.regError.layout=org.apache.log4j.PatternLayout
log4j.appender.regError.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss.SSS} || %5p || %t || %C{1} || %M:%L || %m%n

1 个答案:

答案 0 :(得分:4)

Spring Boot通过spring-boot-starter-log4j依赖提供与Log4J的集成:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-log4j</artifactId>
   <version>1.3.8.RELEASE</version>
</dependency>   

但首先你需要排除默认的Spring Boot的日志记录机制:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
   <exclusions>
      <exclusion>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-logging</artifactId>
      </exclusion>
   </exclusions>
</dependency>

另外,请确保log4j.properties位于src/main/resources下方。在这种情况下,您无需明确指定文件的路径PropertyConfigurator,并且可以安全地删除init()方法。

请注意,Log4J 1.2.17已经过时了 - reached the end of life in 2015。您可能需要考虑使用Log4J2,或者使用Logback - Spring Boot使用的默认日志记录机制。