[ 
https://issues.apache.org/jira/browse/LOG4J2-1184?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14979739#comment-14979739
 ] 

Robert Veitch commented on LOG4J2-1184:
---------------------------------------

Sure! Below is a sample class to demonstrate what I'm seeing, tested against 
log4j2 2.2.

package com.test;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.util.Cancellable;
import org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry;

public class CustomShutdownCallbackRegistry extends 
DefaultShutdownCallbackRegistry {
  private static Logger logger = 
LogManager.getLogger(CustomShutdownCallbackRegistry.class);

  public static void main(final String[] args) {}

  @Override
  public Cancellable addShutdownCallback(final Runnable callback) {
    logger.info("msg");
    return super.addShutdownCallback(callback);
  }
}

And then I call this class using the following system property:

-Dlog4j.shutdownCallbackRegistry=com.test.CustomShutdownCallbackRegistry

The stack trace is below:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
        at 
com.test.CustomShutdownCallbackRegistry.addShutdownCallback(CustomShutdownCallbackRegistry.java:15)
        at 
org.apache.logging.log4j.core.impl.Log4jContextFactory.addShutdownCallback(Log4jContextFactory.java:235)
        at 
org.apache.logging.log4j.core.LoggerContext.setUpShutdownHook(LoggerContext.java:179)
        at 
org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:140)
        at 
org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:147)
        at 
org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41)
        at org.apache.logging.log4j.LogManager.getContext(LogManager.java:175)
        at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:426)
        at 
com.test.CustomShutdownCallbackRegistry.<clinit>(CustomShutdownCallbackRegistry.java:9)

Perhaps it is my misunderstanding, are we not expected to be able to use log4j 
loggers within a custom ShutdownCallbackRegistry?

> NullPointerException when attempting to configure logging in custom 
> ShutdownCallbackRegistry
> --------------------------------------------------------------------------------------------
>
>                 Key: LOG4J2-1184
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1184
>             Project: Log4j 2
>          Issue Type: Bug
>    Affects Versions: 2.2
>         Environment: OSX, Ubuntu
>            Reporter: Robert Veitch
>            Priority: Minor
>
> I have a custom ShutdownCallbackRegistry implementation, which I've 
> configured to be instantiated via:
> -Dlog4j.shutdownCallbackRegistry=<my_implementation>
> If I configure a logger in the normal way, either via a static or instance 
> variable, I get a NullPointerException on startup. Something like:
> private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
> I think this is probably because the configured registry instance gets 
> created prior to the log4j subsystem responsible for loggers. My workaround 
> is to define the logger variable without assigning it:
> private static final Logger logger;
> And then assigning it as the first line of my run method:
> public void run() {
> logger = LoggerFactory.getLogger(MyClass.class);
> // shutdown registry code
> }
> But if possible, it seems like the instantiation order should be reworked so 
> that implementers of a custom registry do not have to do this.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

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

Reply via email to