Discussion:
[jira] [Created] (LOG4J2-865) Changing level in LoggerConfig does not change AppenderControl and does not allow messages at new log level to be logged
Andrew Herr (JIRA)
2014-10-02 14:50:33 UTC
Permalink
Andrew Herr created LOG4J2-865:
----------------------------------

Summary: Changing level in LoggerConfig does not change AppenderControl and does not allow messages at new log level to be logged
Key: LOG4J2-865
URL: https://issues.apache.org/jira/browse/LOG4J2-865
Project: Log4j 2
Issue Type: Bug
Components: Appenders, Configurators
Affects Versions: 2.0.2
Environment: Windows7 64bit, Eclipse, Maven
Reporter: Andrew Herr
Fix For: 2.0.2


I am wrapping log4j2 in order to replace the logging backend used in house, so I am configuring the logger and appenders at runtime with calls to the Context, Configuration, and LoggerConfig. I'd like to change the log level on the fly, so I get the LoggerConfig for my named logger and call setLevel(Level) on it, and then updateLoggers in the context. New messages at the new (less severe) level are not logged. Through a debug session, I can see that the level in LoggerConfig is correctly updated, but the AppenderControl still has the old level, so callAppenders denies my event from being logged.

Code:
Set up the logger + RollingFileAppender
name = logName;
level = logLevel;
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();

LoggerConfig loggerConfig = new LoggerConfig(name, level, false);
PatternLayout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, config, null, null, true, false, null, null);

OnStartupTriggeringPolicy startupTrigger = OnStartupTriggeringPolicy.createPolicy();
SizeBasedTriggeringPolicy sizeTrigger = SizeBasedTriggeringPolicy.createPolicy("25MB");
TriggeringPolicy triggerPolicy = CompositeTriggeringPolicy.createPolicy(startupTrigger, sizeTrigger);
DefaultRolloverStrategy rolloverStrategy = DefaultRolloverStrategy.createStrategy("5", "1", "min", null, config);
RollingFileAppender rollingFileAppender = RollingFileAppender.createAppender(name + ".log", name + ".log.%i", "true", "RollingFile",
"true", "8192", "true", triggerPolicy, rolloverStrategy, layout, null, "true", "false", null, config);
rollingFileAppender.start();
loggerConfig.addAppender(rollingFileAppender, level, null);
config.addLogger(name, loggerConfig);
ctx.updateLoggers();


