SLF4J日志级别作为参数 [英] SLF4J Log Level as an argument

查看:390
本文介绍了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日志级别作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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