You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Benjamin Jaton <be...@gmail.com> on 2015/10/21 00:13:36 UTC
Log4j2 set log level, cascade
Hello,
Here is a simple configuration:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} -
%msg%n"/>
</Console>
</Appenders>
<Loggers>
* <Logger name="com.foo.Bar" level="trace">*
<AppenderRef ref="Console"/>
</Logger>
* <Logger name="com.foo.Test" level="trace">*
<AppenderRef ref="Console"/>
</Logger>
* <Root level="error">*
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
I am trying to programmatically set the log level of "com.rli", but I can't
find the Loggers until they are used:
*System.setProperty("log4j.configurationFile",
"/tmp/test.xml");LogManager.getLogger("com.foo.Test");LoggerContext ctx =
(LoggerContext) LogManager.getContext(false);Configuration conf =
ctx.getConfiguration();for (Logger l : ctx.getLoggers()) { LoggerConfig
config = conf.getLoggerConfig(l.getName()); System.out.println("Found
logger " + l.getName());}*
This will only show the com.foo.Test Logger because I created it just
before, but it will not find com.rli.Bar.
How can I get ALL the loggers, so that I can then set their log level?
Note that setting directly the log level of "com.rli" will not work because
what is defined for com.rli.Bar in the conf file will take precedence.
Thanks
Ben
Re: Log4j2 set log level, cascade
Posted by Gary Gregory <ga...@gmail.com>.
Ben,
I would recommend using the current version 2.4.1. I think I added those
for 2.4...
You can just get the sources from Git and copy the new methods you need to
your code base. Of course, using the current version is better IMO.
Gary
On Tue, Oct 20, 2015 at 3:55 PM, Benjamin Jaton <be...@gmail.com>
wrote:
> It doesn't look like this is available in 2.3, was it added recently?
> Any workaround for 2.3?
>
> Thanks
> Ben
>
> On Tue, Oct 20, 2015 at 3:26 PM, Gary Gregory <ga...@gmail.com>
> wrote:
>
> > Check out the
> > APIs
> org.apache.logging.log4j.core.config.Configurator.setAllLevels(String,
> > Level) and setLevel().
> >
> > Gary
> >
> > On Tue, Oct 20, 2015 at 3:13 PM, Benjamin Jaton <
> benjamin.jaton@gmail.com>
> > wrote:
> >
> > > Hello,
> > >
> > > Here is a simple configuration:
> > >
> > > <?xml version="1.0" encoding="UTF-8"?>
> > > <Configuration status="WARN">
> > > <Appenders>
> > > <Console name="Console" target="SYSTEM_OUT">
> > > <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level
> %logger{36}
> > -
> > > %msg%n"/>
> > > </Console>
> > > </Appenders>
> > > <Loggers>
> > > * <Logger name="com.foo.Bar" level="trace">*
> > > <AppenderRef ref="Console"/>
> > > </Logger>
> > > * <Logger name="com.foo.Test" level="trace">*
> > > <AppenderRef ref="Console"/>
> > > </Logger>
> > > * <Root level="error">*
> > > <AppenderRef ref="Console"/>
> > > </Root>
> > > </Loggers>
> > > </Configuration>
> > >
> > > I am trying to programmatically set the log level of "com.rli", but I
> > can't
> > > find the Loggers until they are used:
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > *System.setProperty("log4j.configurationFile",
> > > "/tmp/test.xml");LogManager.getLogger("com.foo.Test");LoggerContext
> ctx =
> > > (LoggerContext) LogManager.getContext(false);Configuration conf =
> > > ctx.getConfiguration();for (Logger l : ctx.getLoggers()) {
> > LoggerConfig
> > > config = conf.getLoggerConfig(l.getName());
> System.out.println("Found
> > > logger " + l.getName());}*
> > > This will only show the com.foo.Test Logger because I created it just
> > > before, but it will not find com.rli.Bar.
> > > How can I get ALL the loggers, so that I can then set their log level?
> > >
> > > Note that setting directly the log level of "com.rli" will not work
> > because
> > > what is defined for com.rli.Bar in the conf file will take precedence.
> > >
> > > Thanks
> > > Ben
> > >
> >
> >
> >
> > --
> > E-Mail: garydgregory@gmail.com | ggregory@apache.org
> > Java Persistence with Hibernate, Second Edition
> > <http://www.manning.com/bauer3/>
> > JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> > Spring Batch in Action <http://www.manning.com/templier/>
> > Blog: http://garygregory.wordpress.com
> > Home: http://garygregory.com/
> > Tweet! http://twitter.com/GaryGregory
> >
>
--
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition
<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory
Re: Log4j2 set log level, cascade
Posted by Benjamin Jaton <be...@gmail.com>.
It doesn't look like this is available in 2.3, was it added recently?
Any workaround for 2.3?
Thanks
Ben
On Tue, Oct 20, 2015 at 3:26 PM, Gary Gregory <ga...@gmail.com>
wrote:
> Check out the
> APIs org.apache.logging.log4j.core.config.Configurator.setAllLevels(String,
> Level) and setLevel().
>
> Gary
>
> On Tue, Oct 20, 2015 at 3:13 PM, Benjamin Jaton <be...@gmail.com>
> wrote:
>
> > Hello,
> >
> > Here is a simple configuration:
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> > <Configuration status="WARN">
> > <Appenders>
> > <Console name="Console" target="SYSTEM_OUT">
> > <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}
> -
> > %msg%n"/>
> > </Console>
> > </Appenders>
> > <Loggers>
> > * <Logger name="com.foo.Bar" level="trace">*
> > <AppenderRef ref="Console"/>
> > </Logger>
> > * <Logger name="com.foo.Test" level="trace">*
> > <AppenderRef ref="Console"/>
> > </Logger>
> > * <Root level="error">*
> > <AppenderRef ref="Console"/>
> > </Root>
> > </Loggers>
> > </Configuration>
> >
> > I am trying to programmatically set the log level of "com.rli", but I
> can't
> > find the Loggers until they are used:
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > *System.setProperty("log4j.configurationFile",
> > "/tmp/test.xml");LogManager.getLogger("com.foo.Test");LoggerContext ctx =
> > (LoggerContext) LogManager.getContext(false);Configuration conf =
> > ctx.getConfiguration();for (Logger l : ctx.getLoggers()) {
> LoggerConfig
> > config = conf.getLoggerConfig(l.getName()); System.out.println("Found
> > logger " + l.getName());}*
> > This will only show the com.foo.Test Logger because I created it just
> > before, but it will not find com.rli.Bar.
> > How can I get ALL the loggers, so that I can then set their log level?
> >
> > Note that setting directly the log level of "com.rli" will not work
> because
> > what is defined for com.rli.Bar in the conf file will take precedence.
> >
> > Thanks
> > Ben
> >
>
>
>
> --
> E-Mail: garydgregory@gmail.com | ggregory@apache.org
> Java Persistence with Hibernate, Second Edition
> <http://www.manning.com/bauer3/>
> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> Spring Batch in Action <http://www.manning.com/templier/>
> Blog: http://garygregory.wordpress.com
> Home: http://garygregory.com/
> Tweet! http://twitter.com/GaryGregory
>
Re: Log4j2 set log level, cascade
Posted by Gary Gregory <ga...@gmail.com>.
Check out the
APIs org.apache.logging.log4j.core.config.Configurator.setAllLevels(String,
Level) and setLevel().
Gary
On Tue, Oct 20, 2015 at 3:13 PM, Benjamin Jaton <be...@gmail.com>
wrote:
> Hello,
>
> Here is a simple configuration:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <Configuration status="WARN">
> <Appenders>
> <Console name="Console" target="SYSTEM_OUT">
> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} -
> %msg%n"/>
> </Console>
> </Appenders>
> <Loggers>
> * <Logger name="com.foo.Bar" level="trace">*
> <AppenderRef ref="Console"/>
> </Logger>
> * <Logger name="com.foo.Test" level="trace">*
> <AppenderRef ref="Console"/>
> </Logger>
> * <Root level="error">*
> <AppenderRef ref="Console"/>
> </Root>
> </Loggers>
> </Configuration>
>
> I am trying to programmatically set the log level of "com.rli", but I can't
> find the Loggers until they are used:
>
>
>
>
>
>
>
>
>
>
>
>
> *System.setProperty("log4j.configurationFile",
> "/tmp/test.xml");LogManager.getLogger("com.foo.Test");LoggerContext ctx =
> (LoggerContext) LogManager.getContext(false);Configuration conf =
> ctx.getConfiguration();for (Logger l : ctx.getLoggers()) { LoggerConfig
> config = conf.getLoggerConfig(l.getName()); System.out.println("Found
> logger " + l.getName());}*
> This will only show the com.foo.Test Logger because I created it just
> before, but it will not find com.rli.Bar.
> How can I get ALL the loggers, so that I can then set their log level?
>
> Note that setting directly the log level of "com.rli" will not work because
> what is defined for com.rli.Bar in the conf file will take precedence.
>
> Thanks
> Ben
>
--
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition
<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory