log4j 将所有日志输出定向到 stdout,即使它不应该这样做

发布于 2024-08-01 15:39:15 字数 568 浏览 3 评论 0原文

在我的 log4j.properties 中,我有:

log4j.rootLogger=DEBUG,stdout

log4j.logger.notRootLogger=DEBUG,somewhereelse

追加器 stdout 和其他地方都配置正确,stdout 写入控制台,其他地方写入文件。

在每个类的代码中,我设置:

static Logger log =  Logger.getLogger("notRootLogger);

^ 当我不想让东西进入控制台时。

-或-

static Logger log = Logger.getRootLogger();

^ 当我这样做时。

我必须在 log4.properties 中做什么才能阻止写入 notRootLogger 的内容最终出现在 stdout 中? 根记录器写入的地方是否存在某种需要以某种方式关闭的继承?

我不想为每个类单独配置一个记录器,我只想记录到控制台。

In my log4j.properties I have:

log4j.rootLogger=DEBUG,stdout

log4j.logger.notRootLogger=DEBUG,somewhereelse

The appenders stdout and somewhereelse are both configured properly, stdout writes to the console and somewhereelse writes to a file.

In my code in each class either I set either:

static Logger log =  Logger.getLogger("notRootLogger);

^ When I don't want stuff going to the console.

-OR-

static Logger log = Logger.getRootLogger();

^ When I do.

What do I have to do in log4.properties to stop the things that are written to notRootLogger ending up in stdout? Is there some sort of inheritance of wherever the root logger writes to going on that needs to be turned off somehow?

I don't want to have to configure a logger for every single class individually that I just want to log to the console.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛溺水了 2024-08-08 15:39:15

如果java类中已经定义了logger的属性,那么最后可以调用logger.shutdown()方法,最好是在finally块中,以禁止logger的附加性。

If the properties of logger have been defined in java class, you can call logger.shutdown() method in the end, preferebly in the finally block to prohibit the additive nature of logger.

对你而言 2024-08-08 15:39:15

您需要设置additivity = false,IIRC。 来自 log4j 手册

每个启用的日志记录请求
给定的记录器将转发给所有记录器
该记录器中的附加程序也是如此
由于附加程序更高
等级制度。 换句话说,附加程序
是从附加继承的
记录器层次结构。 例如,如果一个
控制台附加程序已添加到根目录
logger,然后是所有启用的日志记录
请求至少会打印在
安慰。 如果另外一个文件
附加程序被添加到记录器中,例如 C,
然后启用 C 的日志记录请求
C 的子级将打印在文件上
并在控制台上。 有可能
覆盖此默认行为,以便
不再有appender累积
通过设置加和性进行加法
标记为 false。

尝试这个:

log4j.rootLogger=DEBUG,stdout
log4j.logger.notRootLogger=DEBUG,somewhereelse
log4j.additivity.notRootLogger=false

You need to set additivity = false, IIRC. From the log4j manual:

Each enabled logging request for a
given logger will be forwarded to all
the appenders in that logger as well
as the appenders higher in the
hierarchy. In other words, appenders
are inherited additively from the
logger hierarchy. For example, if a
console appender is added to the root
logger, then all enabled logging
requests will at least print on the
console. If in addition a file
appender is added to a logger, say C,
then enabled logging requests for C
and C's children will print on a file
and on the console. It is possible to
override this default behavior so that
appender accumulation is no longer
additive by setting the additivity
flag to false.

Try this:

log4j.rootLogger=DEBUG,stdout
log4j.logger.notRootLogger=DEBUG,somewhereelse
log4j.additivity.notRootLogger=false
水波映月 2024-08-08 15:39:15

嗯,应该更仔细地阅读 log4j 的简短介绍来

log4j.additivity.notRootLogger=false

修复它,因为它继承了层次结构中位于其上方的记录器的附加程序,并且根记录器显然位于层次结构的顶部。

Hmm, should have read the short intro to log4j more carefully

log4j.additivity.notRootLogger=false

fixes it, because it inherits appenders from the loggers above it in the hierarchy, and the root logger is at the top of the hierarchy obviously.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文