简体   繁体   English

如何将动态元数据添加到 Java 中的单例 SLF4J 记录器?

[英]How to add dynamic metadata to a singleton SLF4J logger in Java?

This is a Java question.这是一个Java问题。

I have around 100 static functions that use a slf4j logger.我有大约 100 个使用 slf4j 记录器的静态函数。 I want to add some metadata to standardise the logs - assume it's some kind of preamble that is a function of what processing is currently going on.我想添加一些元数据来标准化日志 - 假设它是某种序言,是当前正在进行的处理的函数。 How can I get the logger to print that metadata without going in to each of the static functions and changing them to explicitly add in the metadata.如何让记录器在不进入每个静态函数并更改它们以显式添加元数据的情况下打印该元数据。

eg例如


static Logger logger; ...
void mainProcessing() {
String file = "current_file.txt";
int line = 3;
...
func1();
func2();
...

}

void func1() {
...
logger.warn("some warning");
}

I'd like to see "WARN: File current_file.txt, line 3, msg: some warning" in the logs.我想在日志中看到"WARN: File current_file.txt, line 3, msg: some warning"

Any ideas?有任何想法吗?

(Prefer not to have to change each of the func1() functions obviously, if possible) (如果可能的话,最好不要明显地更改每个func1()函数)

Thanks in advance.提前致谢。

You need to specify print format.您需要指定打印格式。 However beware, that obtaining line number and/or file will greatly decrease your application performance.但是请注意,获取行号和/或文件会大大降低您的应用程序性能。 This is not C++, Logback will probably create a Throwable object for each line, to retrieve the stacktrace and extract the line number and file name.这不是 C++,Logback 可能会为每一行创建一个 Throwable 对象,以检索堆栈跟踪并提取行号和文件名。

Regarding line:关于线路:

L / line Outputs the line number from where the logging request was issued. L / line 输出发出日志请求的行号。

Generating the line number information is not particularly fast.生成行号信息并不是特别快。 Thus, its use should be avoided unless execution speed is not an issue.因此,除非执行速度不是问题,否则应避免使用它。

Regarding file:关于文件:

F / file Outputs the file name of the Java source file where the logging request was issued. F / file 输出发出日志请求的 Java 源文件的文件名。

Generating the file information is not particularly fast.生成文件信息并不是特别快。 Thus, its use should be avoided unless execution speed is not an issue.因此,除非执行速度不是问题,否则应避免使用它。

http://logback.qos.ch/manual/layouts.html#file http://logback.qos.ch/manual/layouts.html#file

Sample logback.xml configuration would be:示例 logback.xml 配置将是:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{"yy-MM-dd HH:mm:ss,SSS"}: %-5p %F:%L [%t] %c{0}: %M - %m%n</pattern>
    </encoder>
  </appender>

  <root level="INFO">
    <appender-ref ref="console" />
  </root>
</configuration>

Better to redesign your code , that messages are more verbose and not unique , containing the context and required data to debug it by a person that does not know the code very well (your future co-worker)最好重新设计您的代码,该消息更加冗长且不唯一,包含上下文和所需数据,以便由不太了解代码的人(您未来的同事)进行调试

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

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