log4j logger不写入日志文件

时间:2017-06-07 16:30:12

标签: java apache spring-mvc logging spring-boot

我正在尝试在Spring Boot应用程序上配置一个记录器 - 我只是从Spring MVC 2迁移到Spring Boot。因此所有配置都已到位。我已将log4j.properties文件放在应用程序类路径中,就在application.properties文件所在的位置。

Logger类的定义如下:

public class Logger {

    public static void Log(String className, String methodName, String message, int loggingLevel) {

        Log4jLogger log4jLogger = Log4jLogger.getInstance();
        org.apache.log4j.Logger logger = log4jLogger.getLogger();

        switch (loggingLevel) {
            case 1: {
                logger.error(className + " | " + methodName + " | " + message);
                break;
            }

            case 2:

            case 3: {
                logger.info(className + " | " + methodName + " | " + message);
                break;
            }

            case 4:

            case 5: {
                logger.debug(className + " | " + methodName + " | " + message);
                break;
            }
        }
    }
}

import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Log4jLogger {

    private Logger logger;
    private static Log4jLogger log_instance;

    public Log4jLogger() {
        String logFileName;
        Properties properties = new Properties();
        try {
            properties.load(getClass().getClassLoader().getResourceAsStream("log4j.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        logFileName = properties.getProperty("log4j.appender.A2.file");
        if (logFileName.equals("")) {
            logFileName = "temp";
        } else {
            logFileName = logFileName.substring(logFileName.lastIndexOf("/") + 1, logFileName.lastIndexOf("."));
        }
        logger = org.apache.log4j.Logger.getLogger(logFileName);
        PropertyConfigurator.configure(properties);
        log_instance = this;
    }

    public static Log4jLogger getInstance() {
        if (log_instance == null) {
            log_instance = new Log4jLogger();
        }
        return log_instance;
    }

    public Logger getLogger() {
        return logger;
    }
}

对于任何需要记录器的类,我只需要调用:

Logger.Log("ClassName", "MethodName", "message Start...", 5);

之前用于登录文件C:/logs/temp.log,但它似乎不适用于Spring Boot。只有信息日志会在控制台上打印出来。

在此放置log4j.properties的内容:

# Log4j configuration file.
log4j.rootCategory=INFO, A1
log4j.logger.orion=DEBUG, A2
log4j.logger.orionJUnit=DEBUG, A3
log4j.appender.A1.Threshold=WARN
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-5p %d{ISO8601} [%t] - %m%n
log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.file=C:/logs/temp.log
log4j.appender.A2.datePattern='.'yyyy-MM-dd
log4j.appender.A2.append=true
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-5p %d{ISO8601} [%t] - %m%n
log4j.appender.A3=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A3.file=c:/temp1/logs/logs.log
log4j.appender.A3.datePattern='.'yyyy-MM-dd
log4j.appender.A3.append=true
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=%-5p %d{ISO8601} [%t] - %m%n

关于我可能缺少什么的任何指示?

1 个答案:

答案 0 :(得分:0)

尝试从您的pom文件

中排除 logback jar
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
    </exclusions>
</dependency>