logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Remko Popma <remko.po...@gmail.com>
Subject Re: Adding a logger with custom appender pragmatically, without using the ROOT appends for this logger, is this possible?
Date Wed, 23 Jul 2014 11:40:31 GMT
One thing you can try is in your log4j2.xml, add a threshold filter that accepts INFO to the
console appender. For an example, see http://logging.apache.org/log4j/2.x/manual/filters.html#ThresholdFilter

Sent from my iPhone

> On 2014/07/23, at 16:27, Nayden Gochev <gochev@gmail.com> wrote:
> 
> 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 <gochev@gmail.com> 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 <boards@gmail.com> wrote:
>>> 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