《Log4j官方文档》 JSON配置

Log4j不仅能通过XML配置,也能通过JSON配置。如你所知,JSON的格式非常类似于紧凑版XML。每个键代表了插件名称,对应的值是属性。键中可以有子级插件。如下,“ThresholdFilter, Console, PatternLayout”都是插件。Console的值是“STDOUT”,意为日志控制台输出;“ThresholdFilter”的值是被设为调试级。


{ "configuration": { "status": "error", "name": "RoutingTest",
"packages": "org.apache.logging.log4j.test",
"properties": {
"property": { "name": "filename",
"value" : "target/rolling1/rollingtest-$${sd:type}.log" }
},
"ThresholdFilter": { "level": "debug" },
"appenders": {
"Console": { "name": "STDOUT",
"PatternLayout": { "pattern": "%m%n" }
},
"List": { "name": "List",
"ThresholdFilter": { "level": "debug" }
},
"Routing": { "name": "Routing",
"Routes": { "pattern": "$${sd:type}",
"Route": [
{
"RollingFile": {
"name": "Rolling-${sd:type}", "fileName": "${filename}",
"filePattern": "target/rolling1/test1-${sd:type}.%i.log.gz",
"PatternLayout": {"pattern": "%d %p %c{1.} [%t] %m%n"},
"SizeBasedTriggeringPolicy": { "size": "500" }
}
},
{ "AppenderRef": "STDOUT", "key": "Audit"},
{ "AppenderRef": "List", "key": "Service"}
]
}
}
},
"loggers": {
"logger": { "name": "EventLogger", "level": "info", "additivity": "false",
"AppenderRef": { "ref": "Routing" }},
"root": { "level": "error", "AppenderRef": { "ref": "STDOUT" }}
}
}
}

注意,在路径追加器中,“Route”元素被声明为数组,这是可行的,因为每个数组元素都是“Route”。然而,在单纯的追加器和过滤器中就不行了,因为在JSON格式里,每个元素都有不同的名称。只有当追加器和过滤器声明是类型的时候,它们才可以由数组定义。如下展现了通过数组定义了多路logger。

{ “configuration”: { “status”: “debug”, “name”: “RoutingTest”,
“packages”: “org.apache.logging.log4j.test”,
“properties”: {
“property”: { “name”: “filename”,
“value” : “target/rolling1/rollingtest-$${sd:type}.log” }
},
“ThresholdFilter”: { “level”: “debug” },
“appenders”: {
“appender”: [
{ “type”: “Console”, “name”: “STDOUT”, “PatternLayout”: { “pattern”: “%m%n” }},
{ “type”: “List”, “name”: “List”, “ThresholdFilter”: { “level”: “debug” }},
{ “type”: “Routing”, “name”: “Routing”,
“Routes”: { “pattern”: “$${sd:type}”,
“Route”: [
{
“RollingFile”: {
“name”: “Rolling-${sd:type}”, “fileName”: “${filename}”,
“filePattern”: “target/rolling1/test1-${sd:type}.%i.log.gz”,
“PatternLayout”: {“pattern”: “%d %p %c{1.} [%t] %m%n”},
“SizeBasedTriggeringPolicy”: { “size”: “500” }
}
},
{ “AppenderRef”: “STDOUT”, “key”: “Audit”},
{ “AppenderRef”: “List”, “key”: “Service”}
]
}
}
]
},
“loggers”: {
“logger”: [
{ “name”: “EventLogger”, “level”: “info”, “additivity”: “false”,
“AppenderRef”: { “ref”: “Routing” }},
{ “name”: “com.foo.bar”, “level”: “error”, “additivity”: “false”,
“AppenderRef”: { “ref”: “Console” }}
],
“root”: { “level”: “error”, “AppenderRef”: { “ref”: “STDOUT” }}
}
}
}

通过Jackson数据处理器来提供JSON文件的解析支持。你需要给项目添加如下依赖:

com.fasterxml.jackson.core
jackson-core
2.8.3

com.fasterxml.jackson.core
jackson-databind
2.8.3

com.fasterxml.jackson.core
jackson-annotations
2.8.3

原创文章,转载请注明: 转载自并发编程网 – ifeve.com本文链接地址: 《Log4j官方文档》 JSON配置

  • Trackback 关闭
  • 评论 (0)
  1. 暂无评论

return top