logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andy Grove <andy.gr...@codefutures.com>
Subject Adding appenders in a custom ConfigurationFactory (log4j 2)
Date Thu, 18 Oct 2012 14:56:19 GMT
Hi,

I've just converted a product to use log4j 2 and it is working well apart from one thing.

The product has multiple processes e.g. processA and processB running on the same host. I
want them to use the same log4j.xml but write to different log files e.g. processA.log and
processB.log.

So I have created a custom ConfigurationFactory:

@Plugin(name = "DbsConfigurationFactory", type = "ConfigurationFactory")
@Order(5)
public class DbsConfigurationFactory extends ConfigurationFactory {
...
}

In the getConfiguration method I am loading the log4j.xml and attempting to add a new appender.
I couldn't find documentation for this and am pretty much guessing on the API calls.

    public Configuration getConfiguration(InputSource inputSource) {

        System.out.println("DbsConfigurationFactory.getConfiguration(" + inputSource + ")");

        final XMLConfiguration xmlConfiguration = new XMLConfiguration(inputSource, configFile);

        String logFilename = System.getProperty("log.file");
        if (logFilename != null) {

            System.out.println("Creating appender to write to " + logFilename);

            final LoggerConfig rootLogger = xmlConfiguration.getRootLogger();

            // create layout
            final PatternLayout layout = PatternLayout.createLayout(PatternLayout.DEFAULT_CONVERSION_PATTERN,
xmlConfiguration, null, "UTF-8");

            // create filter
            final Filter filter = rootLogger.getFilter();

            // create appender
            final FileAppender appender = FileAppender.createAppender(logFilename,
                    "true",     // append
                    "false",    // locking
                    "tbd",      // name
                    "false",    // immediateFlush
                    "false",    // suppress
                    "true",     // bufferedIO
                    layout,
                    filter
            );

            // add the appender to the config
            xmlConfiguration.addAppender(appender);

            // add the appender to the root logger
            rootLogger.addAppender(appender, Level.DEBUG, filter);

        }

        return xmlConfiguration;
    }

I do see the processA.log file being created but it is empty and log output continues to go
just to the console appender that is defined in my log4j.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF" monitorInterval="30" packages="com.dbshards.config">

    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level [%t] %logger{36} - %msg%n"/>
        </Console>
    </appenders>

    <loggers>
        <root level="info">
            <appender-ref ref="Console"/>
        </root>
    </loggers>

</configuration>

Could I get some pointers on what I am doing wrong? Also, is this the correct approach or
am I over complicating this?

Thanks,

Andy.

--
Andy Grove
Chief Architect
CodeFutures Corporation
Share Nothing, Shard Everything
http://www.dbshards.com
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message