I'll look into the JMX.


I can forego the stopping/starting of an appender if there is a way to force a 
rotation.



I also want to change the level of the appender or a given logger/loggers on 
the fly.


On Feb 20, 2018, at 07:11 PM, Ralph Goers <ralph.go...@dslextreme.com> wrote:


Stopping an appender that is in an active configuration is going to cause 
problems. You need to remove all references to that appender from LoggerConfigs 
(or “wrapper” appenders), call the updateLoggers method to have that take 
effect, then remove the appender from the configuration, and then stop it.

Another option would be to see if you can just use the JMX interface.

Ralph


On Feb 20, 2018, at 12:40 PM, Arnold Morein <arnie.mor...@me.com> wrote:


That was it! Thanks.


Next question: When I do this:


LoggerContext loggerContext = (LoggerContext)LogManager.getContext(false);
Configuration loggerConfiguration = loggerContext.getConfiguration();


Map<String, Appender> appendersType = loggerConfiguration.getAppenders();
for ( Map.Entry<String, Appender> appender : appendersType.entrySet() ) {
if ( appender.getKey().equalsIgnoreCase(selected.getAppenderName()) ) {


RollingFileAppender a = (RollingFileAppender)appender.getValue();
a.stop();
loggerConfiguration.initialize();


I get:


2018-02-20 13:29:23,913 ajp-nio-8009-exec-3 ERROR No logging configuration


and my entire log4j run-time config appears to be erased!


If I do a loggerContext.initialize(); there is no error, but upon refresh, the 
logger is still active.


I just want to change the state (start/stop) of the appender. How do I post the 
change back to the context?


On Feb 20, 2018, at 08:26 AM, Franz Wong <franzw...@gmail.com> wrote:


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 <http://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 <http://logger.info/>("msg4");


}




}




On Tue, Feb 20, 2018 at 12:04 AM, Arnie Morein <arnie.mor...@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-unsubscr...@logging.apache.org 
<mailto:log4j-user-unsubscr...@logging.apache.org>
For additional commands, e-mail: log4j-user-h...@logging.apache.org 
<mailto:log4j-user-h...@logging.apache.org>





Reply via email to