update the level:
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(name);
loggerConfig.setLevel(logLevel);
ctx.updateLoggers();



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
Ralph Goers (JIRA)
2014-10-02 15:44:33 UTC
Permalink
[ https://issues.apache.org/jira/browse/LOG4J2-865?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14156679#comment-14156679 ]

Ralph Goers commented on LOG4J2-865:
------------------------------------

Log Level filtering on Appenders is independent of filtering on LoggerConfigs as you might have multiple Appenders configured that you want to filter at different levels.
Rather than calling ctx.getConfiguration() and modifying the existing configuration you would be much better off creating a new configuration and replacing the existing one with the new one once it is constructed.

The AppenderControl actually contains the level to filter on that is specified in the AppenderRef, not on the Appender itself. If you want to change the level on the Appender you need to go through the list of appenders held in AbstractConfiguration, find the one you want, remove the existing filter and then add a new one. However, this cannot be done atomically so you will be in a state momentarily where the Appender has no filter.

The Level in the AppenderControl is final, so the only way to change the level is to remove the Appender from the LoggerConfig and re-add it back using the new level. Again, this can't be done atomically.

Although what you are asking for may seem simple, it is really quite complicated and is why we would recommend that you create a new Configuration with the new levels you want and then replace the old configuration with the new one. Doing it that way means your configuration will never be in a state where it is missing items.
Post by Andrew Herr (JIRA)
Changing level in LoggerConfig does not change AppenderControl and does not allow messages at new log level to be logged
-------------------------------------------------------------------------------------------------------------------------
Key: LOG4J2-865
URL: https://issues.apache.org/jira/browse/LOG4J2-865
Project: Log4j 2
Issue Type: Bug
Components: Appenders, Configurators
Affects Versions: 2.0.2
Environment: Windows7 64bit, Eclipse, Maven
Reporter: Andrew Herr
Fix For: 2.0.2
I am wrapping log4j2 in order to replace the logging backend used in house, so I am configuring the logger and appenders at runtime with calls to the Context, Configuration, and LoggerConfig. I'd like to change the log level on the fly, so I get the LoggerConfig for my named logger and call setLevel(Level) on it, and then updateLoggers in the context. New messages at the new (less severe) level are not logged. Through a debug session, I can see that the level in LoggerConfig is correctly updated, but the AppenderControl still has the old level, so callAppenders denies my event from being logged.
Set up the logger + RollingFileAppender
name = logName;
level = logLevel;
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = new LoggerConfig(name, level, false);
PatternLayout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, config, null, null, true, false, null, null);
OnStartupTriggeringPolicy startupTrigger = OnStartupTriggeringPolicy.createPolicy();
SizeBasedTriggeringPolicy sizeTrigger = SizeBasedTriggeringPolicy.createPolicy("25MB");
TriggeringPolicy triggerPolicy = CompositeTriggeringPolicy.createPolicy(startupTrigger, sizeTrigger);
DefaultRolloverStrategy rolloverStrategy = DefaultRolloverStrategy.createStrategy("5", "1", "min", null, config);
RollingFileAppender rollingFileAppender = RollingFileAppender.createAppender(name + ".log", name + ".log.%i", "true", "RollingFile",
"true", "8192", "true", triggerPolicy, rolloverStrategy, layout, null, "true", "false", null, config);
rollingFileAppender.start();
loggerConfig.addAppender(rollingFileAppender, level, null);
config.addLogger(name, loggerConfig);
ctx.updateLoggers();
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(name);
loggerConfig.setLevel(logLevel);
ctx.updateLoggers();
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
Andrew Herr (JIRA)
2014-10-02 16:35:41 UTC
Permalink
[ https://issues.apache.org/jira/browse/LOG4J2-865?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Andrew Herr closed LOG4J2-865.
------------------------------
Resolution: Won't Fix

Thank you for the explanation, I'll generate a new configuration and replace the existing.
Post by Andrew Herr (JIRA)
Changing level in LoggerConfig does not change AppenderControl and does not allow messages at new log level to be logged
-------------------------------------------------------------------------------------------------------------------------
Key: LOG4J2-865
URL: https://issues.apache.org/jira/browse/LOG4J2-865
Project: Log4j 2
Issue Type: Bug
Components: Appenders, Configurators
Affects Versions: 2.0.2
Environment: Windows7 64bit, Eclipse, Maven
Reporter: Andrew Herr
Fix For: 2.0.2
I am wrapping log4j2 in order to replace the logging backend used in house, so I am configuring the logger and appenders at runtime with calls to the Context, Configuration, and LoggerConfig. I'd like to change the log level on the fly, so I get the LoggerConfig for my named logger and call setLevel(Level) on it, and then updateLoggers in the context. New messages at the new (less severe) level are not logged. Through a debug session, I can see that the level in LoggerConfig is correctly updated, but the AppenderControl still has the old level, so callAppenders denies my event from being logged.
Set up the logger + RollingFileAppender
name = logName;
level = logLevel;
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = new LoggerConfig(name, level, false);
PatternLayout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, config, null, null, true, false, null, null);
OnStartupTriggeringPolicy startupTrigger = OnStartupTriggeringPolicy.createPolicy();
SizeBasedTriggeringPolicy sizeTrigger = SizeBasedTriggeringPolicy.createPolicy("25MB");
TriggeringPolicy triggerPolicy = CompositeTriggeringPolicy.createPolicy(startupTrigger, sizeTrigger);
DefaultRolloverStrategy rolloverStrategy = DefaultRolloverStrategy.createStrategy("5", "1", "min", null, config);
RollingFileAppender rollingFileAppender = RollingFileAppender.createAppender(name + ".log", name + ".log.%i", "true", "RollingFile",
"true", "8192", "true", triggerPolicy, rolloverStrategy, layout, null, "true", "false", null, config);
rollingFileAppender.start();
loggerConfig.addAppender(rollingFileAppender, level, null);
config.addLogger(name, loggerConfig);
ctx.updateLoggers();
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(name);
loggerConfig.setLevel(logLevel);
ctx.updateLoggers();
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
Remko Popma (JIRA)
2014-10-02 22:48:34 UTC
Permalink
[ https://issues.apache.org/jira/browse/LOG4J2-865?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Remko Popma reopened LOG4J2-865:
--------------------------------
Post by Andrew Herr (JIRA)
Changing level in LoggerConfig does not change AppenderControl and does not allow messages at new log level to be logged
-------------------------------------------------------------------------------------------------------------------------
Key: LOG4J2-865
URL: https://issues.apache.org/jira/browse/LOG4J2-865
Project: Log4j 2
Issue Type: Bug
Components: Appenders, Configurators
Affects Versions: 2.0.2
Environment: Windows7 64bit, Eclipse, Maven
Reporter: Andrew Herr
Fix For: 2.0.2
I am wrapping log4j2 in order to replace the logging backend used in house, so I am configuring the logger and appenders at runtime with calls to the Context, Configuration, and LoggerConfig. I'd like to change the log level on the fly, so I get the LoggerConfig for my named logger and call setLevel(Level) on it, and then updateLoggers in the context. New messages at the new (less severe) level are not logged. Through a debug session, I can see that the level in LoggerConfig is correctly updated, but the AppenderControl still has the old level, so callAppenders denies my event from being logged.
Set up the logger + RollingFileAppender
name = logName;
level = logLevel;
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = new LoggerConfig(name, level, false);
PatternLayout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, config, null, null, true, false, null, null);
OnStartupTriggeringPolicy startupTrigger = OnStartupTriggeringPolicy.createPolicy();
SizeBasedTriggeringPolicy sizeTrigger = SizeBasedTriggeringPolicy.createPolicy("25MB");
TriggeringPolicy triggerPolicy = CompositeTriggeringPolicy.createPolicy(startupTrigger, sizeTrigger);
DefaultRolloverStrategy rolloverStrategy = DefaultRolloverStrategy.createStrategy("5", "1", "min", null, config);
RollingFileAppender rollingFileAppender = RollingFileAppender.createAppender(name + ".log", name + ".log.%i", "true", "RollingFile",
"true", "8192", "true", triggerPolicy, rolloverStrategy, layout, null, "true", "false", null, config);
rollingFileAppender.start();
loggerConfig.addAppender(rollingFileAppender, level, null);
config.addLogger(name, loggerConfig);
ctx.updateLoggers();
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(name);
loggerConfig.setLevel(logLevel);
ctx.updateLoggers();
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
Remko Popma (JIRA)
2014-10-02 22:48:34 UTC
Permalink
[ https://issues.apache.org/jira/browse/LOG4J2-865?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Remko Popma updated LOG4J2-865:
-------------------------------
Summary: How to change level in LoggerConfig and AppenderControl programmatically (was: Changing level in LoggerConfig does not change AppenderControl and does not allow messages at new log level to be logged)
How to change level in LoggerConfig and AppenderControl programmatically
------------------------------------------------------------------------
Key: LOG4J2-865
URL: https://issues.apache.org/jira/browse/LOG4J2-865
Project: Log4j 2
Issue Type: Bug
Components: Appenders, Configurators
Affects Versions: 2.0.2
Environment: Windows7 64bit, Eclipse, Maven
Reporter: Andrew Herr
Fix For: 2.0.2
I am wrapping log4j2 in order to replace the logging backend used in house, so I am configuring the logger and appenders at runtime with calls to the Context, Configuration, and LoggerConfig. I'd like to change the log level on the fly, so I get the LoggerConfig for my named logger and call setLevel(Level) on it, and then updateLoggers in the context. New messages at the new (less severe) level are not logged. Through a debug session, I can see that the level in LoggerConfig is correctly updated, but the AppenderControl still has the old level, so callAppenders denies my event from being logged.
Set up the logger + RollingFileAppender
name = logName;
level = logLevel;
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = new LoggerConfig(name, level, false);
PatternLayout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, config, null, null, true, false, null, null);
OnStartupTriggeringPolicy startupTrigger = OnStartupTriggeringPolicy.createPolicy();
SizeBasedTriggeringPolicy sizeTrigger = SizeBasedTriggeringPolicy.createPolicy("25MB");
TriggeringPolicy triggerPolicy = CompositeTriggeringPolicy.createPolicy(startupTrigger, sizeTrigger);
DefaultRolloverStrategy rolloverStrategy = DefaultRolloverStrategy.createStrategy("5", "1", "min", null, config);
RollingFileAppender rollingFileAppender = RollingFileAppender.createAppender(name + ".log", name + ".log.%i", "true", "RollingFile",
"true", "8192", "true", triggerPolicy, rolloverStrategy, layout, null, "true", "false", null, config);
rollingFileAppender.start();
loggerConfig.addAppender(rollingFileAppender, level, null);
config.addLogger(name, loggerConfig);
ctx.updateLoggers();
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(name);
loggerConfig.setLevel(logLevel);
ctx.updateLoggers();
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
Remko Popma (JIRA)
2014-10-02 22:50:33 UTC
Permalink
[ https://issues.apache.org/jira/browse/LOG4J2-865?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Remko Popma closed LOG4J2-865.
------------------------------
Resolution: Won't Fix

Changed title (added keyword "programmatically") to make this question easier to find for other users.
Post by Remko Popma (JIRA)
How to change level in LoggerConfig and AppenderControl programmatically
------------------------------------------------------------------------
Key: LOG4J2-865
URL: https://issues.apache.org/jira/browse/LOG4J2-865
Project: Log4j 2
Issue Type: Bug
Components: Appenders, Configurators
Affects Versions: 2.0.2
Environment: Windows7 64bit, Eclipse, Maven
Reporter: Andrew Herr
Fix For: 2.0.2
I am wrapping log4j2 in order to replace the logging backend used in house, so I am configuring the logger and appenders at runtime with calls to the Context, Configuration, and LoggerConfig. I'd like to change the log level on the fly, so I get the LoggerConfig for my named logger and call setLevel(Level) on it, and then updateLoggers in the context. New messages at the new (less severe) level are not logged. Through a debug session, I can see that the level in LoggerConfig is correctly updated, but the AppenderControl still has the old level, so callAppenders denies my event from being logged.
Set up the logger + RollingFileAppender
name = logName;
level = logLevel;
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = new LoggerConfig(name, level, false);
PatternLayout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, config, null, null, true, false, null, null);
OnStartupTriggeringPolicy startupTrigger = OnStartupTriggeringPolicy.createPolicy();
SizeBasedTriggeringPolicy sizeTrigger = SizeBasedTriggeringPolicy.createPolicy("25MB");
TriggeringPolicy triggerPolicy = CompositeTriggeringPolicy.createPolicy(startupTrigger, sizeTrigger);
DefaultRolloverStrategy rolloverStrategy = DefaultRolloverStrategy.createStrategy("5", "1", "min", null, config);
RollingFileAppender rollingFileAppender = RollingFileAppender.createAppender(name + ".log", name + ".log.%i", "true", "RollingFile",
"true", "8192", "true", triggerPolicy, rolloverStrategy, layout, null, "true", "false", null, config);
rollingFileAppender.start();
loggerConfig.addAppender(rollingFileAppender, level, null);
config.addLogger(name, loggerConfig);
ctx.updateLoggers();
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(name);
loggerConfig.setLevel(logLevel);
ctx.updateLoggers();
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
Andrew Herr (JIRA)
2014-10-08 19:40:33 UTC
Permalink
[ https://issues.apache.org/jira/browse/LOG4J2-865?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14164034#comment-14164034 ]

Andrew Herr commented on LOG4J2-865:
------------------------------------

Well, I thought it sounded straightforward, but after scouring the API docs I still have no idea how I can create a new configuration or replace the existing one in the current context. What is the best way to go about this programmatically? I see how to rip Appenders out and put them back in, but I'd prefer to play it safe.
Post by Remko Popma (JIRA)
How to change level in LoggerConfig and AppenderControl programmatically
------------------------------------------------------------------------
Key: LOG4J2-865
URL: https://issues.apache.org/jira/browse/LOG4J2-865
Project: Log4j 2
Issue Type: Bug
Components: Appenders, Configurators
Affects Versions: 2.0.2
Environment: Windows7 64bit, Eclipse, Maven
Reporter: Andrew Herr
Fix For: 2.0.2
I am wrapping log4j2 in order to replace the logging backend used in house, so I am configuring the logger and appenders at runtime with calls to the Context, Configuration, and LoggerConfig. I'd like to change the log level on the fly, so I get the LoggerConfig for my named logger and call setLevel(Level) on it, and then updateLoggers in the context. New messages at the new (less severe) level are not logged. Through a debug session, I can see that the level in LoggerConfig is correctly updated, but the AppenderControl still has the old level, so callAppenders denies my event from being logged.
Set up the logger + RollingFileAppender
name = logName;
level = logLevel;
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = new LoggerConfig(name, level, false);
PatternLayout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, config, null, null, true, false, null, null);
OnStartupTriggeringPolicy startupTrigger = OnStartupTriggeringPolicy.createPolicy();
SizeBasedTriggeringPolicy sizeTrigger = SizeBasedTriggeringPolicy.createPolicy("25MB");
TriggeringPolicy triggerPolicy = CompositeTriggeringPolicy.createPolicy(startupTrigger, sizeTrigger);
DefaultRolloverStrategy rolloverStrategy = DefaultRolloverStrategy.createStrategy("5", "1", "min", null, config);
RollingFileAppender rollingFileAppender = RollingFileAppender.createAppender(name + ".log", name + ".log.%i", "true", "RollingFile",
"true", "8192", "true", triggerPolicy, rolloverStrategy, layout, null, "true", "false", null, config);
rollingFileAppender.start();
loggerConfig.addAppender(rollingFileAppender, level, null);
config.addLogger(name, loggerConfig);
ctx.updateLoggers();
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(name);
loggerConfig.setLevel(logLevel);
ctx.updateLoggers();
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
Matt Sicker (JIRA)
2014-10-08 19:51:34 UTC
Permalink
[ https://issues.apache.org/jira/browse/LOG4J2-865?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14164047#comment-14164047 ]

Matt Sicker commented on LOG4J2-865:
------------------------------------

Well, the obvious way through the API is via LoggerContext.setConfigLocation(URI), but that's for a configuration file. The other way is handled via LoggerContext.start(Configuration) which will reconfigure the LoggerContext with the provided Configuration if it's already running.
Post by Remko Popma (JIRA)
How to change level in LoggerConfig and AppenderControl programmatically
------------------------------------------------------------------------
Key: LOG4J2-865
URL: https://issues.apache.org/jira/browse/LOG4J2-865
Project: Log4j 2
Issue Type: Bug
Components: Appenders, Configurators
Affects Versions: 2.0.2
Environment: Windows7 64bit, Eclipse, Maven
Reporter: Andrew Herr
Fix For: 2.0.2
I am wrapping log4j2 in order to replace the logging backend used in house, so I am configuring the logger and appenders at runtime with calls to the Context, Configuration, and LoggerConfig. I'd like to change the log level on the fly, so I get the LoggerConfig for my named logger and call setLevel(Level) on it, and then updateLoggers in the context. New messages at the new (less severe) level are not logged. Through a debug session, I can see that the level in LoggerConfig is correctly updated, but the AppenderControl still has the old level, so callAppenders denies my event from being logged.
Set up the logger + RollingFileAppender
name = logName;
level = logLevel;
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = new LoggerConfig(name, level, false);
PatternLayout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, config, null, null, true, false, null, null);
OnStartupTriggeringPolicy startupTrigger = OnStartupTriggeringPolicy.createPolicy();
SizeBasedTriggeringPolicy sizeTrigger = SizeBasedTriggeringPolicy.createPolicy("25MB");
TriggeringPolicy triggerPolicy = CompositeTriggeringPolicy.createPolicy(startupTrigger, sizeTrigger);
DefaultRolloverStrategy rolloverStrategy = DefaultRolloverStrategy.createStrategy("5", "1", "min", null, config);
RollingFileAppender rollingFileAppender = RollingFileAppender.createAppender(name + ".log", name + ".log.%i", "true", "RollingFile",
"true", "8192", "true", triggerPolicy, rolloverStrategy, layout, null, "true", "false", null, config);
rollingFileAppender.start();
loggerConfig.addAppender(rollingFileAppender, level, null);
config.addLogger(name, loggerConfig);
ctx.updateLoggers();
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(name);
loggerConfig.setLevel(logLevel);
ctx.updateLoggers();
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
Ralph Goers (JIRA)
2014-10-08 20:01:34 UTC
Permalink
[ https://issues.apache.org/jira/browse/LOG4J2-865?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14164061#comment-14164061 ]

Ralph Goers commented on LOG4J2-865:
------------------------------------

See http://logging.apache.org/log4j/2.x/manual/customconfig.html. It has an example where an XML configuration is used and then some additional configuration is programmatically added. With that approach it is just a matter of kicking off the reconfiguration.

Another way to do it is to bypass XML or JSON configuration and do something entirely different. In that case, the easiest thing to do is to look at how XMLConfiguration and JSONConfiguration work - the parse the DOM tree JSON tree and create Node objects to represent the things that will end up being configured. AbstractConfiguration will then process the Nodes and create the actual Appenders, Loggers, Filters, etc.
Post by Remko Popma (JIRA)
How to change level in LoggerConfig and AppenderControl programmatically
------------------------------------------------------------------------
Key: LOG4J2-865
URL: https://issues.apache.org/jira/browse/LOG4J2-865
Project: Log4j 2
Issue Type: Bug
Components: Appenders, Configurators
Affects Versions: 2.0.2
Environment: Windows7 64bit, Eclipse, Maven
Reporter: Andrew Herr
Fix For: 2.0.2
I am wrapping log4j2 in order to replace the logging backend used in house, so I am configuring the logger and appenders at runtime with calls to the Context, Configuration, and LoggerConfig. I'd like to change the log level on the fly, so I get the LoggerConfig for my named logger and call setLevel(Level) on it, and then updateLoggers in the context. New messages at the new (less severe) level are not logged. Through a debug session, I can see that the level in LoggerConfig is correctly updated, but the AppenderControl still has the old level, so callAppenders denies my event from being logged.
Set up the logger + RollingFileAppender
name = logName;
level = logLevel;
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = new LoggerConfig(name, level, false);
PatternLayout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, config, null, null, true, false, null, null);
OnStartupTriggeringPolicy startupTrigger = OnStartupTriggeringPolicy.createPolicy();
SizeBasedTriggeringPolicy sizeTrigger = SizeBasedTriggeringPolicy.createPolicy("25MB");
TriggeringPolicy triggerPolicy = CompositeTriggeringPolicy.createPolicy(startupTrigger, sizeTrigger);
DefaultRolloverStrategy rolloverStrategy = DefaultRolloverStrategy.createStrategy("5", "1", "min", null, config);
RollingFileAppender rollingFileAppender = RollingFileAppender.createAppender(name + ".log", name + ".log.%i", "true", "RollingFile",
"true", "8192", "true", triggerPolicy, rolloverStrategy, layout, null, "true", "false", null, config);
rollingFileAppender.start();
loggerConfig.addAppender(rollingFileAppender, level, null);
config.addLogger(name, loggerConfig);
ctx.updateLoggers();
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(name);
loggerConfig.setLevel(logLevel);
ctx.updateLoggers();
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Loading...