Volker,

Great to hear from you.

Yes, that does work, and at least resolves my "configure loggers" part
of the issue.   I can't rely on knowing any logger name from the
original configuration file using
configuration.getLoggerConfig(loggerName), but .getRootLogger() worked
for me.  Registering it under a parent logging config seemed to be the
missing step.

If I can get the appenders in my file registered with the existing
configuration, that will probably be sufficient for my current needs.
 Filters would be nice, but are not required yet at this point.
Still working through this part.

Ralph,

CompositeConfiguration sounds even better.  I'll give that a try first.

On Fri, Feb 9, 2018 at 3:21 AM, Volker Weber <v.we...@inexso.de> wrote:
> Hi Mike,
>
> in our app we implemented a REST-Service to reconfigure log4j2 at runtime.
>
> It's mainly to apply changes to the configuration and call updateLoggers()
>
>     LoggerContext loggerContext = (LoggerContext)
> LogManager.getContext(false);
>     Configuration configuration = loggerContext.getConfiguration();
>     LoggerConfig loggerConfig = configuration.getLoggerConfig(loggerName);
>     LoggerConfig specificConfig = loggerConfig;
>
>     if (!loggerConfig.getName().equals(loggerName)) {
>       specificConfig = new LoggerConfig(loggerName, level, true);
>       specificConfig.setParent(loggerConfig);
>       configuration.addLogger(loggerName, specificConfig);
>     }
>     specificConfig.setLevel(level);
>     loggerContext.updateLoggers();
>
> see attachment for our full Log4j2Util code.
>
> Regards,
>   Volker
>
>
>
> 2018-02-08 21:28 GMT+01:00 Mike Kienenberger <mkien...@gmail.com>:
>>
>> As others have reported in years past, the examples in the docs for
>>
>> Programmatically Modifying the Current Configuration after Initialization
>>
>> are out of date.  They don't compile.  They don't work (affect the
>> existing logging) even if you do fix the errors.
>>
>> Here's my situation:
>>
>> I am working in an environment with EVIL.JAR which includes a log4j2.xml
>> file.
>> I can't change the jar.  I can't specific a System Property to override
>> it.
>>
>> My code gets called as a loaded module long after the logging system
>> is initialized.
>>
>> I want logging in my own code to go to a different location, and
>> preferably I'd like to read the configuration in from a log4j2.xml
>> file so that anyone who uses my module isn't victim to the same evil
>> hardcoded-logging practices of EVIL.JAR.
>>
>> Creating an XMLConfiguration and initializing it lets me read the xml
>> file easily enough.   Looping through the data gets me the Appenders,
>> Filters and Loggers.   But I still can't use them to modify the
>> existing configuration.
>>
>> Another person took the approach of using JUL instead.  I hate JUL and
>> I'd really rather not have to go down that route.
>>
>> Thanks in advance.
>> -Mike
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
>> For additional commands, e-mail: log4j-user-h...@logging.apache.org
>>
>
>
>
> --
> inexso - information exchange solutions GmbH
> Ofener Straße 30 | 26121 Oldenburg
> www.inexso.de
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
> For additional commands, e-mail: log4j-user-h...@logging.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-user-h...@logging.apache.org

Reply via email to