public void autoConfig() throws JoranException { StatusListenerConfigHelper.installIfAsked(loggerContext); URL url = findURLOfDefaultConfigurationFile(true); if (url != null) { configureByResource(url); } else { Configurator c = EnvUtil.loadFromServiceLoader(Configurator.class); if (c != null) { try { c.setContext(loggerContext); c.configure(loggerContext); } catch (Exception e) { throw new LogbackException(String.format("Failed to initialize Configurator: %s using ServiceLoader", c != null ? c.getClass().getCanonicalName() : "null"), e); } } else { BasicConfigurator.configure(loggerContext); } } }
/** * 加载logback配置 * @param location 文件路径 * @throws FileNotFoundException 文件不存在异常 */ public static void initLogging(String location) throws FileNotFoundException { if (LoggerFactory.getILoggerFactory() instanceof LoggerContext) { String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location); URL url = ResourceUtils.getURL(resolvedLocation); LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); try { JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(loggerContext); loggerContext.reset(); configurator.doConfigure(url); } catch (Throwable e) { BasicConfigurator.configureDefaultContext(); Logger log = LoggerFactory.getLogger(MODULE); log.warn("Logback configure fail!", e); } } }
private static void initializeLogging(Arguments arguments) { //bootstrap depends on logback for logging console output. if the clientApplication does not want logback, they //need to exclude logback from their project dependencies and initialize their own logging try { AppMain.class.getClassLoader().loadClass("ch.qos.logback.classic.LoggerContext"); } catch (ClassNotFoundException e) { System.err.println("No Logback found in classpath. Skipping logging initialization. This is expected if you are configuring the logging system yourself. If you are not configuring logging yourself and would like to use logging provided by java-bootstrap, ensure that the Logback dependency jar exists in your classpath."); return; } LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); context.reset(); BasicConfigurator.configure(context); ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); Level level = Level.valueOf(arguments.logLevel.name().toUpperCase()); logger.setLevel(level); logger.info("Initialized logging at {} level", level); }
protected LoggerContext initLogging() throws IOException { LogManager.getLogManager().reset(); LogManager.getLogManager().readConfiguration(); LogManager.getLogManager().getLogger("").setLevel(java.util.logging.Level.INFO); final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); context.reset(); BasicConfigurator configurator = new BasicConfigurator(); configurator.setContext(context); configurator.configure(context); context.getLogger(Logger.ROOT_LOGGER_NAME).setLevel(Level.INFO); org.slf4j.bridge.SLF4JBridgeHandler.removeHandlersForRootLogger(); org.slf4j.bridge.SLF4JBridgeHandler.install(); return context; }
@Before public void resetLogging() { LoggerContext loggerContext = ((Logger) LoggerFactory.getLogger(getClass())) .getLoggerContext(); loggerContext.reset(); new BasicConfigurator().configure(loggerContext); }
@Before public void resetLogging() { LoggerContext loggerContext = ((Logger) LoggerFactory.getLogger(getClass())) .getLoggerContext(); loggerContext.reset(); BasicConfigurator.configure(loggerContext); }
@Before public void addHandler() throws Exception { mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); serializedEvents.clear(); final OnConsoleStatusListener listener = new OnConsoleStatusListener(); listener.start(); context.getStatusManager().add(listener); final JsonLogEncoder encoder = new JsonLogEncoder() { @Override public ObjectNode convertToObjectNode(ILoggingEvent event) { ObjectNode node = super.convertToObjectNode(event); serializedEvents.add(node); return node; } }; encoder.setContext(context); final UnsynchronizedAppenderBase<ILoggingEvent> captureAppender = new UnsynchronizedAppenderBase<ILoggingEvent>() { @Override protected void append(ILoggingEvent eventObject) { encoder.encode(eventObject); } }; captureAppender.setContext(context); captureAppender.start(); context.getLogger(Logger.ROOT_LOGGER_NAME).addAppender(captureAppender); new BasicConfigurator().configure(context); context.start(); }
@After public void reset() { this.context.stop(); new BasicConfigurator() .configure((LoggerContext) LoggerFactory.getILoggerFactory()); }
@Before public void setUp() { loggerContext.setName("LB139"); BasicConfigurator.configure(loggerContext); }
protected void configureDefaultLogging() { ((LoggerContext) loggerFactory).reset(); // reset will cause log level to be set to debug, so we set it to something more useful LogHelper.rootLogger().setLevel(Level.INFO); new BasicConfigurator().configure((LoggerContext) loggerFactory); }