I have a following JSON config, which does not roll over after it reaches the max-size limit, rather overrides it.
我有一个以下的JSON配置,它在达到最大大小限制后不会翻转,而是覆盖它。
{
"configuration": {
"name": "cool_log",
"appenders": { "RollingFile": { "name": "rollingFile",
"fileName": "/var/log/app_test.log",
"filePattern": "/var/log/%d{MM-dd-yy-HH-mm-ss}-%i.log.gz",
"JSONLayout": {
"complete": false,
"compact" : true,
"eventEol": true
},
"Policies": { "SizeBasedTriggeringPolicy": { "size": "100 KB" } },
"DefaultRolloverStrategy": { "max": "10" }
}
},
"loggers": {
"root": {
"level": "DEBUG",
"appender-ref": {
"ref": "rollingFile"
}
}
}
}
}
which I created based on this official example,
我根据这个官方例子创建的,
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="var/log/app_test.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="DEBUG">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
The xml
config rolls over perfectly, but only this I'm missing is PatternLayout
, which is JSONLayout
for me.
xml配置完美地滚动,但只有我缺少的是PatternLayout,这对我来说是JSONLayout。
The setup for log4j2 is,
log4j2的设置是,
public static void initialiseLoggingConfiguration() {
try {
PropertiesConfiguration logConfig = new PropertiesConfiguration("app.properties");
System.out.println("Setting up log4j2 " + logConfig.getString("log4j2.config"));
Configurator.initialize(null, logConfig.getString("log4j2.config"));
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
and maven dependencies,
和maven依赖,
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.6.2</version>
</dependency>
1 个解决方案
#1
1
Nevermind,
<Policies>
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
becomes
"SizeBasedTriggeringPolicy": {
"size": "1 KB"
}
So the working log4j2.json is,
所以工作log4j2.json是,
{
"configuration": {
"name": "cool_log",
"appenders": {
"RollingFile": {
"name": "rollingFile",
"fileName": "logservice.log",
"filePattern": "%d{MM-dd-yy-HH-mm-ss}-%i.log.gz",
"JSONLayout": {
"complete": false,
"compact": true,
"eventEol": true
},
"SizeBasedTriggeringPolicy": {
"size": "1 KB"
},
"DefaultRolloverStrategy": {
"max": "10"
}
}
},
"loggers": {
"root": {
"level": "DEBUG",
"appender-ref": {
"ref": "rollingFile"
}
}
}
}
}
Reference
https://logging.apache.org/log4j/2.x/manual/configuration.html
#1
1
Nevermind,
<Policies>
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
becomes
"SizeBasedTriggeringPolicy": {
"size": "1 KB"
}
So the working log4j2.json is,
所以工作log4j2.json是,
{
"configuration": {
"name": "cool_log",
"appenders": {
"RollingFile": {
"name": "rollingFile",
"fileName": "logservice.log",
"filePattern": "%d{MM-dd-yy-HH-mm-ss}-%i.log.gz",
"JSONLayout": {
"complete": false,
"compact": true,
"eventEol": true
},
"SizeBasedTriggeringPolicy": {
"size": "1 KB"
},
"DefaultRolloverStrategy": {
"max": "10"
}
}
},
"loggers": {
"root": {
"level": "DEBUG",
"appender-ref": {
"ref": "rollingFile"
}
}
}
}
}
Reference
https://logging.apache.org/log4j/2.x/manual/configuration.html