You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Arnie Morein <ar...@mac.com> on 2018/02/19 15:04:27 UTC

Access to current run-time config

Hello, looking for some advice, assuming this is possible.

I am working with a rather complex application and the log4j2 configuration is also complex (12+ loggers).

It is started at war deployment by a WEB-INF/classes/log4j2.xml file and then after the context is started, based on the host name (ie, environment: prod, test, dev), the logging levels are changed to desired defaults.

All of this works great. But now I need to build a control panel page that lists each appender/logger so certain settings can be changed or the file rotated at run time when situations arise.

I know I can get a copy of the configuration from the file, but remember above, those defaults have already been changed based on the host name.

I need to access the CURRENT in-memory configuration, but after digging through the API, I cannot find the right call for this, assuming it exists. Is there one?

My point being is that if I use the altered config default based on host name, change a logger level from A to B, do some work, and want to return to the control panel to return B to A or make some other change, I won't see the fact that A is already at B.

Thanks!
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Access to current run-time config

Posted by Franz Wong <fr...@gmail.com>.
Do you mean something like this?

public class MainApplication {


public static void main(String[] args) throws Exception {

Logger logger = LogManager.getLogger(MainApplication.class);

// logging level is error

logger.error("msg1");

logger.info("msg2");

org.apache.logging.log4j.core.LoggerContext context =
(org.apache.logging.log4j.core.LoggerContext) LogManager

.getContext(false);

Configuration config = context.getConfiguration();

config.getRootLogger().setLevel(Level.INFO);

context.updateLoggers(config);

// now logging level is info

logger.error("msg3");

logger.info("msg4");

}


}


On Tue, Feb 20, 2018 at 12:04 AM, Arnie Morein <ar...@mac.com> wrote:

> Hello, looking for some advice, assuming this is possible.
>
> I am working with a rather complex application and the log4j2
> configuration is also complex (12+ loggers).
>
> It is started at war deployment by a WEB-INF/classes/log4j2.xml file and
> then after the context is started, based on the host name (ie, environment:
> prod, test, dev), the logging levels are changed to desired defaults.
>
> All of this works great. But now I need to build a control panel page that
> lists each appender/logger so certain settings can be changed or the file
> rotated at run time when situations arise.
>
> I know I can get a copy of the configuration from the file, but remember
> above, those defaults have already been changed based on the host name.
>
> I need to access the CURRENT in-memory configuration, but after digging
> through the API, I cannot find the right call for this, assuming it exists.
> Is there one?
>
> My point being is that if I use the altered config default based on host
> name, change a logger level from A to B, do some work, and want to return
> to the control panel to return B to A or make some other change, I won't
> see the fact that A is already at B.
>
> Thanks!
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>