Created
March 20, 2015 15:29
-
-
Save ariens/78504d5e23e33b6aaae4 to your computer and use it in GitHub Desktop.
InstrumentedLoggerSingleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InstrumentedLoggerSingleton implements ConfigurationListener | |
{ | |
private static final Logger LOG = LoggerFactory.getLogger(InstrumentedLoggerSingleton.class); | |
private static InstrumentedLoggerSingleton instance = null; | |
private final InstrumentedAppender appender; | |
private final Filter filter = null; | |
private final PatternLayout layout = null; | |
public static synchronized InstrumentedLoggerSingleton getInstance() | |
{ | |
if (instance == null) | |
{ | |
instance = new InstrumentedLoggerSingleton(); | |
} | |
return instance; | |
} | |
private InstrumentedLoggerSingleton() | |
{ | |
appender = new InstrumentedAppender(MetricRegistrySingleton.getInstance().getMetricsRegistry(), filter, layout, false); | |
appender.start(); | |
instrument(); | |
} | |
private void instrument() | |
{ | |
LoggerContext context = (LoggerContext) LogManager.getContext(false); | |
Configuration config = context.getConfiguration(); | |
config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).addAppender(appender, Level.ALL, filter); | |
config.addListener(this); | |
context.updateLoggers(config); | |
LOG.info("Logging configuration has been modified and a new InstrumentedAppender has been configured on the root logger"); | |
} | |
public void onChange(Reconfigurable reconfigurable) | |
{ | |
instrument(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
log4j2 only, handy though...