logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Sicker <boa...@gmail.com>
Subject Re: Adding a logger with custom appender pragmatically, without using the ROOT appends for this logger, is this possible?
Date Tue, 22 Jul 2014 19:35:13 GMT
How are you creating the appender? It might work better using a config file.


On 22 July 2014 02:47, Nayden Gochev <gochev@gmail.com> wrote:

> Hello Matt,
>
> this sounds great however it doesn't work.
>
> Maybe it is a bug ?
>
> what I do is :
>
>  final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
>
>             Configuration cfg = ctx.getConfiguration();
>
>             if (cfg instanceof XmlConfiguration) {
>
>                 //add logger if not added
>
>                 LoggerConfig logger = ((XmlConfiguration)
> cfg).getLogger(loggerName);
>
>                 if (logger == null) {
>
>                     logger = new LoggerConfig(loggerName, Level.DEBUG,
> false); //here this false is for the additive
>
>                     cfg.addLogger(loggerName, logger);
>
>                 } else { //change the level leave it as it is
>
>                     logger.setLevel(Level.DEBUG);
>
>                 }
>
>                 logger.addAppender(newAppender, Level.DEBUG, null);
>
>                 ctx.updateLoggers(cfg);
>
> }
>
>
> Still .. all root loggers also receive the log message(s) together with
> this new appender that is passed here.
>
> I am using 2.0 final
>
>
> On Jul 21, 2014 5:32 PM, "Matt Sicker" <boards@gmail.com> wrote:
>
>> You should specify false for additivity to prevent the automatic
>> inheritance.
>>
>>
>> On 21 July 2014 09:20, Nayden Gochev <gochev@gmail.com> wrote:
>>
>>> In short:
>>> I am trying to add a custom appender + logger for some package for
>>> example "org.test"
>>> However the root appenders (appender refs) are always added as well
>>> automatically.. so even if I have a root appender(s) with level INFO
>>> defined in the log4j2.xml,
>>> ones I add a logger with level DEBUG.. with a new appender (only 1) ..
>>> the 2 root appenders that I have defined in the XML automatically starts
>>> receiving this DEBUG messages as well.
>>>
>>> Is it possible somehow to REMOVE this ROOT appends.. from my newly
>>> created logger ?
>>> newLoggerConfig.getAppenderRefs().clear(); doesn't work of course :)
>>>
>>> Thanks for the help,
>>> much appreciated.
>>>
>>
>>
>>
>> --
>> Matt Sicker <boards@gmail.com>
>>
>


-- 
Matt Sicker <boards@gmail.com>

Mime
View raw message