在slf4j中设置运行时消息的日志级别 [英] Setting log level of message at runtime in slf4j
问题描述
使用log4j时, Logger.log(优先级p,对象消息)
方法可用,可用于在运行时确定的日志级别记录消息。我们正在使用此事实并此提示将stderr重定向到特定日志级别的记录器。
When using log4j, the Logger.log(Priority p, Object message)
method is available and can be used to log a message at a log level determined at runtime. We're using this fact and this tip to redirect stderr to a logger at a specific log level.
slf4j没有泛型 log()
我能找到的方法。这是否意味着无法实现上述目标?
slf4j doesn't have a generic log()
method that I can find. Does that mean there's no way to implement the above?
推荐答案
使用无法做到这一点slf4j
。
我想这个功能缺失的原因是它几乎不可能构建级别
类型为 slf4j
,可以有效地映射到级别
(或等效)类型用于Facade背后的所有可能的日志记录实现。或者,设计人员决定您的用例太不寻常以证明支持的开销是合理的它。
I imagine that the reason that this functionality is missing is that it is next to impossible to construct a Level
type for slf4j
that can be efficiently mapped to the Level
(or equivalent) type used in all of the possible logging implementations behind the facade. Alternatively, the designers decided that your use-case is too unusual to justify the overheads of supporting it.
关于 @ ripper234 的用例(单元测试),我认为实用的解决方案是将单元测试修改为难 - 在运行单元测试时,了解slf4j外观背后的日志记录系统。
Concerning @ripper234's use-case (unit testing), I think the pragmatic solution is modify the unit test(s) to hard-wire knowledge of what logging system is behind the slf4j facade ... when running the unit tests.
这篇关于在slf4j中设置运行时消息的日志级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!