【发布时间】:2018-08-24 09:21:59
【问题描述】:
我正在尝试使用 log4j2 编写一个日志模块,该模块需要以 json 的形式将日志写入控制台 STDOUT。
为此,我尝试以 JSON 的形式使用 PatternLayout,如下所示。
在将值从我的代码动态传递到 log4j2.xml 配置文件以在写入日志时在运行时替换它们时遇到一些困难。
我尝试使用 StructuredDataMessage 和 MapMessages 替换 Map 中的值,如 https://logging.apache.org/log4j/2.0/manual/lookups.html 中所述。
我也尝试了 StrLookup 和 ContextMaplookup,但目前没有成功。
Below is my xml config
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" name="App" packages="com.test.common.logging">
<Properties>
<Property name="filename">target/rolling1/rollingtest.log</Property>
<Property name="maptype">$${map:type}</Property>
</Properties>
<ThresholdFilter level="debug"/>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%highlight{{'logged time': '%d{dd MMM yyyy HH:mm:ss}',
'LEVEL' : '%level',
'CLASS' : '%c{-1}',
'Module' : '[%t]',
'message' : '%m',
'error' : '%exception',
'class' : '%C',
'threadid' : '%tid',
'threadname' : '%thread',
'whatisthis' : '${filename}',
'processid' : '%pid',
'logdir' : '$${sd:type}'
'location' : '${log4j:configLocation}'
'systemproperty' : '$${ctx:key-}'
}}%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
下面是我尝试使用结构化数据消息、strlookup 和 mapmessage 传递动态值的代码
public class App
{
public static void main( String[] args )
{
Logger logger = LogManager.getLogger(App.class);
// ConfigurationBuilder<BuiltConfiguration> builder
// = ConfigurationBuilderFactory.newConfigurationBuilder();
//
// LayoutComponentBuilder standard
// = builder.newLayout("PatternLayout");
// standard.
//
System.out.println( "Hello World!" );
StructuredDataMessage message = new StructuredDataMessage("1", "name", "string");
message.put("1", "nme");
// MapMessage mapm = new MapMessage(map)
MapMessage map = new MapMessage();
map.put("type", "value");
map.put("key", "value");
map.put("name", "arun");
StrLookup strlook = new StrLookup() {
public String lookup(LogEvent event, String key) {
// TODO Auto-generated method stub
return null;
}
public String lookup(String key) {
// TODO Auto-generated method stub
return "value";
}
};
ContextMapLookup lookup = new ContextMapLookup();
System.out.println(lookup.lookup("key"));
System.out.println(strlook.lookup("key"));
// MapLookup.setMainArguments(args);
System.setProperty("log_dir", App.class.getSimpleName());;
logger.trace("trace log message");
logger.debug("Debug log message");
logger.info("Info log message");
logger.error("Error log message");
logger.fatal("Fatal log message");
logger.info("Info log message[]");
logger.error("null exception[]", new NullPointerException());
// Lay
}
}
我的输出:
Hello World!
null
value
[36m{'logged time': '24 Aug 2018 12:32:51', 'LEVEL' : 'DEBUG', 'CLASS' : 'test.common.logging.App', 'Module' : '[main]', 'message' : 'Debug log message', 'error' : '', 'class' : 'com.test.common.logging.App', 'threadid' : '1', 'threadname' : 'main', 'whatisthis' : 'target/rolling1/rollingtest.log', 'processid' : 'DEBUGid', 'logdir' : '${sd:type}' 'location' : '/Users/parunkarthick/ferry-commons/common/logging/target/classes/log4j2.xml' 'systemproperty' : '${ctx:key}' [m}
[32m{'logged time': '24 Aug 2018 12:32:51', 'LEVEL' : 'INFO', 'CLASS' : 'test.common.logging.App', 'Module' : '[main]', 'message' : 'Info log message', 'error' : '', 'class' : 'com.test.common.logging.App', 'threadid' : '1', 'threadname' : 'main', 'whatisthis' : 'target/rolling1/rollingtest.log', 'processid' : 'INFOid', 'logdir' : '${sd:type}' 'location' : '/Users/parunkarthick/ferry-commons/common/logging/target/classes/log4j2.xml' 'systemproperty' : '${ctx:key}' [m}
[1;31m{'logged time': '24 Aug 2018 12:32:51', 'LEVEL' : 'ERROR', 'CLASS' : 'test.common.logging.App', 'Module' : '[main]', 'message' : 'Error log message', 'error' : '', 'class' : 'com.test.common.logging.App', 'threadid' : '1', 'threadname' : 'main', 'whatisthis' : 'target/rolling1/rollingtest.log', 'processid' : 'ERRORid', 'logdir' : '${sd:type}' 'location' : '/Users/parunkarthick/ferry-commons/common/logging/target/classes/log4j2.xml' 'systemproperty' : '${ctx:key}' [m}
[1;31m{'logged time': '24 Aug 2018 12:32:51', 'LEVEL' : 'FATAL', 'CLASS' : 'test.common.logging.App', 'Module' : '[main]', 'message' : 'Fatal log message', 'error' : '', 'class' : 'com.test.common.logging.App', 'threadid' : '1', 'threadname' : 'main', 'whatisthis' : 'target/rolling1/rollingtest.log', 'processid' : 'FATALid', 'logdir' : '${sd:type}' 'location' : '/Users/parunkarthick/ferry-commons/common/logging/target/classes/log4j2.xml' 'systemproperty' : '${ctx:key}' [m}
[32m{'logged time': '24 Aug 2018 12:32:51', 'LEVEL' : 'INFO', 'CLASS' : 'test.common.logging.App', 'Module' : '[main]', 'message' : 'Info log message[]', 'error' : '', 'class' : 'com.test.common.logging.App', 'threadid' : '1', 'threadname' : 'main', 'whatisthis' : 'target/rolling1/rollingtest.log', 'processid' : 'INFOid', 'logdir' : '${sd:type}' 'location' : '/Users/parunkarthick/ferry-commons/common/logging/target/classes/log4j2.xml' 'systemproperty' : '${ctx:key}' [m}
[1;31m{'logged time': '24 Aug 2018 12:32:51', 'LEVEL' : 'ERROR', 'CLASS' : 'test.common.logging.App', 'Module' : '[main]', 'message' : 'null exception[]', 'error' : ' java.lang.NullPointerException
at com.test.common.logging.App.main(App.java:69)
', 'class' : 'com.test.common.logging.App', 'threadid' : '1', 'threadname' : 'main', 'whatisthis' : 'target/rolling1/rollingtest.log', 'processid' : 'ERRORid', 'logdir' : '${sd:type}' 'location' : '/Users/parunkarthick/ferry-commons/common/logging/target/classes/log4j2.xml' 'systemproperty' : '${ctx:key}' [m}
如果您看到 lastvalue 系统属性未按预期通过代码值进行替换。
【问题讨论】: