我正在使用 Log4j 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 个日志文件——一个主日志(基于上述日志的信息/调试)和错误日志。
- 有一个基于大小的滚动策略
- 日志文件的目录是动态的。
- 格式应该带有特定样式的时间以及线程 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");
}
}
示例测试方法作为 testng 类的一部分,用于测试上述类 >>>
@Test
public void testDoingSomething(){
MyClass m1 = new MyClass();
m1.doSomething();
}
但以上似乎不起作用,并且没有生成任何日志文件,即日志记录不起作用。我主要参考了这个来实现-- 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"
}
}
}
}
}