logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simone Tripodi <simonetrip...@apache.org>
Subject [log4j2] java.lang.NullPointerException: No Configuration was provided
Date Fri, 02 Aug 2013 08:52:50 GMT
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


Mime
View raw message