Java日志记录:Log4j Version2.x:显示最终客户端调用方法(不是中间日志记录助手方法) [英] Java Logging: Log4j Version2.x: show the method of an end-client caller (not an intermediate logging helper method)

查看:218
本文介绍了Java日志记录:Log4j Version2.x:显示最终客户端调用方法(不是中间日志记录助手方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下3篇帖子提供了如何使用中间日志记录助手的答案,并且仍然让底层记录器从客户端的方法报告给该日志助手(而不是将日志助手方法报告为源):

The following 3 posts offer answers for how to use an intermediate logging helper and still get the underlying logger to report from the method of a client to that logging helper (rather than reporting the logging helper method as the source):

致电log4j的间接日志方法(来自帮助方法)

打印source源代码>带有log4j包装器的日志语句中的类

但似乎只提供 Log4j 1.2 ,提供现已解散的:

But the seem to only offer answers for Log4j 1.2, which offers the now-defunct:

 Category.log(String callerFQCN, Priority level, Object message, Throwable t). 

Log4 in log4J 2.5 API

任何人都可以提供与直接使用Log4J 2.x兼容的答案吗?

Can anybody offer an answer compatible with direct use of Log4J 2.x ?

推荐答案

对于Log4j2,答案完全通过使用logger包装器提供,如生成的记录器包装器的示例用法。可以简单地生成(使用其中所示的org.apache.logging.log4j.core.tools.Generate $ ExtendedLogger工具)具有单个STUB级别的记录器包装器,然后调整它以创建模仿logIfEnabled使用的自定义日志记录方法(FQCN,LEVEL,Marker,message,Throwable) - 可能忽略STUB级别并使用常规级别 - 然后根据需要删除或注释掉STUB级别及其方法)。为此,FormattedMessage可能会有所帮助。

For Log4j2 the answer is provided completely by the use of logger wrappers as described in the Log4j2 manual under Example Usage of a Generated Logger Wrapper. One can simply generate (using the org.apache.logging.log4j.core.tools.Generate$ExtendedLogger tools illustrated there) a logger wrapper with a single STUB level, and then adapt that to create custom logging methods mimicking the use of the logIfEnabled(FQCN, LEVEL, Marker, message, Throwable) - possibly ignoring the STUB level and using the regular ones - then if desired, deleting or commenting out the STUB level and its methods). For this purpose the FormattedMessage can be helpful.

示例:

java -cp log4j-core-2.5.jar org.apache.logging.log4j.core.tools.Generate\$ExtendedLogger com.mycomp.ExtLogger STUB=350 > com/mycomp/ExtLogger.java

然后调整生成的类(省略大多数支持方法):

Then adapt the generated class (most support methods omitted):

public final class ExtLogger extends ExtendedLoggerWrapper {
...
private final ExtendedLoggerWrapper logger;

private static final String FQCN = ExtLogger.class.getName();
private static final Level STUB = Level.forName("STUB", 350);
//Delete this afterwards if level not used.

private ExtLogger(final Logger logger) {
    super((AbstractLogger) logger, logger.getName(), logger.getMessageFactory());
    this.logger = this;
}

/**
 * Returns a custom Logger with the name of the calling class.
 * 
 * @return The custom Logger for the calling class.
 */
public static ExtLogger create() {
    final Logger wrapped = LogManager.getLogger();
    return new ExtLogger(wrapped);
}

/**
 * Returns a custom Logger using the fully qualified name of the Class as
 * the Logger name.
 * 
 * @param loggerName The Class whose name should be used as the Logger name.
 *            If null it will default to the calling class.
 * @return The custom Logger.
 */
public static ExtLogger create(final Class<?> loggerName) {
    final Logger wrapped = LogManager.getLogger(loggerName);
    return new ExtLogger(wrapped);
}

...

/**
 * Logs a message object with the {@code STUB} level.
 * 
 * @param message the message object to log.
 */
public void stub(final String message) {
    logger.logIfEnabled(FQCN, STUB, null, message, (Throwable) null);
}


/**
 * Example: Adapt with custom formatting.
 * Here DEBUG level is used just as an example.
 *
 * @param name
 * @param value 
 */
public void echo(final String name, Object value) {
    Message m = new FormattedMessage("echo: %s(%s)",name,value);
    logger.logIfEnabled(FQCN, Level.DEBUG, null, m, (Throwable) null);
}
...
}

然后在客户端现在,它将通过记录器的帮助方法正确地代表该客户端进行记录,在这种情况下,格式化示例echo(name,value):

Then in a client class it will now log "on behalf" of that client correctly via the logger's helper methods, in this case the formatting example echo(name,value):

public class TestLog4j {

private static final ExtLogger extLogger = ExtLogger.create(TestLog4j.class);

public static void elseWhere() {
    extLogger.echo("aVariableName", 4);
}

public static void main(String[] args) {
        extLogger.echo("aStringVariableName","from main");
        elseWhere();
}
}

Simple PatternLayout:

Simple PatternLayout:

  <PatternLayout pattern=" %-5level [%C{1}::%M(%L)] %logger{36} - %msg%n"/>

输出:

 DEBUG [TestLog4j::main(63)] testlogging.TestLog4j - echo: aStringVariableName(from main)
 DEBUG [TestLog4j::elseWhere(42)] testlogging.TestLog4j - echo: aVariableName(4)

一旦你掌握了使用logger.logIfEnabled(FQCN,...) FQCN(log4j在堆栈跟踪中搜索)如果不使用其他级别,您可能希望删除或注释掉存根(..)方法和STUB级别。

Once you've got the hang of using logger.logIfEnabled(FQCN,...) with the FQCN (which log4j searches for in the stack trace) you may wish to delete or comment out the stub(..) methods and STUB level if you don't use an additional level.

这篇关于Java日志记录:Log4j Version2.x:显示最终客户端调用方法(不是中间日志记录助手方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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