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

Adam Gent commented on LOG4J2-2649:
-----------------------------------

See LogManager:

[https://github.com/apache/logging-log4j2/blob/master/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java]

 

This whole static junk should be done via the ServiceLoader as well as its 
using reflection that will be exceedingly hard to fix with Graal VM:

 
{code:java}
    static {
        // Shortcut binding to force a specific logging implementation.
        final PropertiesUtil managerProps = PropertiesUtil.getProperties();
        final String factoryClassName = 
managerProps.getStringProperty(FACTORY_PROPERTY_NAME);
        if (factoryClassName != null) {
            try {
                factory = LoaderUtil.newCheckedInstanceOf(factoryClassName, 
LoggerContextFactory.class);
            } catch (final ClassNotFoundException cnfe) {
                LOGGER.error("Unable to locate configured LoggerContextFactory 
{}", factoryClassName);
            } catch (final Exception ex) {
                LOGGER.error("Unable to create configured LoggerContextFactory 
{}", factoryClassName, ex);
            }
        }

        if (factory == null) {
            final SortedMap<Integer, LoggerContextFactory> factories = new 
TreeMap<>();
            // note that the following initial call to ProviderUtil may block 
until a Provider has been installed when
            // running in an OSGi environment
            if (ProviderUtil.hasProviders()) {
                for (final Provider provider : ProviderUtil.getProviders()) {
                    final Class<? extends LoggerContextFactory> factoryClass = 
provider.loadLoggerContextFactory();
                    if (factoryClass != null) {
                        try {
                            factories.put(provider.getPriority(), 
factoryClass.newInstance());
                        } catch (final Exception e) {
                            LOGGER.error("Unable to create class {} specified 
in provider URL {}", factoryClass.getName(), provider
                                    .getUrl(), e);
                        }
                    }
                }

                if (factories.isEmpty()) {
                    LOGGER.error("Log4j2 could not find a logging 
implementation. "
                            + "Please add log4j-core to the classpath. Using 
SimpleLogger to log to the console...");
                    factory = new SimpleLoggerContextFactory();
                } else if (factories.size() == 1) {
                    factory = factories.get(factories.lastKey());
                } else {
                    final StringBuilder sb = new StringBuilder("Multiple 
logging implementations found: \n");
                    for (final Map.Entry<Integer, LoggerContextFactory> entry : 
factories.entrySet()) {
                        sb.append("Factory: 
").append(entry.getValue().getClass().getName());
                        sb.append(", Weighting: 
").append(entry.getKey()).append('\n');
                    }
                    factory = factories.get(factories.lastKey());
                    sb.append("Using factory: 
").append(factory.getClass().getName());
                    LOGGER.warn(sb.toString());

                }
            } else {
                LOGGER.error("Log4j2 could not find a logging implementation. "
                        + "Please add log4j-core to the classpath. Using 
SimpleLogger to log to the console...");
                factory = new SimpleLoggerContextFactory();
            }
        }
    }

{code}

Is also notable that this is is in API jar. I'm not fond of this kind of static 
initialization in API jars but I suppose somewhere bootstrapping has to happen. 
I rather that initialization be configurable or be able to intercept it (e.g. 
ServiceLoader).

I'll try to find more examples of reflection. Hopefully the above code mention 
is of some help.

Cheers
-Adam

>  Is Log4j2 considered to support successful static compilation under GraalVM?
> -----------------------------------------------------------------------------
>
>                 Key: LOG4J2-2649
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-2649
>             Project: Log4j 2
>          Issue Type: New Feature
>            Reporter: YangGuanchao
>            Priority: Blocker
>   Original Estimate: 168h
>  Remaining Estimate: 168h
>
> Oracle released GraalVM [https://github.com/oracle/graal] to support the 
> static compilation of Java applications to Native Image, which can increase 
> the startup speed of Java by at least 10 times. In the cloud native field, 
> this basic technology is believed to be in the near future. Will be supported 
> by more and more vendors, such as the Spring/Spring Boot community has 
> supported or plans to support the feature of GraalVM, see this issue: 
> [https://github.com/spring-projects/spring-framework/search?q=Graalvm&type=Issues]
>  can support static compilation, so it is recommended that Log4j2 also need 
> to support static compilation as soon as possible, and can be compiled 
> correctly under GraalVM.



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)

Reply via email to