Log4j2 - Write a specific logger declared in XML


to the friars;

I've been trying to configure Log4j2 for hours but can't solve this.

I want to use two separate loggers in my application (each class can use both). One of the loggers will act like a deafult (a log place), but the second will just collect some data.

Now I have somethink like this:

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 + "\"");

From content validationLoggerhas gone to another file (where from stuff loggercan't appear.

My log4j2.xml (this is in the root of the project):

<?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>

Unfortunately the file "validation.log" does not appear, all logs are printed to the console. What am I doing wrong?


edit:

From the 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 [email protected]ee...
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 [email protected]ee OK
2019-06-10 14:36:48,942 main TRACE Reregistering MBeans after reconfigure. Selector=or[email protected]707194ba
2019-06-10 14:36:48,943 main TRACE Reregistering context (1/1): '2a139a55' [email protected]
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 ([email protected]) 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, [email protected]] started OK.
2019-06-10 14:36:49,189 main DEBUG Using configurationFactory or[email protected]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 [email protected]e64f
to the friars;

My configuration (problem) is correct.

It all started working fine after restarting the computer (and Eclipse IDE). Unfortunately, I don't know why, but I will post this answer to future readers.

Related