Matt,
so you are right.. if I do a XML configuration and make additivity= false
and register my logger with appenderRef there.. everything works as
expected.. the ROOT appenders are not used.
However.. when I do it via code ( the one I send in the previous mail ) the
root appenders are working...
Do I do something wrong abotu creation ?
or is there some difference in how updateLoggers(cfg) works compared to
after parsing the log4j2.xml and again updating loggers ?

Thanks,
Nayden


On Wed, Jul 23, 2014 at 9:55 AM, Nayden Gochev <[email protected]> wrote:

> Hello Matt,
> the problem is that I want to create the appender and the logger together
> via code.
> So the appender or the logger are not mentioned in the log4j2.xml file
>
> I am creating the appender via the createAppender factory method but in
> the code.
> Full code for enable the logger + appender I use is this :
> if (enable) {
>             final LoggerContext ctx = (LoggerContext)
> LogManager.getContext(false);
>             Configuration cfg = ctx.getConfiguration();
>             if (cfg instanceof XmlConfiguration) {
>                 //add appender if not added
>                 Appender appender =
> cfg.getAppender(LogStreamAppender.NAME);
>                 if (appender == null) {
>                     appender
> = LogStreamAppender.createAppender(LogStreamAppender.NAME, "%highlight{%d
> [%t] %-5level: %msg%n%throwable}", "false", null);
>                     cfg.addAppender(appender);
>                 }
>                 //add logger if not added
>                 LoggerConfig logger = ((XmlConfiguration)
> cfg).getLogger(loggerName);
>                 if (logger == null) {
>                     logger = new LoggerConfig(loggerName, Level.DEBUG,
> false);
>                     cfg.addLogger(loggerName, logger);
>                 } else {
>                     logger.setLevel(Level.DEBUG);
>                 }
>                 logger.addAppender(appender, Level.DEBUG, null);
>                 ctx.updateLoggers(cfg);
>             }
>         }
>
> The problem is that indeed this LogStreamAppender is receiving everything
> correctly. However in the log4j2.xml I have several other appenders added
> at the ROOT log.
> <Appenders>
> <Console name="CONSOLE" target="SYSTEM_OUT">
> <PatternLayout pattern="%highlight{%d [%t] %-5level: %msg%n%throwable}" />
>  </Console>
> .....
> </Appenders>
> <Root level="info">
> <AppenderRef ref="CONSOLE"/>
>
>  ....
> </Root>
>
> All of which .. start automatically receive my Stream log ... which is
> what I don't want to happen :(
> So even if I pass additivity to false... the root appenders (which were
> info but are now as it looks are debug for this package (loggerName)) are
> still receiving the messages
>
> Regards,
> Nayden
>
>
>
> On Tue, Jul 22, 2014 at 10:35 PM, Matt Sicker <[email protected]> wrote:
>
>> How are you creating the appender? It might work better using a config
>> file.
>>
>>
>> On 22 July 2014 02:47, Nayden Gochev <[email protected]> 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" <[email protected]> wrote:
>>>
>>>> You should specify false for additivity to prevent the automatic
>>>> inheritance.
>>>>
>>>>
>>>> On 21 July 2014 09:20, Nayden Gochev <[email protected]> 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 <[email protected]>
>>>>
>>>
>>
>>
>> --
>> Matt Sicker <[email protected]>
>>
>
>

Reply via email to