Return-Path: X-Original-To: apmail-logging-log4j-dev-archive@www.apache.org Delivered-To: apmail-logging-log4j-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CE07D109ED for ; Wed, 23 Jul 2014 15:31:28 +0000 (UTC) Received: (qmail 42891 invoked by uid 500); 23 Jul 2014 15:31:28 -0000 Delivered-To: apmail-logging-log4j-dev-archive@logging.apache.org Received: (qmail 42834 invoked by uid 500); 23 Jul 2014 15:31:28 -0000 Mailing-List: contact log4j-dev-help@logging.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Log4J Developers List" Reply-To: "Log4J Developers List" Delivered-To: mailing list log4j-dev@logging.apache.org Received: (qmail 42822 invoked by uid 99); 23 Jul 2014 15:31:28 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Jul 2014 15:31:28 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of boards@gmail.com designates 209.85.218.44 as permitted sender) Received: from [209.85.218.44] (HELO mail-oi0-f44.google.com) (209.85.218.44) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Jul 2014 15:31:24 +0000 Received: by mail-oi0-f44.google.com with SMTP id x69so987092oia.3 for ; Wed, 23 Jul 2014 08:30:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=DtxrQn1N+eSf4H95QcGMAQyGEQM4AjswGtvdjPQXK7U=; b=DjyS540T0qvL+4cRJNMqslKNdPP/7KcKadNYfPIRzjsz7LkbHPYbxN5FJ9XOxp84eC vPMLL/jG6qVWgpzBtSbr7F3kSUkiOxosyuvhqecenF/KkckcV0+dDBEpsondEVmOPsPj K4DU6oJNo78YTO2nw00wfwJfcWjwSY8+cHp5zgF07oFjKBYteuQ2TfUoDDNfOtIw1FW0 WJA811rPZauX/sR33GqC8RPhF8SyJzIqd5Pnw5vzpX1vx9P3okso6shwo7P0GUgwH1sQ yVShcZ+I2AF9kGDXD5/eEMnfUrPGYgRcA0mHDDdOiYck77to7fklQURV4Gh1GjkySkNy s7zw== MIME-Version: 1.0 X-Received: by 10.182.120.129 with SMTP id lc1mr2975441obb.65.1406129459556; Wed, 23 Jul 2014 08:30:59 -0700 (PDT) Received: by 10.76.128.200 with HTTP; Wed, 23 Jul 2014 08:30:59 -0700 (PDT) In-Reply-To: <01BA46FE-D178-4186-88F7-78518ABCC324@gmail.com> References: <01BA46FE-D178-4186-88F7-78518ABCC324@gmail.com> Date: Wed, 23 Jul 2014 10:30:59 -0500 Message-ID: Subject: Re: Adding a logger with custom appender pragmatically, without using the ROOT appends for this logger, is this possible? From: Matt Sicker To: Log4J Developers List Content-Type: multipart/alternative; boundary=089e0117616df2717a04fede0693 X-Virus-Checked: Checked by ClamAV on apache.org --089e0117616df2717a04fede0693 Content-Type: text/plain; charset=UTF-8 Programmatic configuration is not yet supported which is the biggest issue here. The only "supported" way of doing it would be through creating a hierarchy of Node objects that directly correspond to a parsed tree of an XML or JSON config file. On 23 July 2014 06:40, Remko Popma wrote: > 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 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 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. >> >> >> >> >> ..... >> >> >> >> >> .... >> >> >> 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 wrote: >> >>> How are you creating the appender? It might work better using a config >>> file. >>> >>> >>> On 22 July 2014 02:47, Nayden Gochev 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" wrote: >>>> >>>>> You should specify false for additivity to prevent the automatic >>>>> inheritance. >>>>> >>>>> >>>>> On 21 July 2014 09:20, Nayden Gochev 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 >>>>> >>>> >>> >>> >>> -- >>> Matt Sicker >>> >> >> > -- Matt Sicker --089e0117616df2717a04fede0693 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Programmatic configuration is not yet supported which is t= he biggest issue here. The only "supported" way of doing it would= be through creating a hierarchy of Node objects that directly correspond t= o a parsed tree of an XML or JSON config file.


On 23 J= uly 2014 06:40, Remko Popma <remko.popma@gmail.com> wrot= e:
One thing you can try= is in your log4j2.xml, add a threshold filter that accepts INFO to the con= sole appender. For an example, see=C2=A0http://l= ogging.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,=C2=A0
so you are right.. if I do a XML conf= iguration and make additivity=3D false and register my logger with appender= Ref 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 crea= tion ?
or is there some difference in how updateLoggers(cfg) work= s 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= =C2=A0

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) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 final LoggerContext ctx =3D (LoggerContext) LogManager.getCon= text(false);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Configuration cfg =3D ctx.ge= tConfiguration();
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (c= fg instanceof XmlConfiguration) {
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 //add appender if not added
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Appender appender =3D = cfg.getAppender(LogStreamAppender.NAME);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (appender = =3D=3D null) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 appender =3D=C2=A0LogStreamAppender.createAppender(Log= StreamAppender.NAME, "%highlight{%d [%t] %-5level: %msg%n%throwable}&q= uot;, "false", null);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = cfg.addAppender(appender);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 //add logger if not added
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 LoggerConfig logger =3D ((XmlConfigurati= on) cfg).getLogger(loggerName);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (logger =3D= =3D null) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 logger =3D new LoggerConfig(loggerName, Level.DEBUG, fals= e);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 cfg.addLogger(loggerName, logger);
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 } else {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = logger.setLevel(Level.DEBUG);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 logger.addAppender(appender, Level.DEBUG, null);
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ctx.updateLoggers(c= fg);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

The probl= em is that indeed this=C2=A0LogStreamAppender=C2=A0is receiving everything = correctly. However in the log4j2.xml I have several other appenders added a= t the ROOT log.
<Appenders>
<Console name=3D"CONSOLE" target=3D"SYSTEM_OUT"&= gt;
<PatternLay= out pattern=3D"%highlight{%d [%t] %-5level: %msg%n%throwable}" /&= gt;
</Console>
.....=C2=A0
</Appenders>
<Root l= evel=3D"info">
= <AppenderRef ref=3D"CONSOLE"/>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0
=C2=A0....
</Root>

All of = which .. start automatically receive my Stream log ... which is what I don&= #39;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 t= his package (loggerName)) are still receiving the messages=C2=A0

Regards,
Nayden



On Tue, Jul = 22, 2014 at 10:35 PM, Matt Sicker <boards@gmail.com> wrote:
How are you creating the ap= pender? It might work better using a config file.


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

Hello Matt,<= /p>

this sounds great however it doesn't work.

Mayb= e it is a bug ?

what I do is :

=C2=A0final LoggerContext ctx =3D (LoggerContext) L= ogManager.getContext(false);

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Configuration cfg =3D ctx.getC= onfiguration();

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (cfg ins= tanceof XmlConfiguration) {

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 //add logger if not added

=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 LoggerConfig logger =3D ((XmlConfiguration) cfg= ).getLogger(loggerName);

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (logger =3D= =3D null) {

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 logger =3D new LoggerConfig(loggerName, Level.DEBUG, false); = //here this false is for the additive

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 cfg.addLogger(loggerName, logger);

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 } else { //chang= e the level leave it as it is

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 logger.setLevel(Level.DEBUG);

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }

=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 logger.addAppender(newAppender, Leve= l.DEBUG, null);

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ctx.updateLogger= s(cfg);

}


Still .. all root loggers also receive the= log message(s) together with this new appender that is passed here.=C2=A0<= /p>

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 aut= omatic inheritance.


On 21 July 2014 09:20, Nayden Gochev <gochev@gmail.com><= /span> 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 automat= ically.. so even if I have a root appender(s) with level INFO defined in th= e log4j2.xml,=C2=A0
= 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 receiv= ing this DEBUG messages as well.
=
Is it possible somehow to REMOVE this ROOT appends.. from my newl= y created logger ?
= newLoggerConfig.getAppenderRefs().clear(); doesn't work of course :)

Thanks for the help,
much appreciated.=C2=A0



--
Matt Sicker = <boards@gmail.com<= /a>>



<= font color=3D"#888888">--
Matt Sicker <
boards@gmail.com>





--
Matt Sicker <boards@gmail.com>
--089e0117616df2717a04fede0693--