Setting Log4jContextSelector programmatic in Main application

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Setting Log4jContextSelector programmatic in Main application

Sverre Moe
Using static initialization of two log4j properties on top of my main
application.

    static {
        System.setProperty("Log4jContextSelector",
"org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
        System.setProperty("log4j.configurationFactory",
"com.company.utils.LoggingConfigurationFactory");
    }

    private static Logger logger = LogManager.getLogger(MyApplication.class);


Seems I have to define Log4jContextSelector on the command line,
otherwise Log4j2 does not use Async logging.
-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector

I can add this to maven configuration so users don't have to think
about this when they are running the application, but I also don't
want developers to have to manually have to add this property in IDE
or command line. Will end up with developers who might get different
performance than our users when they develop, test and debug the
application.


/Sverre

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Setting Log4jContextSelector programmatic in Main application

Remko Popma-2
The Java language spec says that static blocks and static fields are initialized in textual order.  https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.

Are you seeing something different?



(Shameless plug) Every java main() method deserves http://picocli.info

> On Jan 15, 2018, at 19:47, Sverre Moe <[hidden email]> wrote:
>
> Using static initialization of two log4j properties on top of my main
> application.
>
>    static {
>        System.setProperty("Log4jContextSelector",
> "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
>        System.setProperty("log4j.configurationFactory",
> "com.company.utils.LoggingConfigurationFactory");
>    }
>
>    private static Logger logger = LogManager.getLogger(MyApplication.class);
>
>
> Seems I have to define Log4jContextSelector on the command line,
> otherwise Log4j2 does not use Async logging.
> -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
>
> I can add this to maven configuration so users don't have to think
> about this when they are running the application, but I also don't
> want developers to have to manually have to add this property in IDE
> or command line. Will end up with developers who might get different
> performance than our users when they develop, test and debug the
> application.
>
>
> /Sverre
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
Reply | Threaded
Open this post in threaded view
|

Re: Setting Log4jContextSelector programmatic in Main application

Matt Sicker
What I normally do to avoid any initialization issues is to make a minimal
main() that delegates to a real main. My top level main sets system
properties and calls into the delegate.

If you're loading classes in that class that have loggers, you might be
waiting too long to set the properties.

On 15 January 2018 at 05:03, Remko Popma <[hidden email]> wrote:

> The Java language spec says that static blocks and static fields are
> initialized in textual order.  https://docs.oracle.com/
> javase/specs/jls/se8/html/jls-12.html#jls-12.
>
> Are you seeing something different?
>
>
>
> (Shameless plug) Every java main() method deserves http://picocli.info
>
> > On Jan 15, 2018, at 19:47, Sverre Moe <[hidden email]> wrote:
> >
> > Using static initialization of two log4j properties on top of my main
> > application.
> >
> >    static {
> >        System.setProperty("Log4jContextSelector",
> > "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
> >        System.setProperty("log4j.configurationFactory",
> > "com.company.utils.LoggingConfigurationFactory");
> >    }
> >
> >    private static Logger logger = LogManager.getLogger(
> MyApplication.class);
> >
> >
> > Seems I have to define Log4jContextSelector on the command line,
> > otherwise Log4j2 does not use Async logging.
> > -DLog4jContextSelector=org.apache.logging.log4j.core.async.
> AsyncLoggerContextSelector
> >
> > I can add this to maven configuration so users don't have to think
> > about this when they are running the application, but I also don't
> > want developers to have to manually have to add this property in IDE
> > or command line. Will end up with developers who might get different
> > performance than our users when they develop, test and debug the
> > application.
> >
> >
> > /Sverre
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
>



--
Matt Sicker <[hidden email]>