0

I have an eclipse RCP Project. There I want to enable logging via log4j2. This is working like expected when I define my appenders in my log4j2.xml

Now I want to configure the logging path depending on some Application Constants.

I found this log4j2 Plugin and created the following class:

    package x.y.z.system;

    import ...;

    @Plugin(name = "LogProperties", category = StrLookup.CATEGORY)
    public class LogPropertiesLookup extends AbstractLookup {

    @Override
    public String lookup(final LogEvent event, final String key) {

        if (key.equals("root_path")) {
            return Paths.get(System.getProperty("user.home"), "xxx", "xxx_" + Application.getAppVersion('_')).toString();
        }

        return new String();

    }

    @Override
    public String lookup(String key) {

        if (key.equals("root_path")) {
            return Paths.get(System.getProperty("user.home"), "xxx", "xxx_" + Application.getAppVersion('_')).toString();
        }

        return new String();

    }

Now I try to use ${LogProperties:root_path} in my log4j2.xml but get an Exception "main ERROR Unable to create file ${LogProperties:root_path}/log/xxx.log"

No breakpoint is hit inside my LogPropertiesLookup class BUT if I comment the @Plugin annotation the logs showing up that log4j2 recognizes one LookupPlugin less

2015-09-29 07:41:48,657 main DEBUG PluginManager 'Core' found 75 plugins
2015-09-29 07:41:48,657 main DEBUG PluginManager 'Level' found 0 plugins
2015-09-29 07:41:48,657 main DEBUG PluginManager 'Lookup' found 13 plugins

Removing the comment shows 14 plugins so log4j is finding something.

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="TRACE" packages="x.y.z.system">

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <RollingFile name="xxx" fileName="${LogProperties:root_path}/log/xxx.log"
            filePattern="${LogProperties:root_path}/log/archive/$${date:yyyy-MM}/xxx-%d{MM-dd-yyyy}-%i.log.gz"
            immediateFlush="true">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="50 MB" />
            </Policies>
        </RollingFile>
    </Appenders>

    <Loggers>

        <Root level="DEBUG">
            <AppenderRef ref="xxx" />
            <AppenderRef ref="Console" />
        </Root>

        <logger name="org.hibernate" level="DEBUG" additivity="false">
            <AppenderRef ref="xxx" />
        </logger>

        <logger name="org.hibernate.SQL" level="DEBUG" additivity="false">
            <AppenderRef ref="xxx" />
        </logger>

    </Loggers>

</Configuration>

Edit 1

I have added the log4j2 sources and debugged through the code. My Lookup Plugin is recognized BUT if log4j2 tries to find an matching Lookup Provider for "LogProperties" it does not .toLowerCase() and map.containsKey("LogProperties") does not match "logproperties" ;)

${logproperties:root_path} is replaced correctly.

Community
  • 1
  • 1
Pascal
  • 2,059
  • 3
  • 31
  • 52

0 Answers0