问题描述
我正在尝试仅使用和参考。我正在使用的代码如下:
I'm trying to configure and set up Log4j2 only through using ConfigurationFactory
and this reference. The code I'm using is as follows:
public class LoggingConfiguration {
public static final String PATTERN_LAYOUT = "[%d] [%t] [%-5level] - %msg (%logger{1}:%L) %n%throwable";
public static final String LOG_FILE_NAME = "app.log";
public static final String LOG_FILE_NAME_PATTERN = LOG_FILE_NAME + "-yyyy.MM.dd";
static {
ConfigurationFactory.setConfigurationFactory(new Log4j2ConfigurationFactory());
}
/**
* Just to make JVM visit this class to initialize the static parts.
*/
public static void configure() {
}
@Plugin(category = "ConfigurationFactory", name = "Log4j2ConfigurationFactory")
@Order(0)
public static class Log4j2ConfigurationFactory extends ConfigurationFactory {
@Override
protected String[] getSupportedTypes() {
return null;
}
@Override
public Configuration getConfiguration(ConfigurationSource source) {
return new Log4j2Configuration();
}
@Override
public Configuration getConfiguration(String name, URI configLocation) {
return new Log4j2Configuration();
}
}
private static class Log4j2Configuration extends DefaultConfiguration {
public Log4j2Configuration() {
setName("app-log4j2");
String root = System.getProperty("APP_ROOT", "/tmp");
if (!root.endsWith("/")) {
root += "/";
}
// MARKER
Layout<? extends Serializable> layout = PatternLayout.createLayout(PATTERN_LAYOUT, null, null, null, null);
String oneDay = TimeUnit.DAYS.toMillis(1) + "";
String oneMB = (1024 * 1024) + "";
final TimeBasedTriggeringPolicy timeBasedTriggeringPolicy = TimeBasedTriggeringPolicy.createPolicy(oneDay,
"true");
final SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy = SizeBasedTriggeringPolicy.createPolicy(oneMB);
final CompositeTriggeringPolicy policy = CompositeTriggeringPolicy.createPolicy(timeBasedTriggeringPolicy,
sizeBasedTriggeringPolicy);
final DefaultRolloverStrategy strategy = DefaultRolloverStrategy.createStrategy("7", "1", null,
Deflater.DEFAULT_COMPRESSION + "", this);
Appender appender = RollingFileAppender.createAppender(root + LOG_FILE_NAME, LOG_FILE_NAME_PATTERN, "true",
"app-log-file-appender", "true", "true", policy, strategy, layout, null, null, null, null, null);
addAppender(appender);
getRootLogger().addAppender(appender, Level.INFO, null);
}
}
}
请注意
- 它扩展
BaseConfiguration
已默认配置控制台 - 它尝试将滚动文件追加器添加到根记录器
- it extends
BaseConfiguration
that already configures console by default - it tries to add a rolling file appender to the root logger
我得到以下异常:
Exception in thread "main" java.lang.IllegalStateException: Pattern does not contain a date
at org.apache.logging.log4j.core.appender.rolling.PatternProcessor.getNextTime(PatternProcessor.java:91)
at org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy.initialize(TimeBasedTriggeringPolicy.java:49)
at org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy.initialize(CompositeTriggeringPolicy.java:43)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.<init>(RollingFileManager.java:58)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager$RollingFileManagerFactory.createManager(RollingFileManager.java:297)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager$RollingFileManagerFactory.createManager(RollingFileManager.java:267)
at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:71)
at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:65)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.getFileManager(RollingFileManager.java:78)
at org.apache.logging.log4j.core.appender.RollingFileAppender.createAppender(RollingFileAppender.java:175)
at com.narmnevis.papyrus.LoggingConfiguration$Log4j2Configuration.<init>(LoggingConfiguration.java:79)
at com.narmnevis.papyrus.LoggingConfiguration$Log4j2ConfigurationFactory.getConfiguration(LoggingConfiguration.java:55)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:377)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:149)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:85)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:34)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:200)
at org.slf4j.helpers.Log4jLoggerFactory$PrivateManager.getContext(Log4jLoggerFactory.java:104)
at org.slf4j.helpers.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:90)
at org.slf4j.helpers.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:46)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:270)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
at com.narmnevis.papyrus.Main.<init>(Main.java:12)
at com.narmnevis.papyrus.Main.main(Main.java:21)
如果我发表评论o在上面的代码中输出 MARKER
之后的代码,它可以工作,但似乎我错过了配置滚动文件appender的东西。我该怎么做才能解决这个问题?
If I comment out the code after MARKER
in above code, it works but it seems that I'm missing something to configure a rolling file appender. What should I do to fix this?
推荐答案
在 log4j 2.x 中你必须指定以这种方式的日期格式
In log4j 2.x you have to specify the date format in this way
public static final String LOG_FILE_NAME_PATTERN = LOG_FILE_NAME + "-%d{dd-MM-yyy}";
-
%
标记格式的开头 -
d
表示它是日期格式(您也可以使用日期
) - 在大括号
{}
中定义格式化程序的选项。在这种情况下,日期格式。您可以使用接受的所有内容。 %
marks the beginning of a formatd
means that it is a date format (you can also usedate
)- within the curly braces
{}
you define the formatter's options. In this case the date format. You can use everything that a SimpleDateFormat would accept. -
%d {ABSOLUTE}
- >HH:mm:ss,SSS
-
%d {COMPACT}
- >yyyyMMddHHmmssSSS
-
%d {DATE}
- >dd MMM yyyy HH:mm:ss,SSS
-
%d {ISO8601_BASIC}
- >yyyyMMdd HHmmss,SSS
-
%d {ISO8601}
- >yyyy-MM-dd HH:mm:ss,SSS
%d{ABSOLUTE}
->HH:mm:ss,SSS
%d{COMPACT}
->yyyyMMddHHmmssSSS
%d{DATE}
->dd MMM yyyy HH:mm:ss,SSS
%d{ISO8601_BASIC}
->yyyyMMdd HHmmss,SSS
%d{ISO8601}
->yyyy-MM-dd HH:mm:ss,SSS
此外,您还可以使用:
注意:此信息基于log4j 2.0-beta9(当前版本)。由于它是测试版,因此可能略有变化。
Note: This information is based on log4j 2.0-beta9 (the current release). Since it is a beta version it might change slightly.
这篇关于使用ConfigurationFactory以编程方式配置log4j2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!