简体   繁体   English

“在警告级别使用 slf4j 进行日志记录”是否会阻止运行“logger.debug(...”

[英]Does "Logging with slf4j at warn level", prevent the running "logger.debug(..."

i've a question about processing log lines according to level like;我有一个关于根据级别处理日志行的问题;

logger.debug("hello i am a log line not so neccessary, also log level is setted WARN and some complexProcessResult-> {}",
doSomeWorkwhichTakeslongTime());

If logger level sets to WARN so does it call the process "doSomeWorkwhichTakeslongTime()" because jsonizing some classes, take so much time, and i dont want to run this in production.如果记录器级别设置为 WARN,那么它会调用进程“doSomeWorkwhichTakeslongTime()”,因为对某些类进行 jsonizing 需要花费很多时间,而且我不想在生产中运行它。 To achieving this, is it enough setting the log level as "warn"?为了实现这一点,将日志级别设置为“警告”就足够了吗?

No, if you use this specific method call, then changing the log level would have no impact on whether or not doSomeWorkWhichTakesLongTime() is being called or not.不,如果您使用此特定方法调用,则更改日志级别不会影响doSomeWorkWhichTakesLongTime()是否被调用。

It can't have an effect, because the Java language specifies that the parameter values need to be computed before the method is called.它不能有效果,因为Java语言规定在调用方法之前需要计算参数值。

There are two possible workarounds:有两种可能的解决方法:

The "ugly" one is to use isDebugEnabled() : “丑陋”的是使用isDebugEnabled()

if (logger.isDebugEnabled()) {
    logger.debug("hello i am a log line not so neccessary, also log level is setted WARN and some complexProcessResult-> {}", doSomeWorkwhichTakeslongTime());
}

The other one is a bit trickier, but potentially nicer: Have the heavy lifting happen in the toString() of an object that is either cheap to construct or that you have accessible anyway:另一个有点棘手,但可能更好:在构建成本低或您可以访问的对象的toString()中进行繁重的工作:

logger.debug("hello i am a log line not so neccessary, also log level is setted WARN and some complexProcessResult-> {}", someObjectThatDescribesTheOutput)

and have someObjectThatDescribesTheOutput have a toString method a little like this:并让someObjectThatDescribesTheOutput有一个类似于这样的toString方法:

public String toString() {
  return doSomeWorkwhichTakeslongTime();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM