Skip to content

Instantly share code, notes, and snippets.

@ariens
Created March 20, 2015 15:29
Show Gist options
  • Save ariens/78504d5e23e33b6aaae4 to your computer and use it in GitHub Desktop.
Save ariens/78504d5e23e33b6aaae4 to your computer and use it in GitHub Desktop.
InstrumentedLoggerSingleton
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();
}
}
@ariens
Copy link
Author

ariens commented Oct 8, 2015

log4j2 only, handy though...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment