log4j2不会从MVC中的Spring Boot Controller写入日志文件

时间:2017-09-28 18:31:39

标签: spring spring-mvc logging spring-boot log4j2

如果这是另一个问题的重复,我表示道歉,但我还没有找到我在这里找到的任何其他答案的修复方法。下面的代码摘录并非包含文件中的所有代码,但包含所有相关部分。我无法完全复制/粘贴我的整个文件,因为这是针对公司项目的。

我对春季启动相当新(使用它可能需要10周),这是我第一次尝试使用log4j2(必须在我们的公司环境中使用它。)

build.gradle摘录:

dependencies {
        ....
        // log4j2
        compile("org.springframework.boot:spring-boot-starter-log4j2")
        compile("org.apache.logging.log4j:log4j-api")
        compile("org.apache.logging.log4j:log4j-core")
        compile("org.apache.logging.log4j:log4j-web")
        ....
}

ApplicationController.java:

package com.test.controllers;

import ...
....
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Controller
public class ApplicationController {

    public static final Logger logger = LogManager.getLogger(Application.class);
}

IndexController.java:

package com.test.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController extends ApplicationController {

  public IndexController() {
  }

  @RequestMapping("/")
  public String index(Model model) {
    // just a quick example of logging
    logger.debug("--INDEX PAGE--");
    return "index/index";
  }
}

Application.java (我的日志文件有1个条目,它来自这个类 - “正在启动应用程序......”行):

package com.test;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static final Logger logger = LogManager.getLogger(Application.class);

    public static void main(String[] args) {
        logger.debug("Starting application...");
        SpringApplication.run(Application.class, args);
        logger.debug("ending application...");
    }

}

log4j2.properties:

status = debug
name = PropertiesConfig

property.filename = /var/tmp/logs/test.log

filters = threshold

filter.threshold.type = ThresholdFilter
filter.threshold.level = debug

appenders = rolling

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = test1-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20

loggers = rolling

logger.rolling.name = com.test
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

logger.rolling.name = com.test.controllers
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

rootLogger.level = debug
rootLogger.appenderRef.rolling.ref = RollingFile

使用上面的代码,我的应用程序创建/var/tmp/logs/test.log文件并向其写入一个条目:

2017-09-28 13:54:56,141 DEBUG c.t.Application [main] Starting application...

但是,我还需要从我的Controllers / etc中访问记录器,以允许我的整个应用程序写入此日志文件。我试图在我的ApplicationController.java中调用一个记录器,然后在我的IndexController.java中使用它。我在控制台中看到了IndexController的条目,但它没有写入日志文件。

我做错了什么?这里的任何帮助将不胜感激。

0 个答案:

没有答案