logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "SMITH, CURTIS" <cs0...@att.com>
Subject programmatically setting level for many loggers not working for 2nd - Nth logger
Date Thu, 25 Jul 2013 21:36:59 GMT
Programmatical setting of loggers only works for the first logger declared unfortunately. 
 Further there's a missing API that allows me to get a list of loggers (or configurations)
to walk and set level like get Category in v1.  See my code below that has coupling between
code and log4j2.xml

Only the DLA logger seems to work for setting level at run time.   If I remove the level="DEBUG"
attributes from the loggers, then I don't get any logs.

Code:
if (logLevelDebug != null) {
                 LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
                 Configuration config = ctx.getConfiguration();
                 LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
                 loggerConfig.setLevel(Level.DEBUG);
                 loggerConfig = config.getLoggerConfig("VIDEO");  // doesn't work.  Is coupling
with log4j2.xml
                 loggerConfig.setLevel(Level.DEBUG);
                 loggerConfig = config.getLoggerConfig("DLA");    // this works, but is first
declared in log4j2.xml
                 loggerConfig.setLevel(Level.DEBUG);
                 loggerConfig = config.getLoggerConfig("DEVICES");// doesn't work
                 loggerConfig.setLevel(Level.DEBUG);
                 ctx.updateLoggers();  // This causes all Loggers to refetch information from
their LoggerConfig.

                 logger.debug("ErrorLogsUI: setting root logger to DEBUG");
         }
         String logLevelWarn = req.getParameter("logLevelWarn");

         if (logLevelWarn != null) {
                 LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
                 Configuration config = ctx.getConfiguration();
                 LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
                 loggerConfig.setLevel(Level.WARN);
                 loggerConfig = config.getLoggerConfig("VIDEO");
                 loggerConfig.setLevel(Level.WARN);
                 loggerConfig = config.getLoggerConfig("DLA");
                 loggerConfig.setLevel(Level.WARN);
                 loggerConfig = config.getLoggerConfig("DEVICES");
                 loggerConfig.setLevel(Level.WARN);
                 ctx.updateLoggers();  // This causes all Loggers to refetch information from
their LoggerConfig.

                 logger.warn("ErrorLogsUI: setting root logger to WARN");
         }

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="trace" level="trace" >
        <appenders>
                <FastRollingFile name="DLA"
                        fileName="/tmp/att/sync/log/dla.log"
                        filePattern="/tmp/att/sync/log/dla.log.%i" append="true">
                        <PatternLayout>
                                <pattern>[%d][%-5p][%-15t][%-15c{1}]:%m%n</pattern>
                        </PatternLayout>
                        <Policies>
                                <SizeBasedTriggeringPolicy size="3 MB" />
                        </Policies>
                        <DefaultRolloverStrategy max="2" />
                </FastRollingFile>
                <FastRollingFile name="DEVICES"
                        fileName="/tmp/att/sync/log/dla_devices.log"
                        filePattern="/tmp/att/sync/log/dla_devices.log.%i" append="true">
                        <PatternLayout>
                                <pattern>[%d][%-5p][%-15t][%-15c{1}]:%m%n</pattern>
                        </PatternLayout>
                        <Policies>
                                <SizeBasedTriggeringPolicy size="3 MB" />
                        </Policies>
                        <DefaultRolloverStrategy max="1" />
                </FastRollingFile>
                <FastRollingFile name="VIDEO"
                        fileName="/tmp/att/sync/log/dla_video.log"
                        filePattern="/tmp/att/sync/log/dla_video.log.%i" append="true">
                        <PatternLayout>
                                <pattern>[%d][%-5p][%-15t][%-15c{1}]:%m%n</pattern>
                        </PatternLayout>
                        <Policies>
                                <SizeBasedTriggeringPolicy size="3 MB" />
                        </Policies>
                        <DefaultRolloverStrategy max="1" />
                </FastRollingFile>
                <Console name="CO" target="SYSTEM_OUT">
                        <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
                </Console>
        </appenders>
        <loggers>
                <logger name="com.att.dlc.afm" additivity="false" >
                        <appender-ref ref="DLA" />
                </logger>
                <logger name="com.att.dlc.devices" additivity="false" level="debug">
                        <appender-ref ref="DEVICES" />
                </logger>
                <logger name="com.att.dlc.util.serialport" additivity="false" level="debug">
                        <appender-ref ref="DEVICES" />
                </logger>
                <logger name="com.att.dlc.webcamserver" additivity="false" level="debug">
                        <appender-ref ref="VIDEO" />
                </logger>
                <logger name="com.att.dlc.devices.cameras" additivity="false" level="debug">
                        <appender-ref ref="VIDEO" />
                </logger>
                <root level="debug">
                        <appender-ref ref="DLA" />
                </root>
        </loggers>
</configuration>

Curt Smith



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message