如何在 log4j 中集成编程配置?
How to integrate programmatic configuration in log4j?
我使用的是Log4j version 2.0.1版本,由于组织政策,暂时无法使用更高版本
group:'org.apache.logging.log4j',name:'log4j-core',version:'2.0.1'
group:'org.apache.logging.log4j',name:'log4j-api',version:'2.0.1'
我的日志记录要求
- 如果 customDebug=true 作为环境变量,则启用调试日志记录
- 应该生成 2 个日志记录文件 -- 一个主要日志记录(info/debug 基于上述日志)和一个错误日志记录。
- 有一个基于大小的滚动策略
- 日志文件的目录是动态的。
- 格式应包含特定样式的时间以及线程 ID。
我尝试以下列方式实现 log4j 的编程配置 --
CustomConfigFactory.java >>
@Plugin(name = "CustomConfigFactory", category = "JsonConfigurationFactory")
@Order(10)
public class CustomConfigFactory extends JsonConfigurationFactory {
@Override
public String[] getSupportedTypes() {
return new String[] {".json"};
}
@Override
public Configuration getConfiguration(ConfigurationSource cs) {
try{
String file = Thread.currentThread().getContextClassLoader().getResource("customRollingConfiguration.json").getFile()
cs = new ConfigurationSource(new FileInputStream(new File(file ));
} catch (Exception e){
System.err.println("ERROR : Exception encountered while initiating configuration source: "+e);
}
return new CustomJsonConfiguration(cs);
}
}
CustomJsonConfiguration.java >>
public class CustomJsonConfiguration extends JsonConfiguration {
public CustomJsonConfiguration(final ConfigurationSource configSource) {
super(configSource);
}
@Override
protected void doConfigure(){
try {
ConfigurationSource source = this.getConfigurationSource();
Configuration config = CustomConfigFactory.getInstance().getConfiguration(source);
final Layout layout = PatternLayout.createLayout("%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%5T] %c{1}:%L - %m%n",
config,null,Charset.defaultCharset(),true,false,"","");
SizeBasedTriggeringPolicy policy = SizeBasedTriggeringPolicy.createPolicy("10 MB");
DefaultRolloverStrategy strategy = DefaultRolloverStrategy.createStrategy("50", "5", "min", "9", config);
String logDir = System.getenv("log_dir");
String primaryLogFile = logDir+"/my_main_log";
String primaryLogFilePattern = primaryLogFile+"-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz";
String errorLogFile = logDir+"/my_err_log";
String errorLogFilePattern = errorLogFile+"-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz";
RollingFileAppender appender1 = RollingFileAppender.createAppender(primaryLogFile,
primaryLogFilePattern,"true", "debugFile","true", "false","false",
policy, strategy, layout, null, null, null, null, config);
appender1.start();;
RollingFileAppender appender2 = RollingFileAppender.createAppender(errorLogFile,
errorLogFilePattern,"true","errorFile","true","false","false",
policy, strategy, layout, null,null,null,null, config);
appender2.start();;
config.addAppender(appender1);
config.addAppender(appender2);
LoggerConfig primaryLoggerConfig = null;
if(Boolean.valueOf(System.getenv("customDebug")){
primaryLoggerConfig = new LoggerConfig("aa.bb.cc.dd",Level.DEBUG,false);
} else {
primaryLoggerConfig = new LoggerConfig("aa.bb.cc.dd",Level.INFO,false);
}
LoggerConfig errorLoggerConfig = new LoggerConfig("aa.bb.cc.dd",Level.ERROR,false);
primaryLoggerConfig.addAppender(appender1,null,null);
errorLoggerConfig.addAppender(appender2,null,null);
config.addLogger("aa.bb.cc.dd",primaryLoggerConfig);
config.addLogger("aa.bb.cc.dd",errorLoggerConfig);
LoggerContext context = new LoggerContext("default_context");
context.start(config);
} catch (Exception e){
System.err.println("");
}
}
其中aa.bb.cc.dd指的是下面MyClass的包名。
MyClass.java 将在何处使用此记录器 >>
package aa.bb.cc.dd;
public MyClass {
static final Logger myCustomLog = LogManager.getLogger(MyClass.class);
void doSomething(){
myCustomLog.info("Doing something");
}
}
示例测试方法 作为测试的一部分 class 用于测试上述 class >>>
@Test
public void testDoingSomething(){
MyClass m1 = new MyClass();
m1.doSomething();
}
但以上似乎不起作用,none 的日志文件正在生成,即日志记录不起作用。
我主要参考这个来实现 - -https://logging.apache.org/log4j/2.x/manual/customconfig.html#Hybrid
如果有人能帮我解决这个问题,我将不胜感激。欢迎所有建议!!!
提前致谢。
编辑>>>
以下是合并@VikasSachdeva 的评论后的配置 json。即使记录了错误条件,错误文件仍然是空的 my_main.log
{
"configuration": {
"name": "RollingKitConfig",
"Appenders": {
"appender": [
{
"type": "RollingFile",
"name": "debugFile",
"fileName": "${env:log_dir}/my_main_log",
"filePattern": "${env:log_dir }/my_main_backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz",
"PatternLayout": {
"pattern": "%d{yyyy-MM-dd HH:mm:ss,nnn} [%-5p] [%5t] [%c{3}:%L] - %m%n"
},
"Policies": {
"SizeBasedTriggeringPolicy": {
"size": "10 MB"
}
},
"DefaultRolloverStrategy": {
"max": "10"
},
"ThresholdFilter": {
"level": "${env:customDebug:-info}"
}
},
{
"type": "RollingFile",
"name": "errorFile",
"fileName": "${env:log_dir}/errors.log",
"filePattern": "${env:log_dir}/errors_backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz",
"PatternLayout": {
"pattern": "%d{yyyy-MM-dd HH:mm:ss,nnn} [%-5p] [%5t] [%c{1}:%L] - %m%n"
},
"Policies": {
"SizeBasedTriggeringPolicy": {
"size": "10 MB"
}
},
"DefaultRolloverStrategy": {
"max": "10"
},
"ThresholdFilter": {
"level": "ERROR",
"onMatch": "ACCEPT",
"onMisMatch": "DENY"
}
}
]
},
"Loggers": {
"logger": [
{
"name": "KitLogger",
"level": "info",
"additivity": "false",
"AppenderRef": [
{
"ref": "debugFile"
},
{
"ref": "errorFile"
}
]
}
],
"root": {
"level": "debug",
"appender-ref": {
"ref": "debugFile"
}
}
}
}
}
如果您只想使用环境变量选择日志文件位置,您可以单独使用配置文件,如 log4j2 documentation
中所述
考虑到您的环境变量是 log_dir=D:/logs/
。然后你可以像下面这样更改你的配置 -
{
"configuration": {
"name": "RollingConfig",
"ThresholdFilter": { "level": "info" },
"properties": {
"property": {
"name": "basePath",
"value": "${env:log_dir}"
}
},
"appenders": {
"RollingFile": {
"name": "debugFile",
"fileName": "${basePath}my_main.log",
"filePattern": "${basePath}my_main_%d{yyyyMMdd}.log.gz",
"PatternLayout": { "pattern": "%d{yyyy-MM-dd HH:mm:ss} %-5p [%5T] %c{1}:%L - %m%n" },
"Policies": {
"SizeBasedTriggeringPolicy": { "size": "10 MB" }
},
"DefaultRolloverStrategy": { "max": "10" }
},
"RollingFile": {
"name": "errorFile",
"fileName": "${basePath}my_err.log",,
"filePattern": "${basePath}my_err_%d{yyyyMMdd}.log.gz",
"thresholdFilter": {
"level": "ERROR",
"onMatch": "ACCEPT",
"onMisMatch": "DENY"
},
"PatternLayout": { "pattern": "%d{yyyy-MM-dd HH:mm:ss} %-5p [%5T] %c{1}:%L - %m%n" },
"Policies": {
"SizeBasedTriggeringPolicy": { "size": "10 MB" }
},
"DefaultRolloverStrategy": {"max": "10"}
}
},
"loggers": {
"logger": {
"name": "mainLogger",
"level": "info",
"additivity": "false",
"appender-ref": [
{"ref": "debugFile"},
{ "ref":"errorFile"}
]
},
"root": {
"level": "info",
"appender-ref": [
{"ref": "debugFile" },
{ "ref":"errorFile" }
]
}
}
}
}
我使用的是Log4j version 2.0.1版本,由于组织政策,暂时无法使用更高版本
group:'org.apache.logging.log4j',name:'log4j-core',version:'2.0.1'
group:'org.apache.logging.log4j',name:'log4j-api',version:'2.0.1'
我的日志记录要求
- 如果 customDebug=true 作为环境变量,则启用调试日志记录
- 应该生成 2 个日志记录文件 -- 一个主要日志记录(info/debug 基于上述日志)和一个错误日志记录。
- 有一个基于大小的滚动策略
- 日志文件的目录是动态的。
- 格式应包含特定样式的时间以及线程 ID。
我尝试以下列方式实现 log4j 的编程配置 --
CustomConfigFactory.java >>
@Plugin(name = "CustomConfigFactory", category = "JsonConfigurationFactory")
@Order(10)
public class CustomConfigFactory extends JsonConfigurationFactory {
@Override
public String[] getSupportedTypes() {
return new String[] {".json"};
}
@Override
public Configuration getConfiguration(ConfigurationSource cs) {
try{
String file = Thread.currentThread().getContextClassLoader().getResource("customRollingConfiguration.json").getFile()
cs = new ConfigurationSource(new FileInputStream(new File(file ));
} catch (Exception e){
System.err.println("ERROR : Exception encountered while initiating configuration source: "+e);
}
return new CustomJsonConfiguration(cs);
}
}
CustomJsonConfiguration.java >>
public class CustomJsonConfiguration extends JsonConfiguration {
public CustomJsonConfiguration(final ConfigurationSource configSource) {
super(configSource);
}
@Override
protected void doConfigure(){
try {
ConfigurationSource source = this.getConfigurationSource();
Configuration config = CustomConfigFactory.getInstance().getConfiguration(source);
final Layout layout = PatternLayout.createLayout("%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%5T] %c{1}:%L - %m%n",
config,null,Charset.defaultCharset(),true,false,"","");
SizeBasedTriggeringPolicy policy = SizeBasedTriggeringPolicy.createPolicy("10 MB");
DefaultRolloverStrategy strategy = DefaultRolloverStrategy.createStrategy("50", "5", "min", "9", config);
String logDir = System.getenv("log_dir");
String primaryLogFile = logDir+"/my_main_log";
String primaryLogFilePattern = primaryLogFile+"-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz";
String errorLogFile = logDir+"/my_err_log";
String errorLogFilePattern = errorLogFile+"-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz";
RollingFileAppender appender1 = RollingFileAppender.createAppender(primaryLogFile,
primaryLogFilePattern,"true", "debugFile","true", "false","false",
policy, strategy, layout, null, null, null, null, config);
appender1.start();;
RollingFileAppender appender2 = RollingFileAppender.createAppender(errorLogFile,
errorLogFilePattern,"true","errorFile","true","false","false",
policy, strategy, layout, null,null,null,null, config);
appender2.start();;
config.addAppender(appender1);
config.addAppender(appender2);
LoggerConfig primaryLoggerConfig = null;
if(Boolean.valueOf(System.getenv("customDebug")){
primaryLoggerConfig = new LoggerConfig("aa.bb.cc.dd",Level.DEBUG,false);
} else {
primaryLoggerConfig = new LoggerConfig("aa.bb.cc.dd",Level.INFO,false);
}
LoggerConfig errorLoggerConfig = new LoggerConfig("aa.bb.cc.dd",Level.ERROR,false);
primaryLoggerConfig.addAppender(appender1,null,null);
errorLoggerConfig.addAppender(appender2,null,null);
config.addLogger("aa.bb.cc.dd",primaryLoggerConfig);
config.addLogger("aa.bb.cc.dd",errorLoggerConfig);
LoggerContext context = new LoggerContext("default_context");
context.start(config);
} catch (Exception e){
System.err.println("");
}
}
其中aa.bb.cc.dd指的是下面MyClass的包名。
MyClass.java 将在何处使用此记录器 >>
package aa.bb.cc.dd;
public MyClass {
static final Logger myCustomLog = LogManager.getLogger(MyClass.class);
void doSomething(){
myCustomLog.info("Doing something");
}
}
示例测试方法 作为测试的一部分 class 用于测试上述 class >>>
@Test
public void testDoingSomething(){
MyClass m1 = new MyClass();
m1.doSomething();
}
但以上似乎不起作用,none 的日志文件正在生成,即日志记录不起作用。 我主要参考这个来实现 - -https://logging.apache.org/log4j/2.x/manual/customconfig.html#Hybrid
如果有人能帮我解决这个问题,我将不胜感激。欢迎所有建议!!!
提前致谢。
编辑>>> 以下是合并@VikasSachdeva 的评论后的配置 json。即使记录了错误条件,错误文件仍然是空的 my_main.log
{
"configuration": {
"name": "RollingKitConfig",
"Appenders": {
"appender": [
{
"type": "RollingFile",
"name": "debugFile",
"fileName": "${env:log_dir}/my_main_log",
"filePattern": "${env:log_dir }/my_main_backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz",
"PatternLayout": {
"pattern": "%d{yyyy-MM-dd HH:mm:ss,nnn} [%-5p] [%5t] [%c{3}:%L] - %m%n"
},
"Policies": {
"SizeBasedTriggeringPolicy": {
"size": "10 MB"
}
},
"DefaultRolloverStrategy": {
"max": "10"
},
"ThresholdFilter": {
"level": "${env:customDebug:-info}"
}
},
{
"type": "RollingFile",
"name": "errorFile",
"fileName": "${env:log_dir}/errors.log",
"filePattern": "${env:log_dir}/errors_backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz",
"PatternLayout": {
"pattern": "%d{yyyy-MM-dd HH:mm:ss,nnn} [%-5p] [%5t] [%c{1}:%L] - %m%n"
},
"Policies": {
"SizeBasedTriggeringPolicy": {
"size": "10 MB"
}
},
"DefaultRolloverStrategy": {
"max": "10"
},
"ThresholdFilter": {
"level": "ERROR",
"onMatch": "ACCEPT",
"onMisMatch": "DENY"
}
}
]
},
"Loggers": {
"logger": [
{
"name": "KitLogger",
"level": "info",
"additivity": "false",
"AppenderRef": [
{
"ref": "debugFile"
},
{
"ref": "errorFile"
}
]
}
],
"root": {
"level": "debug",
"appender-ref": {
"ref": "debugFile"
}
}
}
}
}
如果您只想使用环境变量选择日志文件位置,您可以单独使用配置文件,如 log4j2 documentation
中所述考虑到您的环境变量是 log_dir=D:/logs/
。然后你可以像下面这样更改你的配置 -
{
"configuration": {
"name": "RollingConfig",
"ThresholdFilter": { "level": "info" },
"properties": {
"property": {
"name": "basePath",
"value": "${env:log_dir}"
}
},
"appenders": {
"RollingFile": {
"name": "debugFile",
"fileName": "${basePath}my_main.log",
"filePattern": "${basePath}my_main_%d{yyyyMMdd}.log.gz",
"PatternLayout": { "pattern": "%d{yyyy-MM-dd HH:mm:ss} %-5p [%5T] %c{1}:%L - %m%n" },
"Policies": {
"SizeBasedTriggeringPolicy": { "size": "10 MB" }
},
"DefaultRolloverStrategy": { "max": "10" }
},
"RollingFile": {
"name": "errorFile",
"fileName": "${basePath}my_err.log",,
"filePattern": "${basePath}my_err_%d{yyyyMMdd}.log.gz",
"thresholdFilter": {
"level": "ERROR",
"onMatch": "ACCEPT",
"onMisMatch": "DENY"
},
"PatternLayout": { "pattern": "%d{yyyy-MM-dd HH:mm:ss} %-5p [%5T] %c{1}:%L - %m%n" },
"Policies": {
"SizeBasedTriggeringPolicy": { "size": "10 MB" }
},
"DefaultRolloverStrategy": {"max": "10"}
}
},
"loggers": {
"logger": {
"name": "mainLogger",
"level": "info",
"additivity": "false",
"appender-ref": [
{"ref": "debugFile"},
{ "ref":"errorFile"}
]
},
"root": {
"level": "info",
"appender-ref": [
{"ref": "debugFile" },
{ "ref":"errorFile" }
]
}
}
}
}