Log4j2 RollingFile无效

时间:2016-02-07 04:06:36

标签: java log4j log4j2

我开始在我正在开发的系统上使用Log4j2。 我遇到了一些问题。

这是我的 log4j2.properties 文件。

status = error
name = PropertiesConfig

filters = threshold

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

appenders = console, rolling, list

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${sys:catalina.home}/logs/beo.log
appender.rolling.filePattern = ${sys:catalina.home}/logs/beo-%d{MM-dd-yy-HH-mm-ss}-%i.log
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 = 2
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 = 5

appender.list.type = List
appender.list.name = List
appender.list.filters = threshold
appender.list.filter.threshold.type = ThresholdFilter
appender.list.filter.threshold.level = error

loggers = rolling

logger.rolling.name = org.apache.logging.log4j.core.appender.rolling
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRefs = rolling
logger.rolling.appenderRef.rolling.ref = RollingFile

rootLogger.level = info
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

以下是登录方法,我试图测试日志记录。

private static final Logger LOGGER = LogManager
            .getLogger(NavigationController.class.getName());

@RequestMapping(value = { "/", "/login" })
    public ModelAndView loginPage(
            @RequestParam(value = "error", required = false) String error,
            @RequestParam(value = "logout", required = false) String logout) {

        ModelAndView mv = new ModelAndView();

        Authentication auth = SecurityContextHolder.getContext()
                .getAuthentication();

        if (!(auth instanceof AnonymousAuthenticationToken)) {
            mv.setViewName("redirect:/auth/panel");

        } else {
            if (error != null) {
                mv.addObject("error", "Invalid username and password!");
                LOGGER.debug("debug");
                LOGGER.info("info");
                LOGGER.error("error");
                LOGGER.warn("warning");
                LOGGER.fatal("fatal");
            }
            mv.setViewName("login");
        }

        return mv;
    }

enter image description here

RollingFile似乎不起作用。它创建一个名为beo.logs的日志文件,但它也是空的。我的配置有问题吗?

1 个答案:

答案 0 :(得分:2)

您的Logger名称显示为somepackage.NavigationController。当然它所包含的包不是org.apache.logging.log4j.core.appender.rolling,它是您配置为转到RollingFileAppender的唯一Logger。我怀疑你真的想使用NavigationController的包名。