logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <garydgreg...@gmail.com>
Subject Re: [log4j2] java.lang.NullPointerException: No Configuration was provided
Date Fri, 02 Aug 2013 21:38:16 GMT
Does any of this belong in the 'extending log4j' section of the manual? The
Javadoc?

Gary


On Fri, Aug 2, 2013 at 8:30 AM, Christian Grobmeier <grobmeier@gmail.com>wrote:

> Hi,
>
> I looked at it (Simone provided me a patch with the current state of work).
>
> You need to override this in the OnamiConfigurationFactory:
>
>     @Override
>     public Configuration getConfiguration(final String name, final URI
> configLocation) {
>         return getConfiguration(null);
>     }
>
> The problem is that you need a configLocation otherwise you will be
> returned null which causes the NPE:
>
> LoggerContext line 369 in beta-8:
> final Configuration instance =
> ConfigurationFactory.getInstance().getConfiguration(name,
> configLocation)
>
> This is of course unfortunate. I have debugged the whole configuration
> and think its very difficult
> to programmatically configure log4j without using a xml/json file. If
> we have a chance to make configuration easier,
> we should do it.
>
> Others: are there any plans / ideas for it already?
>
> Cheers
> Christian
>
>
> On Fri, Aug 2, 2013 at 10:52 AM, Simone Tripodi
> <simonetripodi@apache.org> wrote:
> > Hi all mates,
> >
> > I am trying to setup log4j2 configuration programmatically, I defined
> > my version of Configuration:
> >
> >
> +------------------------------------------------------------------------------------------------------------------------------------+
> > class MyLoggingConfiguration
> >     extends BaseConfiguration
> > {
> >
> >     private final String levelName;
> >
> >     private final String pattern;
> >
> >     public MyLoggingConfiguration( String levelName, String pattern )
> >     {
> >         this.levelName = levelName;
> >         this.pattern = pattern;
> >     }
> >
> >     @Override
> >     protected void doConfigure()
> >     {
> >         setName( "my" );
> >
> >         Layout<? extends Serializable> layout = createLayout( pattern,
> > null, null, null, null );
> >         Appender<? extends Serializable> appender = createAppender(
> > layout, null, "SYSTEM_OUT", "Console", "false", "true" );
> >         appender.start();
> >         addAppender( appender );
> >         getRootLogger().addAppender( appender, null, null );
> >
> >         Level level = Level.valueOf( levelName );
> >         getRootLogger().setLevel( level );
> >     }
> >
> > }
> >
> +------------------------------------------------------------------------------------------------------------------------------------+
> >
> > and ConfigurationFactory:
> >
> >
> +------------------------------------------------------------------------------------------------------------------------------------+
> > class MyConfigurationFactory
> >     extends ConfigurationFactory
> > {
> >
> >     private final String levelName;
> >
> >     private final String pattern;
> >
> >     public MyConfigurationFactory( String levelName, String pattern )
> >     {
> >         this.levelName = levelName;
> >         this.pattern = pattern;
> >     }
> >
> >     @Override
> >     protected String[] getSupportedTypes()
> >     {
> >         return new String[] { "*" };
> >     }
> >
> >     @Override
> >     public Configuration getConfiguration( ConfigurationSource source )
> >     {
> >         return new MyLoggingConfiguration( levelName, pattern );
> >     }
> >
> > }
> >
> +------------------------------------------------------------------------------------------------------------------------------------+
> >
> > then, as specified in the Javadoc, invoke
> > ConfigurationFactory#setConfigurationFactory(new
> > MyConfigurationFactory("INFO", "[%level] %message%n")) before any
> > other calls to Log4j, but when my application starts, an exception is
> > thrown:
> >
> >
> +------------------------------------------------------------------------------------------------------------------------------------+
> > Exception in thread "main" java.lang.NullPointerException: No
> > Configuration was provided
> >         at
> org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:323)
> >         at
> org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:370)
> >         at
> org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:149)
> >         at
> org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:85)
> >         at
> org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:34)
> >         at
> org.apache.logging.log4j.LogManager.getLogger(LogManager.java:364)
> >         at
> org.apache.logging.log4j.LogManager.getLogger(LogManager.java:319)
> >         ...
> >
> +------------------------------------------------------------------------------------------------------------------------------------+
> >
> > What am I doing wrong?
> > Many thanks in advance, all the best!
> > -Simo
> >
> > http://people.apache.org/~simonetripodi/
> > http://twitter.com/simonetripodi
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> > For additional commands, e-mail: log4j-user-help@logging.apache.org
> >
>
>
>
> --
> http://www.grobmeier.de
> https://www.timeandbill.de
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>


-- 
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

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message