Doing what you have below seems like a very "sledgehammer-like" approach.

If you have something you can key on it would make more sense to use either
a) configure both appenders and filter them based on the process.
b) use the RoutingAppender to more or less do the same as option a but in a 
more dynamic way.

FWIW - the RoutingAppender basically does exactly what you are trying to do - 
it can dynamically create new appender instances.

That said, I don't see anything obviously wrong with the code below. I have a 
suspicion that if you change the call to addAppender to addAppender(appender, 
null, filter) it might work. There might be a bug in checking the level on the 
AppenderControl. 

Does


On Oct 18, 2012, at 7:56 AM, Andy Grove wrote:

> 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


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-user-h...@logging.apache.org

Reply via email to