懒惰评估Java 8中的日志记录 [英] Lazy evaluation for logging in Java 8

查看:100
本文介绍了懒惰评估Java 8中的日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您计算出的值不算昂贵时,在日志记录框架中看到的常见模式就是

When you have values than are expensive to compute, a common pattern you see in logging frameworks is

if (log.isDebugEnabled()) {
    String value = expensiveComputation();
    log.debug("value: {}", value);
}

自从Java 8添加了lambda以来,这样做就很好了:

Since Java 8 added lambdas, it'd be nice to do:

log.debug("value: {}", (Supplier<String>) this::expensiveComputation);

几乎可以使用 的原因是,日志记录框架将对参数执行toString().问题是Supplier上的toString()Object中的实现.

Which almost works because the logging framework will do toString() on the parameter. The problem is toString() on Supplier is the implementation in Object.

是否有办法向Logger方法提供延迟评估的内容?几乎只是一个Supplier,默认的toString()会调用get().

Is there a way to supply something that's evaluated lazily to Logger methods? It would almost just be a Supplier with a default toString() that calls get().

推荐答案

要传递将以懒惰方式执行String计算的参数,您必须传递Supplier而不是String. br> 您调用的方法应具有以下签名:

To pass an argument that will executed in a lazy way the String computation, you have to pass a Supplier and not a String.
The method that you invoke should have this signature :

void debug(Supplier<?> msgSupplier, Throwable t)

您可以在自己的实用程序类中引入此实用程序方法.
但是您不必这样做,因为最近的日志记录框架(例如Log4j2)提供了开箱即用的功能.

You could introduce this utility method in your own utility class.
But you should not need to do that as recent logging frameworks such as Log4j2 provides this feature out of the box.

例如, org .apache.logging.log4j.Logger 提供了接受Supplier的重载方法来记录日志.
对于

For example, org.apache.logging.log4j.Logger provides overloaded methods to log that accept a Supplier.
For example :

void debug(MessageSupplier msgSupplier, Throwable t)

记录一条消息(仅当记录级别为 DEBUG级别),包括按以下方式传递的Throwable t的堆栈跟踪 范围. MessageSupplier可能会或可能不会使用MessageFactory 构造消息.

Logs a message (only to be constructed if the logging level is the DEBUG level) including the stack trace of the Throwable t passed as parameter. The MessageSupplier may or may not use the MessageFactory to construct the Message.

Parameters:

msgSupplier-一个函数,在调用该函数时会生成所需的日志 消息.

msgSupplier - A function, which when called, produces the desired log message.

t-要记录的异常,包括其堆栈跟踪.

t - the exception to log, including its stack trace.

从Log4j2文档中:

From Log4j2 documentation :

Java 8 lambda支持惰性日志记录

在版本2.4中,Logger界面添加了对lambda的支持 表达式.这允许客户端代码懒惰地记录消息,而无需 明确检查请求的日志级别是否已启用.为了 例如,以前您会写:

In release 2.4, the Logger interface added support for lambda expressions. This allows client code to lazily log messages without explicitly checking if the requested log level is enabled. For example, previously you would write:

if (logger.isTraceEnabled()) {
    logger.trace("Some long-running operation returned {}", expensiveOperation());
}

使用Java 8,您可以使用lambda表达式实现相同的效果. 您不再需要显式检查日志级别:

With Java 8 you can achieve the same effect with a lambda expression. You no longer need to explicitly check the log level:

logger.trace("Some long-running operation returned {}", 
              () ->    expensiveOperation());

这篇关于懒惰评估Java 8中的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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