我的log4j 2.9.x有严重问题
我写“严重”是因为log4j的文档简直就是废话!
此版本没有清晰,最新的文档。即使是现有的文档代码也不会通过编译器,因为它们会不时地破坏更改,并且您无法确定它们所记录的版本。我整天用Google搜寻,但是我发现的大多数东西也已经过时了。
所以我有点以编程方式配置log4j2感到困惑。
我要实现的目标:
1)始终独立于当前日志级别独立打印到控制台的其他“日志级别”(模拟人生级别)
2)登录到控制台并归档
3)在运行时确定文件名
我找到了一种使用XML实现1)和2)的方法。我还没有找到适合3)的方法。我不希望为文件名使用静态上下文或系统变量,因为我期望并发问题。
因此,我的方法是以编程方式配置log4j。这是我的配置工厂:
package my.abc
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.core.Filter
import org.apache.logging.log4j.core.LoggerContext
import org.apache.logging.log4j.core.config.Configuration
import org.apache.logging.log4j.core.config.ConfigurationFactory
import org.apache.logging.log4j.core.config.ConfigurationSource
import org.apache.logging.log4j.core.config.Order
import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder
import org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration
import org.apache.logging.log4j.core.config.plugins.Plugin
@Plugin(name = "CustomConfigurationFactory", category = ConfigurationFactory.CATEGORY)
@Order(50)
class CustomConfigurationFactory extends ConfigurationFactory {
private static String MESSAGE_PATTERN = "%d{HH:mm:ss.SSS} [%t] %-5level 123 %logger{36} - %msg%n"
Level logLevel
String packageToScan
CustomConfigurationFactory(Level logLevel, String packageToScan) {
this.logLevel = logLevel
this.packageToScan = packageToScan
}
private Configuration createConfiguration(final String name, ConfigurationBuilder<BuiltConfiguration> builder) {
builder.setConfigurationName(name)
builder.setStatusLevel(Level.ERROR)
//add appender
builder.add(createConsoleAppender(builder))
builder.add(createLifecycleAppender(builder))
//add logger
builder.add(createLogger(builder))
// builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.DENY)
// .addAttribute("level", logLevel))
builder.add(builder.newRootLogger(Level.TRACE).add(builder.newAppenderRef("stdout")))
return builder.build()
}
@Override
Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
return getConfiguration(loggerContext, source.toString(), null)
}
@Override
Configuration getConfiguration(final LoggerContext loggerContext, final String name, final URI configLocation) {
ConfigurationBuilder<BuiltConfiguration> builder = newConfigurationBuilder()
return createConfiguration(name, builder)
}
@Override
protected String[] getSupportedTypes() {
return [{ "*" }]
}
private AppenderComponentBuilder createConsoleAppender(ConfigurationBuilder<BuiltConfiguration> builder) {
AppenderComponentBuilder appenderBuilder = builder.newAppender("stdout", "CONSOLE")
appenderBuilder.add(builder.newLayout("PatternLayout").
addAttribute("pattern", MESSAGE_PATTERN))
appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY,
Filter.Result.NEUTRAL).addAttribute("marker", "lifecycle"))
// appenderBuilder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT,
// Filter.Result.DENY).addAttribute("level", logLevel))
return appenderBuilder
}
private AppenderComponentBuilder createLifecycleAppender(ConfigurationBuilder<BuiltConfiguration> builder) {
AppenderComponentBuilder appenderBuilder = builder.newAppender("lifecycle", "CONSOLE")
appenderBuilder.add(builder.newLayout("PatternLayout").
addAttribute("pattern", "%d{HH:mm:ss.SSS} [%t] LIFECYCLE %logger{36} - %msg%n"))
appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.ACCEPT, Filter.Result.DENY)
.addAttribute("marker", "lifecycle"))
return appenderBuilder
}
private LoggerComponentBuilder createLogger(ConfigurationBuilder<BuiltConfiguration> builder) {
builder.newLogger(packageToScan, "trace")
.add(builder.newAppenderRef("stdout"))
.add(builder.newAppenderRef("lifecycle"))
.addAttribute("additivity", false)
}
}
您可能会注意到2条注释过的行。多数民众赞成在我的问题。
当我使用
builder.add(builder.newFilter("ThresholdFilter"...
时,所有日志均不符合日志级别时将被过滤掉。即使在任何情况下,我都希望记录“生命周期”日志。当我使用
appenderBuilder.add(builder.newFilter("ThresholdFilter...
我收到错误消息2017-11-23 12:12:34,905 main ERROR appender CONSOLE has no parameter that matches element ThresholdFilter
什么是奇怪的。因为当我使用xml配置所有内容时,我可以完全做到这一点(将thresholdfilter添加到单个追加程序中)并且可以正常工作。
<Appender type="Console" name="STDOUT" >
<Filters>
<Filter type="MarkerFilter" marker="lifecycle" onMatch="DENY" onMismatch="NEUTRAL"/>
<Filter type="ThresholdFilter" level="${logLevel}" onMatch="ACCEPT" onMismatch="DENY" />
</Filters>
<Layout type="PatternLayout" pattern="${pattern}"/>
</Appender>
要在此处完成,可满足要求1)和2)的整个xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" strict="true" name="XMLConfigTest"
packages="org.apache.logging.log4j.test">
<Properties>
<Property name="logLevel">error</Property>
<Property name="filename">target/test.log</Property>
<Property name="pattern">%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Property>
</Properties>
<Appenders>
<Appender type="Console" name="STDOUT">
<Filters>
<Filter type="MarkerFilter" marker="lifecycle" onMatch="DENY" onMismatch="NEUTRAL"/>
<Filter type="ThresholdFilter" level="${logLevel}" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Layout type="PatternLayout" pattern="${pattern}"/>
</Appender>
<Appender type="Console" name="LIFECYCLE">
<Filters>
<Filter type="MarkerFilter" marker="lifecycle" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Layout type="PatternLayout" pattern="%d{HH:mm:ss.SSS} [%t] LIFECYCLE %logger{36} - %msg%n"/>
</Appender>
<Appender type="File" name="File" fileName="${filename}">
<Layout type="PatternLayout">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</Layout>
</Appender>
</Appenders>
<Loggers>
<Logger name="my.abc" level="trace" additivity="false">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="LIFECYCLE"/>
</Logger>
<Root level="error">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
必须有一种将ThresholdFilter添加到附加程序的方法。我找到了ThresholdFilter类。但是我绝对不知道如何使用它。实例化
ThresholdFilter.createFilter(...)
,但是我可以在哪里添加呢?简而言之..
如何在没有xml的情况下将ThresholdFilter添加到单个Appender?
最佳答案
费耶
我解决所有需求的方法:
我切换回XML并添加了一些动态属性。一些属性取自系统属性。如果不存在,将采用默认值(定义为xml属性)。
对属性“ logFile”有个特殊的外观。它是系统属性(用于日志文件的修复路径)的混合,由log4j动态确定。这并不是真正的线程安全。但是在我们的情况下可以接受。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" strict="true" name="XMLConfig"
packages="org.apache.logging.log4j.test">
<Properties>
<Property name="logLevel">INFO</Property>
<Property name="logFileDir">log/</Property>
<Property name="logLevelFile">OFF</Property>
<Property name="logFile">${sys:logFileDir}ConfigMgmt-${date:yyyyMMdd_HHmmss-SSS}.log</Property>
<Property name="packageName">com.hamburgsud</Property>
<Property name="messagePattern">%d{HH:mm:ss.SSS} (Test) %-5level %logger{1.} - %msg%n</Property>
</Properties>
<Appenders>
<!-- Console Logging -->
<Appender type="Console" name="STDOUT">
<Filters>
<Filter type="MarkerFilter" marker="lifecycle" onMatch="DENY" onMismatch="NEUTRAL"/>
<Filter type="ThresholdFilter" level="${sys:logLevel}" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Layout type="PatternLayout" pattern="${sys:messagePattern}"/>
</Appender>
<!-- LIFECYCLE is analog to gradles lifecycle log level - means always logging
When the marker for lifecycle-logging is added, the message will always be logged -->
<Appender type="Console" name="LIFECYCLE">
<Filters>
<Filter type="MarkerFilter" marker="lifecycle" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Layout type="PatternLayout" pattern="%logger{1.} - %msg%n"/>
</Appender>
<!-- File Logging -->
<Appender type="File" name="FILE" fileName="${sys:logFile}" createOnDemand="true">
<Filters>
<Filter type="MarkerFilter" marker="lifecycle" onMatch="DENY" onMismatch="NEUTRAL"/>
<Filter type="ThresholdFilter" level="${sys:logLevelFile}" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Layout type="PatternLayout" pattern="${sys:messagePattern}"/>
</Appender>
<!-- Counterpart to consoles LIFECYCLE logging. When file logging is activated and a log is marked with
lifecycle, the message will be logged to the file too. -->
<Appender type="File" name="FILELIFECYCLE" fileName="${sys:logFile}" createOnDemand="true">
<Filters>
<Filter type="MarkerFilter" marker="lifecycle" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Layout type="PatternLayout" pattern="%logger{1.} - %msg%n"/>
</Appender>
</Appenders>
<Loggers>
<Root level="trace" additivity="false">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="LIFECYCLE"/>
<AppenderRef ref="FILE"/>
<AppenderRef ref="FILELIFECYCLE"/>
</Root>
</Loggers>
</Configuration>