Is there a log4j 2 config configuration that accepts an input stream, e.g. 1.x PropertyConfigurator.configure (in)?

My old Log4j 1.x code looks like this:

PropertyConfigurator.configure(getClass().getResourceAsStream("/log4j.properties")); 

I am trying to find the equivalent of Log4j 2.x, but so far I have not been successful. I started digging through my code, I know that context.setConfigLocation(f.toURI()) works well, and I found a private setConfiguration() method in LoggerContext that can do the trick, so I hack it for the public and end up method:

 public static String configLog4j2(InputStream in) { if (in == null) { String msg = "Error, InputStream is null"; System.err.println(msg); return msg; } try { LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false); ConfigurationSource source = new ConfigurationSource(in); Configuration instance = ConfigurationFactory.getInstance().getConfiguration(context, source); Class[] args = new Class[1]; args[0] = Configuration.class; Method method = context.getClass().getDeclaredMethod("setConfiguration", args); method.setAccessible(true); method.invoke(context, instance); } catch (Exception exr) { String msg = "Error, config log4j2 by inputStream failed, " + exr.getMessage(); System.err.println(msg); return msg; } return "ok"; } 

It seems to work well, however this is a private method, possibly changed in state in a future version.

Is there an official Log4j 2 API that can do this?

0
java log4j2
source share

No one has answered this question yet.

See similar questions:

8
I need log4j v4 equivalent to log4j v2 PropertyConfigurator.configure

or similar:

twenty
Enforce not-null field in a JSON object
one
Why is JMockIt mocking getter X.getE () for class X in this snippet?
one
Java log4j configuration configuration issues
one
Is there a mechanism for reconfiguring log4j2 in a predictable way?
one
Java ResourceBundles not reading utf-8 characters correctly - after updating Eclipse
0
Java: Invalid Key Store Generated Format Using Code
-one
Which constructor is here?
-one
Failed to deserialize private objects from Cipher input stream
-one
Why does reading a byte array into an object throw a java.io.StreamCorruptedException?

All Articles