use of org.slf4j.ILoggerFactory in project weave by continuuity.
the class ServiceMain method configureLogger.
private void configureLogger() {
// Check if SLF4J is bound to logback in the current environment
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
if (!(loggerFactory instanceof LoggerContext)) {
return;
}
LoggerContext context = (LoggerContext) loggerFactory;
context.reset();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
try {
File weaveLogback = new File(Constants.Files.LOGBACK_TEMPLATE);
if (weaveLogback.exists()) {
configurator.doConfigure(weaveLogback);
}
new ContextInitializer(context).autoConfig();
} catch (JoranException e) {
throw Throwables.propagate(e);
}
doConfigure(configurator, getLogConfig(getLoggerLevel(context.getLogger(Logger.ROOT_LOGGER_NAME))));
}
use of org.slf4j.ILoggerFactory in project cdap by caskdata.
the class LogStageInjector method start.
/**
* Hijacks the appenders for the root logger and replaces them with a {@link LogStageAppender} that will insert
* the ETL stage name at the start of each message if the stage name is set. Uses {@link org.slf4j.MDC} to look up
* the current stage name.
*/
public static void start() {
if (!initialized.compareAndSet(false, true)) {
return;
}
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
if (!(loggerFactory instanceof LoggerContext)) {
LOG.warn("LoggerFactory is not a logback LoggerContext. Stage names will not be injected into log messages.");
return;
}
LoggerContext loggerContext = (LoggerContext) loggerFactory;
ch.qos.logback.classic.Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
List<Appender<ILoggingEvent>> appenders = new ArrayList<>();
Iterator<Appender<ILoggingEvent>> appenderIterator = rootLogger.iteratorForAppenders();
while (appenderIterator.hasNext()) {
Appender<ILoggingEvent> appender = appenderIterator.next();
// appender will have a different classloader than LogStageAppender...
if (appender.getClass().getName().equals(LogStageAppender.class.getName())) {
return;
}
appenders.add(appender);
}
Appender<ILoggingEvent> stageAppender = new LogStageAppender(appenders);
stageAppender.setContext(loggerContext);
stageAppender.start();
rootLogger.addAppender(stageAppender);
// To avoid duplicate messages, need to detach the original appenders
for (Appender<ILoggingEvent> appender : appenders) {
rootLogger.detachAppender(appender);
}
}
use of org.slf4j.ILoggerFactory in project weave by continuuity.
the class ServiceMain method doMain.
protected final void doMain(final ZKClientService zkClientService, final Service service) throws ExecutionException, InterruptedException {
configureLogger();
final String serviceName = service.toString();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
Services.chainStop(service, zkClientService);
}
});
// Listener for state changes of the service
ListenableFuture<Service.State> completion = Services.getCompletionFuture(service);
// Starts the service
LOG.info("Starting service {}.", serviceName);
Futures.getUnchecked(Services.chainStart(zkClientService, service));
LOG.info("Service {} started.", serviceName);
try {
completion.get();
LOG.info("Service {} completed.", serviceName);
} catch (Throwable t) {
LOG.warn("Exception thrown from service {}.", serviceName, t);
throw Throwables.propagate(t);
} finally {
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
if (loggerFactory instanceof LoggerContext) {
((LoggerContext) loggerFactory).stop();
}
}
}
use of org.slf4j.ILoggerFactory in project spring-boot by spring-projects.
the class LogbackLoggingSystem method getLoggerContext.
private LoggerContext getLoggerContext() {
ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory();
Assert.isInstanceOf(LoggerContext.class, factory, String.format("LoggerFactory is not a Logback LoggerContext but Logback is on " + "the classpath. Either remove Logback or the competing " + "implementation (%s loaded from %s). If you are using " + "WebLogic you will need to add 'org.slf4j' to " + "prefer-application-packages in WEB-INF/weblogic.xml", factory.getClass(), getLocation(factory)));
return (LoggerContext) factory;
}
use of org.slf4j.ILoggerFactory in project spring-boot by spring-projects.
the class LogbackLoggingSystemTests method testBasicConfigLocation.
@Test
public void testBasicConfigLocation() throws Exception {
this.loggingSystem.beforeInitialize();
ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory();
LoggerContext context = (LoggerContext) factory;
Logger root = context.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
assertThat(root.getAppender("CONSOLE")).isNotNull();
}
Aggregations