我目前正在试用 netflix 的 Blitz4j。一切似乎都运行良好,但由于某种原因,我在日志文件中看到 log4j 不断尝试配置属性(每分钟)。
以下是我对 log4j 的配置:
# logging properties
## root logger options
log4j.rootCategory=OFF
log4j.rootLogger=INFO, stdout, LOGTXT
## stdout, logs show on console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=com.netflix.logging.log4jAdapter.NFPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss} %5p [ %c ] %m%n
## LOGTXT, logs to a file
log4j.appender.LOGTXT=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOGTXT.File=./log/log.txt
log4j.appender.LOGTXT.Append=true
log4j.appender.LOGTXT.Threshold=DEBUG
log4j.appender.LOGTXT.DatePattern='-' yyyy-MM-dd
log4j.appender.LOGTXT.layout=com.netflix.logging.log4jAdapter.NFPatternLayout
log4j.appender.LOGTXT.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss} %5p [ %c ] %m%n
log4j.logger.com.otcmarkets.mfa.common.util.logging.MfaLogFactory=INFO, stdout, LOGTXT
在控制台/标准输出中,每分钟我都会看到:
2013-04-12 06:36:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] Configuring log4j dynamically
2013-04-12 06:36:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] Updated properties is :{log4j.logger.com.otcmarkets.mfa.common.util.logging.MfaLogFactory=INFO, stdout, LOGTXT}
2013-04-12 06:36:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] The root category for log4j.rootCategory now is OFF
2013-04-12 06:36:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] The root category for log4j.rootLogger now is INFO, stdout, LOGTXT
2013-04-12 06:36:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] Configuring log4j with properties :{log4j.appender.stdout=org.apache.log4j.ConsoleAppender, log4j.appender.LOGTXT.Append=true, log4j.rootLogger=INFO, stdout, LOGTXT, log4j.appender.LOGTXT.File=./log/log.txt, log4j.appender.LOGTXT=org.apache.log4j.DailyRollingFileAppender, log4j.appender.LOGTXT.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss} %5p [ %c ] %m%n, log4j.appender.LOGTXT.layout=com.netflix.logging.log4jAdapter.NFPatternLayout, log4j.appender.LOGTXT.Threshold=DEBUG, log4j.loggerFactory=com.netflix.blitz4j.NFCategoryFactory, log4j.logger.com.otcmarkets.mfa.common.util.logging.MfaLogFactory=INFO, stdout, LOGTXT, log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss} %5p [ %c ] %m%n, log4j.appender.LOGTXT.DatePattern='.' yyyy-MM-dd, log4j.appender.stdout.layout=com.netflix.logging.log4jAdapter.NFPatternLayout, log4j.appender.stdout.Target=System.out, log4j.rootCategory=OFF}
log4j: Parsing for [root] with value=[INFO, stdout, LOGTXT].
log4j: Level token is [INFO].
log4j: Category root set to INFO
log4j: Parsing appender named "stdout".
log4j: Parsing layout options for "stdout".
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd hh:mm:ss} %5p [ %c ] %m%n].
log4j: End of parsing for "stdout".
log4j: Setting property [target] to [System.out].
log4j: Parsed "stdout" options.
log4j: Parsing appender named "LOGTXT".
log4j: Parsing layout options for "LOGTXT".
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd hh:mm:ss} %5p [ %c ] %m%n].
log4j: End of parsing for "LOGTXT".
log4j: Setting property [append] to [true].
log4j: Setting property [file] to [./log/log.txt].
log4j: Setting property [threshold] to [DEBUG].
log4j: Setting property [datePattern] to ['.' yyyy-MM-dd].
log4j: setFile called: ./log/log.txt, true
log4j: setFile ended
log4j: Appender [LOGTXT] to be rolled at midnight.
log4j: Parsed "LOGTXT" options.
log4j: Setting category factory to [com.netflix.blitz4j.NFCategoryFactory].
log4j: Parsing for [com.otcmarkets.mfa.common.util.logging.MfaLogFactory] with value=[INFO, stdout, LOGTXT].
log4j: Level token is [INFO].
log4j: Category com.otcmarkets.mfa.common.util.logging.MfaLogFactory set to INFO
log4j: Parsing appender named "stdout".
log4j: Appender "stdout" was already parsed.
log4j: Parsing appender named "LOGTXT".
log4j: Appender "LOGTXT" was already parsed.
log4j: Handling log4j.additivity.com.otcmarkets.mfa.common.util.logging.MfaLogFactory=[null]
log4j: Finished configuring.
在 log.txt 文件中,每分钟我都会看到:
2013-04-12 06:38:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] Configuring log4j dynamically
2013-04-12 06:38:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] Updated properties is :{log4j.logger.com.otcmarkets.mfa.common.util.logging.MfaLogFactory=INFO, stdout, LOGTXT}
2013-04-12 06:38:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] The root category for log4j.rootCategory now is OFF
2013-04-12 06:38:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] The root category for log4j.rootLogger now is INFO, stdout, LOGTXT
2013-04-12 06:38:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] Configuring log4j with properties :{log4j.appender.stdout=org.apache.log4j.ConsoleAppender, log4j.appender.LOGTXT.Append=true, log4j.rootLogger=INFO, stdout, LOGTXT, log4j.appender.LOGTXT.File=./log/log.txt, log4j.appender.LOGTXT=org.apache.log4j.DailyRollingFileAppender, log4j.appender.LOGTXT.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss} %5p [ %c ] %m%n, log4j.appender.LOGTXT.layout=com.netflix.logging.log4jAdapter.NFPatternLayout, log4j.appender.LOGTXT.Threshold=DEBUG, log4j.loggerFactory=com.netflix.blitz4j.NFCategoryFactory, log4j.logger.com.otcmarkets.mfa.common.util.logging.MfaLogFactory=INFO, stdout, LOGTXT, log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss} %5p [ %c ] %m%n, log4j.appender.LOGTXT.DatePattern='.' yyyy-MM-dd, log4j.appender.stdout.layout=com.netflix.logging.log4jAdapter.NFPatternLayout, log4j.appender.stdout.Target=System.out, log4j.rootCategory=OFF}
2013-04-12 06:38:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] Configuring log4j dynamically
2013-04-12 06:38:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] Updated properties is :{log4j.logger.com.otcmarkets.mfa.common.util.logging.MfaLogFactory=INFO, stdout, LOGTXT}
2013-04-12 06:38:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] The root category for log4j.rootCategory now is OFF
2013-04-12 06:38:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] The root category for log4j.rootLogger now is INFO, stdout, LOGTXT
2013-04-12 06:38:36 INFO [ com.netflix.blitz4j.LoggingConfiguration ] Configuring log4j with properties :{log4j.appender.stdout=org.apache.log4j.ConsoleAppender, log4j.appender.LOGTXT.Append=true, log4j.rootLogger=INFO, stdout, LOGTXT, log4j.appender.LOGTXT.File=./log/log.txt, log4j.appender.LOGTXT=org.apache.log4j.DailyRollingFileAppender, log4j.appender.LOGTXT.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss} %5p [ %c ] %m%n, log4j.appender.LOGTXT.layout=com.netflix.logging.log4jAdapter.NFPatternLayout, log4j.appender.LOGTXT.Threshold=DEBUG, log4j.loggerFactory=com.netflix.blitz4j.NFCategoryFactory, log4j.logger.com.otcmarkets.mfa.common.util.logging.MfaLogFactory=INFO, stdout, LOGTXT, log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss} %5p [ %c ] %m%n, log4j.appender.LOGTXT.DatePattern='.' yyyy-MM-dd, log4j.appender.stdout.layout=com.netflix.logging.log4jAdapter.NFPatternLayout, log4j.appender.stdout.Target=System.out, log4j.rootCategory=OFF}
不知何故,它在文件中记录了两次配置内容。
这是我的“日志工厂”:
public class MyLogFactory {
// init the logging configuration instance
private final static LoggingConfiguration logConfig = LoggingConfiguration.getInstance();
static {
/* set up the properties for logging
we will use two forms of logging
1. stdout
2. log file, rollover is every midnight
*/
Properties props = new Properties();
System.out.println("configuring properties for log4j");
// root logger options
props.setProperty("log4j.rootCategory", "OFF");
props.setProperty("log4j.rootLogger", "INFO, stdout, LOGTXT");
// direct log messages to stdout
props.setProperty("log4j.appender.stdout", "org.apache.log4j.ConsoleAppender");
props.setProperty("log4j.appender.stdout.Target", "System.out");
props.setProperty("log4j.appender.stdout.layout", "com.netflix.logging.log4jAdapter.NFPatternLayout");
props.setProperty("log4j.appender.stdout.layout.ConversionPattern", "%d{yyyy-MM-dd hh:mm:ss} %5p [ %c ] %m%n");
// direct log messages to log file
props.setProperty("log4j.appender.LOGTXT", "org.apache.log4j.DailyRollingFileAppender");
props.setProperty("log4j.appender.LOGTXT.File", "./log/log.txt");
props.setProperty("log4j.appender.LOGTXT.Append", "true");
props.setProperty("log4j.appender.LOGTXT.Threshold", "DEBUG");
props.setProperty("log4j.appender.LOGTXT.DatePattern", "'.' yyyy-MM-dd");
props.setProperty("log4j.appender.LOGTXT.layout", "com.netflix.logging.log4jAdapter.NFPatternLayout");
props.setProperty("log4j.appender.LOGTXT.layout.ConversionPattern", "%d{yyyy-MM-dd hh:mm:ss} %5p [ %c ] %m%n");
props.setProperty("log4j.logger.com.otcmarkets.mfa.common.util.logging.MfaLogFactory", "INFO,stdout,LOGTXT");
logConfig.configure(props);
System.out.println("finished configuring properties for log4j");
}
public static Logger getLogger(Class clazz)
{
return LoggerFactory.getLogger(clazz);
}
}