简体   繁体   English

Java Logging - slf4j,log4j

[英]Java Logging - slf4j, log4j

Is there a way to pass slf4j logger a map of properties? 有没有办法将slf4j logger传递给属性映射?

for example, I would like to do something like this: 例如,我想做这样的事情:

Logger logger = LoggerFactory.getLogger(SomeClass.class);

Map data = new HashMap<String, String>();
data["key1"] = "value1";
data["key2"] = "value2";

logger.info("my info message", data)

Then, I would like that my appender will receive log4j LoggingEvent with the above parameters, like this: 然后,我希望我的appender将使用上述参数接收log4j LoggingEvent ,如下所示:

public class MyAppender extends AppenderSkeleton {
...
...

@Override
protected void append(LoggingEvent event) {
    Map properties = event.getProperties();
    String valueOfKey1 = (String) properties.get("key1");   
    //valueOfKey1 sould be equal to 'value1'
}
...
...    

UPDATE: 更新:

Is there any java logging framework that supports passing HashMap data (or similar in its interface) 是否有任何支持传递HashMap数据的java日志框架(或其界面中的类似)

From SLF4J Manual "Mapped Diagnostic Context" is essentially a map maintained by the logging framework where the application code provides key-value pairs which can then be inserted by the logging framework in log messages. 来自SLF4J手册 “映射诊断上下文”本质上是由日志框架维护的映射,其中应用程序代码提供键值对,然后日志消息中的日志记录框架可以插入键值对。 MDC data can also be highly helpful in filtering messages or triggering certain actions. MDC数据在过滤消息或触发某些操作方面也非常有用。

SLF4J supports MDC, or mapped diagnostic context. SLF4J支持MDC或映射诊断上下文。 If the underlying logging framework offers MDC functionality, then SLF4J will delegate to the underlying framework's MDC. 如果底层日志记录框架提供了MDC功能,那么SLF4J将委托给底层框架的MDC。 Note that at this time, only log4j and logback offer MDC functionality. 请注意,目前只有log4j和logback提供MDC功能。

As you tagged the question with log4j and as your example cites log4j classes, SLF4J will allow you to set values into the MDC map on a thread bases using slf4j API . 当您使用log4j标记问题并且您的示例引用了log4j类时,SLF4J将允许您使用slf4j API在线程基础上将值设置到MDC映射中。 Those values will then natively accessible at log4j level in the LoggingEvent . 这些值将在本地访问log4j的水平LoggingEvent

Here is an example adapted from Logback manual cited in SLF4J manual (and also by @Seelenvirtuose in its comment) : 这是一个改编自SLF4J手册中引用的Logback手册的例子(以及@Seelenvirtuose在其评论中):

public class SimpleMDC {
  static public void main(String[] args) throws Exception {

    // You can put values in the MDC at any time. Before anything else
    // we put the first name
    MDC.put("key1", "value1");

    [ SNIP ]

    Logger logger = LoggerFactory.getLogger(SimpleMDC.class);
    // We now put the last name
    MDC.put("key2", "value2");

    logger.info("Info log message");
  }
}

Then, in your custom appender, you can easyly get the values : 然后,在您的自定义appender中,您可以轻松获取值:

public class MyAppender extends AppenderSkeleton {
...
...

@Override
protected void append(LoggingEvent event) {
    String valueOfKey1 = (String) event.getMDC("key1");   
    //valueOfKey1 should be equal to 'value1'
}
...
... 

Of course, same functionnality would be available through Logback ... 当然,通过Logback可以获得相同的功能...

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

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