首页 > 解决方案 > Log4j2 - 写入在 XML 中声明的特定记录器

问题描述

我试图配置 Log4j2 几个小时,但无法解决这个问题。

我想在我的应用程序中使用两个单独的记录器(每个类都可以使用它们)。一个记录器的行为类似于 deafult(在某处记录),但第二个记录器将仅收集一些数据。

现在我有这样的想法:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

...

private static final Logger logger = LogManager.getLogger(ProductAttributesVerifier.class);
private static final Logger validationLoger = LogManager.getLogger("validation");

...

logger.warn("Started validation (in basic logger)");
validationLoger.info("INF Started validation of \"" + product.name + "\"");
validationLoger.warn("WRN Started validation of \"" + product.name + "\"");
validationLoger.error("ERR Started validation of \"" + product.name + "\"");

来自的内容validationLogger必须转到另一个文件(其中的内容logger不能出现。

我的 log4j2.xml (它在项目的根目录中):

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <File name="validation" fileName="validation.log">
            <PatternLayout pattern="%d %p %c{1.} [%t] %m %ex%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Logger name="validation" level="warn">
            <AppenderRef ref="validation"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

不幸的是,文件“validation.log”没有出现,所有日志都打印到控制台中。我究竟做错了什么?


编辑:

日志来自<Configuration level="trace">

2019-06-10 14:36:48,507 main DEBUG Apache Log4j Core 2.11.2 initializing configuration XmlConfiguration[location=...................\src\main\resources\log4j2.xml]
2019-06-10 14:36:48,514 main DEBUG Installed 1 script engine
2019-06-10 14:36:48,808 main DEBUG Oracle Nashorn version: 1.8.0_201, language: ECMAScript, threading: Not Thread Safe, compile: true, names: [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript], factory class: jdk.nashorn.api.scripting.NashornScriptEngineFactory
2019-06-10 14:36:48,809 main DEBUG PluginManager 'Core' found 117 plugins
2019-06-10 14:36:48,809 main DEBUG PluginManager 'Level' found 0 plugins
2019-06-10 14:36:48,813 main DEBUG PluginManager 'Lookup' found 13 plugins
2019-06-10 14:36:48,815 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2019-06-10 14:36:48,825 main TRACE TypeConverterRegistry initializing.
2019-06-10 14:36:48,826 main DEBUG PluginManager 'TypeConverter' found 26 plugins
2019-06-10 14:36:48,840 main DEBUG PatternLayout$Builder(pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n", PatternSelector=null, Configuration(..................\src\main\resources\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2019-06-10 14:36:48,840 main DEBUG PluginManager 'Converter' found 47 plugins
2019-06-10 14:36:48,841 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
2019-06-10 14:36:48,850 main DEBUG ConsoleAppender$Builder(target="SYSTEM_OUT", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n), name="Console", Configuration(......................\src\main\resources\log4j2.xml), Filter=null, ={})
2019-06-10 14:36:48,852 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false
2019-06-10 14:36:48,853 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2019-06-10 14:36:48,854 main DEBUG PatternLayout$Builder(pattern="%d %p %c{1.} [%t] %m %ex%n", PatternSelector=null, Configuration(.............................\src\main\resources\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2019-06-10 14:36:48,870 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.FileAppender].
2019-06-10 14:36:48,873 main DEBUG FileAppender$Builder(fileName="validation.log", append="null", locking="null", advertise="null", advertiseUri="null", createOnDemand="null", filePermissions="null", fileOwner="null", fileGroup="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d %p %c{1.} [%t] %m %ex%n), name="validation", Configuration(..................\src\main\resources\log4j2.xml), Filter=null, ={})
2019-06-10 14:36:48,875 main DEBUG Starting FileManager validation.log
2019-06-10 14:36:48,877 main DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
2019-06-10 14:36:48,878 main DEBUG createAppenders(={Console, validation})
2019-06-10 14:36:48,879 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2019-06-10 14:36:48,883 main DEBUG createAppenderRef(ref="validation", level="null", Filter=null)
2019-06-10 14:36:48,883 main DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
2019-06-10 14:36:48,886 main DEBUG createLogger(additivity="true", level="WARN", name="validation", includeLocation="null", ={validation}, ={}, Configuration (..........................\src\main\resources\log4j2.xml), Filter=null)
2019-06-10 14:36:48,888 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2019-06-10 14:36:48,889 main DEBUG createAppenderRef(ref="Console", level="null", Filter=null)
2019-06-10 14:36:48,889 main DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
2019-06-10 14:36:48,891 main DEBUG createLogger(additivity="null", level="ERROR", includeLocation="null", ={Console}, ={}, Configuration(.........................\src\main\resources\log4j2.xml), Filter=null)
2019-06-10 14:36:48,891 main DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
2019-06-10 14:36:48,892 main DEBUG createLoggers(={validation, root})
2019-06-10 14:36:48,892 main DEBUG Configuration XmlConfiguration[location=.......................\src\main\resources\log4j2.xml] initialized
2019-06-10 14:36:48,893 main DEBUG Starting configuration XmlConfiguration[location=.......................\src\main\resources\log4j2.xml]
2019-06-10 14:36:48,893 main DEBUG Started configuration XmlConfiguration[location=.......................\src\main\resources\log4j2.xml] OK.
2019-06-10 14:36:48,893 main TRACE Stopping org.apache.logging.log4j.core.config.DefaultConfiguration@60215eee...
2019-06-10 14:36:48,894 main TRACE DefaultConfiguration notified 1 ReliabilityStrategies that config will be stopped.
2019-06-10 14:36:48,894 main TRACE DefaultConfiguration stopping root LoggerConfig.
2019-06-10 14:36:48,894 main TRACE DefaultConfiguration notifying ReliabilityStrategies that appenders will be stopped.
2019-06-10 14:36:48,894 main TRACE DefaultConfiguration stopping remaining Appenders.
2019-06-10 14:36:48,895 main DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
2019-06-10 14:36:48,895 main DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false-1, all resources released: true
2019-06-10 14:36:48,895 main DEBUG Appender DefaultConsole-1 stopped with status true
2019-06-10 14:36:48,895 main TRACE DefaultConfiguration stopped 1 remaining Appenders.
2019-06-10 14:36:48,896 main TRACE DefaultConfiguration cleaning Appenders from 1 LoggerConfigs.
2019-06-10 14:36:48,896 main DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@60215eee OK
2019-06-10 14:36:48,942 main TRACE Reregistering MBeans after reconfigure. Selector=org.apache.logging.log4j.core.selector.ClassLoaderContextSelector@707194ba
2019-06-10 14:36:48,943 main TRACE Reregistering context (1/1): '2a139a55' org.apache.logging.log4j.core.LoggerContext@1190200a
2019-06-10 14:36:48,943 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55'
2019-06-10 14:36:48,944 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=StatusLogger'
2019-06-10 14:36:48,944 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=ContextSelector'
2019-06-10 14:36:48,945 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=Loggers,name=*'
2019-06-10 14:36:48,945 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=Appenders,name=*'
2019-06-10 14:36:48,946 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=AsyncAppenders,name=*'
2019-06-10 14:36:48,947 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=AsyncLoggerRingBuffer'
2019-06-10 14:36:48,947 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=2a139a55,component=Loggers,name=*,subtype=RingBuffer'
2019-06-10 14:36:48,948 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55
2019-06-10 14:36:48,950 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55,component=StatusLogger
2019-06-10 14:36:48,952 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55,component=ContextSelector
2019-06-10 14:36:48,953 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55,component=Loggers,name=
2019-06-10 14:36:48,954 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55,component=Loggers,name=validation
2019-06-10 14:36:48,955 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55,component=Appenders,name=Console
2019-06-10 14:36:48,956 main DEBUG Registering MBean org.apache.logging.log4j2:type=2a139a55,component=Appenders,name=validation
2019-06-10 14:36:48,958 main TRACE Using default SystemClock for timestamps.
2019-06-10 14:36:48,959 main DEBUG org.apache.logging.log4j.core.util.SystemClock does not support precise timestamps.
2019-06-10 14:36:48,959 main TRACE Using DummyNanoClock for nanosecond timestamps.
2019-06-10 14:36:48,959 main DEBUG Reconfiguration complete for context[name=2a139a55] at URI .......................\src\main\resources\log4j2.xml (org.apache.logging.log4j.core.LoggerContext@1190200a) with optional ClassLoader: null
2019-06-10 14:36:48,959 main DEBUG Shutdown hook enabled. Registering a new one.
2019-06-10 14:36:48,960 main DEBUG LoggerContext[name=2a139a55, org.apache.logging.log4j.core.LoggerContext@1190200a] started OK.
2019-06-10 14:36:49,189 main DEBUG Using configurationFactory org.apache.logging.log4j.core.config.ConfigurationFactory$Factory@14bdbc74
2019-06-10 14:36:49,190 main INFO Log4j appears to be running in a Servlet environment, but there's no log4j-web module available. If you want better web container support, please add the log4j-web JAR to your web archive or server lib directory.
2019-06-10 14:36:49,190 main DEBUG Closing JarURLInputStream sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@5a7fe64f

标签: javalogginglog4j2

解决方案


我的配置(有问题)是正确的。

重新启动计算机(和 Eclipse IDE)后一切正常。不幸的是,我不知道为什么,但我会为未来的读者发布这个答案。


推荐阅读