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; }
return StaticLoggerBinder.getSingleton().getLoggerFactory(); case NOP_FALLBACK_INITIALIZATION: return NOP_FALLBACK_FACTORY;
return StaticLoggerBinder.getSingleton().getLoggerFactory(); case NOP_FALLBACK_INITIALIZATION: return NOP_FALLBACK_FACTORY;
/** * Creates a file appender and adds it to logback. * * @param verboseLog the path to the verbose log file */ private void prepareLogger(String verboseLog) { final StaticLoggerBinder loggerBinder = StaticLoggerBinder.getSingleton(); final LoggerContext context = (LoggerContext) loggerBinder.getLoggerFactory(); final PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setPattern("%d %C:%L%n%-5level - %msg%n"); encoder.setContext(context); encoder.start(); final FileAppender<ILoggingEvent> fa = new FileAppender<>(); fa.setAppend(true); fa.setEncoder(encoder); fa.setContext(context); fa.setFile(verboseLog); final File f = new File(verboseLog); String name = f.getName(); final int i = name.lastIndexOf('.'); if (i > 1) { name = name.substring(0, i); } fa.setName(name); fa.start(); final ch.qos.logback.classic.Logger rootLogger = context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME); rootLogger.addAppender(fa); }
/** * Return the {@link ILoggerFactory} instance in use. * <p/> * <p/> * ILoggerFactory instance is bound with this class at compile time. * * @return the ILoggerFactory instance in use */ public static ILoggerFactory getILoggerFactory() { if (INITIALIZATION_STATE == UNINITIALIZED) { INITIALIZATION_STATE = ONGOING_INITIALIZATION; performInitialization(); } switch (INITIALIZATION_STATE) { case SUCCESSFUL_INITIALIZATION: return StaticLoggerBinder.getSingleton().getLoggerFactory(); case NOP_FALLBACK_INITIALIZATION: return NOP_FALLBACK_FACTORY; case FAILED_INITIALIZATION: throw new IllegalStateException(UNSUCCESSFUL_INIT_MSG); case ONGOING_INITIALIZATION: // support re-entrant behavior. // See also http://bugzilla.slf4j.org/show_bug.cgi?id=106 return TEMP_FACTORY; } throw new IllegalStateException("Unreachable code"); } }
/** * Returns the current logger-context. */ @VisibleForTesting static LoggerContext loggerContext() { ILoggerFactory factory = LoggerFactory.getILoggerFactory(); if (factory instanceof LoggerContext) { return (LoggerContext) factory; } // Pax-Logging registers a custom implementation of ILoggerFactory which hides logback; as a workaround // we set org.ops4j.pax.logging.StaticLogbackContext=true in system.properties and access it statically return (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory(); }
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; }
protected void initLogger() { ILoggerFactory loggerFactory = StaticLoggerBinder.getSingleton().getLoggerFactory(); logger = loggerFactory.getLogger("org.eclipse.jetty"); }
@Override public void onApplicationEvent(ApplicationEvent event) { final String settings = environment.getProperty("logging.config.src"); if (StringUtils.hasText(settings)) { try { final ContextBase context = (ContextBase) StaticLoggerBinder.getSingleton().getLoggerFactory(); final JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(context); LOG.info("try to update logback configuration to {}", settings); context.reset(); configurator.doConfigure(new ByteArrayInputStream(settings.getBytes())); } catch (JoranException e) { LOG.error("can't load settings", e); } } } }
/** * Return the {@link ILoggerFactory} instance in use. * * <p> * ILoggerFactory instance is bound with this class at compile time. * * @return the ILoggerFactory instance in use */ public static ILoggerFactory getILoggerFactory() { if (INITIALIZATION_STATE == UNINITIALIZED) { INITIALIZATION_STATE = ONGOING_INITILIZATION; performInitialization(); } switch (INITIALIZATION_STATE) { case SUCCESSFUL_INITILIZATION: return getSingleton().getLoggerFactory(); case FAILED_INITILIZATION: throw new IllegalStateException(UNSUCCESSFUL_INIT_MSG); case ONGOING_INITILIZATION: // support re-entrant behavior. // See also http://bugzilla.slf4j.org/show_bug.cgi?id=106 return TEMP_FACTORY; } throw new IllegalStateException("Unreachable code"); } }
protected void initializeLogging() { String graviteeHome = System.getProperty(GRAVITEE_HOME_PROPERTY); String logbackConfiguration = graviteeHome + File.separator + "config" + File.separator + "logback.xml"; File logbackConfigurationfile = new File(logbackConfiguration); // If logback configuration available, load it, else, load default logback configuration if (logbackConfigurationfile.exists()) { System.setProperty("logback.configurationFile", logbackConfigurationfile.getAbsolutePath()); StaticLoggerBinder loggerBinder = StaticLoggerBinder.getSingleton(); LoggerContext loggerContext = (LoggerContext) loggerBinder.getLoggerFactory(); loggerContext.reset(); JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(loggerContext); try { configurator.doConfigure(logbackConfigurationfile); } catch( JoranException e ) { LoggerFactory.getLogger(this.getClass()).error("An error occurs while initializing logging system", e); } // Internal status data is printed in case of warnings or errors. StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext); } }
return StaticLoggerBinder.getSingleton().getLoggerFactory(); case NOP_FALLBACK_INITIALIZATION: return NOP_FALLBACK_FACTORY;
private static void injectLoggerIntoLoggerFactory(final Class loggingClass, final Logger logger) throws Exception { final SimpleLoggerFactory simpleLoggerFactory = (SimpleLoggerFactory) StaticLoggerBinder.getSingleton().getLoggerFactory(); final Field loggerMapField = SimpleLoggerFactory.class.getDeclaredField("loggerMap"); loggerMapField.setAccessible(true); final Map loggerMap = (Map) loggerMapField.get(simpleLoggerFactory); loggerMap.put(loggingClass.getName(), logger); } }
private void initializeLogging() { String graviteeHome = System.getProperty("gravitee.home"); String logbackConfiguration = graviteeHome + File.separator + "config" + File.separator + "logback.xml"; File logbackConfigurationfile = new File(logbackConfiguration); // If logback configuration available, load it, else, load default logback configuration if (logbackConfigurationfile.exists()) { System.setProperty("logback.configurationFile", logbackConfigurationfile.getAbsolutePath()); StaticLoggerBinder loggerBinder = StaticLoggerBinder.getSingleton(); LoggerContext loggerContext = (LoggerContext) loggerBinder.getLoggerFactory(); loggerContext.reset(); JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(loggerContext); try { configurator.doConfigure(logbackConfigurationfile); } catch( JoranException e ) { e.printStackTrace(); } // Internal status data is printed in case of warnings or errors. StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext); } }
LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();
public static void initialise(Log log) { StaticLoggerBinder loggerBinder = StaticLoggerBinder.getSingleton(); LoggerContext loggerContext = (LoggerContext) loggerBinder.getLoggerFactory(); MavenAppender mavenAppender = new MavenAppender(log); mavenAppender.setContext(loggerContext); mavenAppender.start(); Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); LogbackUtils.syncLogLevelWithMaven(rootLogger, log); rootLogger.detachAndStopAllAppenders(); rootLogger.addAppender(mavenAppender); StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext); } }
@Override public void changeAll(LoggerLevel loggerLevel) { ILoggerFactory loggerFactory = StaticLoggerBinder.getSingleton().getLoggerFactory(); if (!(loggerFactory instanceof LoggerContext)) return; Level newLevel = transToLogback(loggerLevel); for (Logger logger : ((LoggerContext) loggerFactory).getLoggerList()) { changeToNewLevel(newLevel, logger); } }
private void configureLogging() { NSLoggerAdapter adapter = (NSLoggerAdapter) StaticLoggerBinder.getSingleton().getLoggerFactory().getLogger(Node.class.getName()); adapter.logger = new NSLogger(activity.getApplicationContext()); adapter.logger.connect("192.168.5.203", 50000); Underdark.configureLogging(true); }
/** * Initialize logback from the given file. * * @param location the location of the config file: either a "classpath:" location * (e.g. "classpath:logback.xml"), an absolute file URL * (e.g. "file:C:/logback.xml), or a plain absolute path in the file system * (e.g. "C:/logback.xml") * @throws java.io.FileNotFoundException if the location specifies an invalid file path * @throws ch.qos.logback.core.joran.spi.JoranException * Thrown */ public static void initLogging(String location) throws FileNotFoundException, JoranException { String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location); URL url = ResourceUtils.getURL(resolvedLocation); LoggerContext loggerContext = (LoggerContext)StaticLoggerBinder.getSingleton().getLoggerFactory(); // in the current version logback automatically configures at startup the context, so we have to reset it loggerContext.reset(); // reinitialize the logger context. calling this method allows configuration through groovy or xml new ContextInitializer(loggerContext).configureByResource(url); }
@Override public void changeSome(String loggerWildcard, LoggerLevel loggerLevel) { ILoggerFactory loggerFactory = StaticLoggerBinder.getSingleton().getLoggerFactory(); if (!(loggerFactory instanceof LoggerContext)) return; Level newLevel = transToLogback(loggerLevel); for (Logger logger : ((LoggerContext) loggerFactory).getLoggerList()) { if (FilenameUtils.wildcardMatch(logger.getName(), loggerWildcard)) changeToNewLevel(newLevel, logger); } } }