这是我的log4j2 json config
{
"Configuration": {
"Appenders": {
"Console": {
"PatternLayout": {
"pattern": "%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n"
},
"name": "Console",
"target": "SYSTEM_OUT"
},
"RollingFile": {
"name": "general",
"fileName": "C:/logs/simulator-log.log",
"filePattern": "C:/logs/simulator-log-%d{yyyy-MM-dd HH-mm-ss}.log",
"PatternLayout": {
"pattern": "%msg%n"
},
"Policies": {
"OnStartupTriggeringPolicy": {
}
}
},
"File": {
"PatternLayout": {
"pattern":"%msg%n"
},
"name": "test",
"fileName": "c:/logs/requests_received.log"
},
"File": {
"PatternLayout": {
"pattern":"%msg%n"
},
"name": "test1",
"fileName": "c:/logs/response_sent.log"
}
},
"Loggers": {
"Root": {
"AppenderRef": [
{
"ref": "Console"
}
],
"level": "trace"
},
"logger": [
{
"name": "test",
"level": "trace",
"additivity":"false",
"AppenderRef": {
"ref": "test"
}
},
{
"name": "test1",
"level": "trace",
"additivity":"false",
"AppenderRef": {
"ref": "test1"
}
},
{
"name": "general",
"level": "trace",
"additivity":"false",
"AppenderRef": {
"ref": "general"
}
}
]
}
}
}
当我使用一个文件追加器时,它工作正常。当我使用两个文件appender在title.cant中提到错误时,我们添加了多个文件appender。我不想使用滚动文件追加器。
用于创建记录器变量的Java代码
private static final Logger requestsReceived = LogManager.getLogger("test");
private static final Logger responseSent = LogManager.getLogger("test1");
private static final Logger logger = LogManager.getLogger("general");
答案 0 :(得分:2)
尝试使用type
属性定义您的appender,如下所示 -
{
"type": "File",
"PatternLayout": {
"pattern": "%msg%n"
},
"name": "test",
"fileName": "c:/logs/requests_received.log"
},
{
"type": "File",
"PatternLayout": {
"pattern": "%msg%n"
},
"name": "test1",
"fileName": "c:/logs/response_sent.log"
}
为此,您必须使用type
属性修改所有appender配置。您的完整配置文件将如下所示更改 -
{
"Configuration": {
"Appenders": {
"appender": [
{
"type": "Console",
"PatternLayout": {
"pattern": "%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n"
},
"name": "Console",
"target": "SYSTEM_OUT"
},
{
"type": "RollingFile",
"name": "general",
"fileName": "C:/logs/simulator-log.log",
"filePattern": "C:/logs/simulator-log-%d{yyyy-MM-dd HH-mm-ss}.log",
"PatternLayout": {
"pattern": "%msg%n"
},
"Policies": {
"OnStartupTriggeringPolicy": {
}
}
},
{
"type": "File",
"PatternLayout": {
"pattern": "%msg%n"
},
"name": "test",
"fileName": "c:/logs/requests_received.log"
},
{
"type": "File",
"PatternLayout": {
"pattern": "%msg%n"
},
"name": "test1",
"fileName": "c:/logs/response_sent.log"
}
]
},
"Loggers": {
"Root": {
"AppenderRef": [
{
"ref": "Console"
}
],
"level": "trace"
},
"logger": [
{
"name": "test",
"level": "trace",
"additivity": "false",
"AppenderRef": {
"ref": "test"
}
},
{
"name": "test1",
"level": "trace",
"additivity": "false",
"AppenderRef": {
"ref": "test1"
}
},
{
"name": "general",
"level": "trace",
"additivity": "false",
"AppenderRef": {
"ref": "general"
}
}
]
}
}
}