SLF4J日志级别作为参数

编程入门 行业动态 更新时间:2024-10-24 17:21:30
本文介绍了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日志级别作为参数

本文发布于:2023-11-25 04:01:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628236.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:级别   参数   日志   SLF4J

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!