为什么从其他代码而不是从我的代码向控制台输出Log4j? [英] Why is there Log4j output to the console from other code, but not from my code?

查看:51
本文介绍了为什么从其他代码而不是从我的代码向控制台输出Log4j?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Eclipse/STS和Spring MVC构建的Web应用程序.

I have a web app built with Eclipse/STS and Spring MVC.

我正在使用slf4j和log4j进行记录.

I'm using slf4j and log4j for logging.

这是我的log4j.properties文件...我知道它正在使用,因为我在日期模式中添加了文本"MY TEST PROPS FILE",并且它出现在日志中...

Here's my log4j.properties file ... I know it's being used because I added the text "MY TEST PROPS FILE" to the date pattern and it appears in the logs ...

# Root logger option
log4j.rootLogger=DEBUG, INFO, WARN, ERROR, stdout, file

# Redirect log messages to console
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=MY TEST PROPS FILE - %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

当我运行Web应用程序时,控制台输出看起来像这样...您可以在输出中看到我的测试道具文件" ...

When I run my web app, the console output looks like this ... you can see the "MY TEST PROPS FILE" in the output ...

MY TEST PROPS FILE - 2020-05-29 17:18:36 DEBUG BasicResourcePool:422 - decremented pending_acquires: 0
MY TEST PROPS FILE - 2020-05-29 17:18:36 DEBUG BasicResourcePool:1644 - trace com.mchange.v2.resourcepool.BasicResourcePool@468f3122 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@642b4cb2)
MY TEST PROPS FILE - 2020-05-29 17:18:36 DEBUG NewProxyConnection:1260 - com.mchange.v2.c3p0.impl.NewProxyConnection@71cf92cb: close() called more than once.
MY TEST PROPS FILE - 2020-05-29 17:18:36 DEBUG BasicResourcePool:1644 - trace com.mchange.v2.resourcepool.BasicResourcePool@468f3122 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@642b4cb2)
MY TEST PROPS FILE - 2020-05-29 17:18:36 DEBUG BasicResourcePool:1644 - trace com.mchange.v2.resourcepool.BasicResourcePool@468f3122 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@642b4cb2)

我的问题是,我从我的代码尝试的日志记录没有出现在控制台输出中.

My problem is, the logging I attempt from my code doesn't appear in the console output.

我正在这样做...

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* ... */

    private static Logger logger = LoggerFactory.getLogger(WebController.class);

/* ... */

    @RequestMapping(value={"", "/", "showhome"}, method={RequestMethod.POST,RequestMethod.GET})
    public ModelAndView showHome(Model model) {

        logger.info("########### TEST LOG INFO");
        logger.error("########### TEST LOG ERROR");
        logger.warn("########### TEST LOG WARN");
        logger.debug("########### TEST LOG DEBUG");

/* ... */
    }

我一定缺少简单的东西.为什么我的测试记录器调用未出现在控制台输出中?

I must be missing something simple. Why are my test logger calls not appearing in the Console output?

更新-找到的解决方案:我找到了一个解决方案,并将其发布为下面的答案.

UPDATE - SOLUTION FOUND: I found a solution and posted it as an answer below.

推荐答案

我找到了解决问题的方法.

I found a solution to my issue.

我将其放在pom.xml中更改为log4j2 ...

I changed to log4j2 by putting this in my pom.xml ...

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.7</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.7</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.7</version>
</dependency>

然后我创建了一个像这样的log4j2.properties文件...

And then I created a log4j2.properties file like this ...

status = error
name = PropertiesConfig

filters = threshold

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

appenders = console

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

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

现在工作.

干杯.

这篇关于为什么从其他代码而不是从我的代码向控制台输出Log4j?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