如何在运行时更改slf4j级别? [英] How to change slf4j level at runtime?

查看:1347
本文介绍了如何在运行时更改slf4j级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SLF4j作为我的日志框架,由log4j支持。我的问题是我正在寻找一种方法来在运行时更改记录器的日志记录级别。

I've using SLF4j as my logging framework, backed by log4j. My problem is that I am looking for a way to change the logging level for my logger at runtime.

我知道slf4j不允许直接通过自己的API,因此,我必须直接访问日志记录提供程序。就个人而言,我发现这是slf4j的一个巨大缺陷。所以现在我的问题是如何通过slf4j以编程方式确定我正在使用哪个提供程序?使用slf4j的最大目的是让您成为提供者无关的 - 您可以轻松地在您喜欢的日志记录系统之间切换,而无需重新编码。但是现在,如果我必须直接调用log4j,我就失去了这种能力。

I understand that slf4j does not permit this directly through its own API, and hence, I have to access the logging provider directly. Personally, I find this to be a huge deficiency in slf4j. So now my question is how can I determine programatically through slf4j which provider I am using? The biggest purpose of using slf4j is that you become provider agnostic - you can easily switch between your favourite logging system without having to recode anything. But now, if I have to make direct calls to log4j, I am losing that ability.

至少,我希望能够确定我是不是使用log4j作为提供程序,如果是,则允许用户切换日志级别。

At the very least, I would like to be able to determine if I am using log4j as the provider and if so, then allow the user to switch log levels.

如果我执行 LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME) ,结果是 org.slf4j.impl.Log4jLoggerAdapter 的一个实例,甚至不是 org.apache.log4j.Logger (正如我希望/预期的那样)。

If I do LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), the result is an instance of org.slf4j.impl.Log4jLoggerAdapter and not even org.apache.log4j.Logger (as I would have hoped/expected).

有没有办法找到这个?

谢谢,
Eric

Thanks, Eric

推荐答案

SLF4J被设计为库的抽象,而不是应用程序(当然,您可以并且仍然应该在自己的应用程序的日志记录调用中使用SLF4J,以保持一致性)。在您自己的应用程序中,您选择了底层记录器框架,因此可以在特定于logging-config的部分中访问log4j API。

SLF4J is designed as an abstraction for libraries, not applications (of course, you can and should still use SLF4J in your own app's logging calls, for consistency). In your own app, you choose the underlying logger framework, so it's fine to access the log4j API in the logging-config-specific parts.

库不应该是更改日志配置,恕我直言,所以它不适合在SLF4J API上。

No way should a library be mucking about with changing the logging config, IMHO, so it's not appropriate for it to be on the SLF4J API.

这篇关于如何在运行时更改slf4j级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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