本文介绍了SLF4J日志级别作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们希望使用SLF4J,但我们发现的一件事是您无法将关卡指定为参数,即
We are looking to use SLF4J, but one thing we found was that you can't specify the level as an argument, i.e
Logger.log(Level.INFO, "messsage");你必须这样做
logger.info("message");这可以防止通过方法传递所有内容,因此您可以将所有日志消息的其他属性在课堂上。
this prevents being able to pass everything through a method, so you can tack other properties to all log messages in a class.
public class Test { public Test(SomeObj obj) { log(Level.INFO, "message"); } public void anotherMethod() { log(Level.DEBUG, "another message"); } private void log(Level level, String message) { logger.log(level, message + obj.someString()); } }有没有办法用SLF4j实现这个目的?
Is there a way to achieve this using SLF4j ?
推荐答案围绕slf4j调用编写一个包装器,并为六个日志级别创建自己的枚举。然后在你的包装器中,使用一个开关来调用正确的slf4j调用。
Write a wrapper around the slf4j call and create your own enum for the six log levels. Then in your wrapper, use a switch to call the correct slf4j call.
void myLog(Level level, String message) { switch (level) { case FATAL: log.fatal(message); break; case ERROR: log.error(message); break; .... } }更多推荐
SLF4J日志级别作为参数
发布评论