Java Code Examples for ch.qos.logback.core.util.StatusPrinter#printInCaseOfErrorsOrWarnings()

The following examples show how to use ch.qos.logback.core.util.StatusPrinter#printInCaseOfErrorsOrWarnings() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ITestApplication.java    From camel-spring-boot with Apache License 2.0 7 votes vote down vote up
private static void overrideLoggingConfig() {

        URL logbackFile = ITestApplication.class.getResource("/spring-logback.xml");
        if (logbackFile != null) {

            LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

            try {
                JoranConfigurator configurator = new JoranConfigurator();
                configurator.setContext(context);
                // Call context.reset() to clear any previous configuration, e.g. default
                // configuration. For multi-step configuration, omit calling context.reset().
                context.reset();
                configurator.doConfigure(logbackFile);
            } catch (JoranException je) {
                // StatusPrinter will handle this
            }
            StatusPrinter.printInCaseOfErrorsOrWarnings(context);
        }

    }
 
Example 2
Source File: Global.java    From NationStatesPlusPlus with MIT License 6 votes vote down vote up
private static void setupLogback() {
	LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
	Logger.info("Setting up Logback");
	Logger.info("Application running from: {}", Start.getApplicationDirectory().getAbsolutePath());
	System.setProperty("application.home", Start.getApplicationDirectory().getAbsolutePath());
	File xmlConfig = new File(new File(Start.getApplicationDirectory(), "conf"), "application-logger.xml");
	if (xmlConfig.exists()) {
		try {
			JoranConfigurator configurator = new JoranConfigurator();
			configurator.setContext(context);
			context.reset();
			configurator.doConfigure(xmlConfig);
		} catch (JoranException je) {
			// StatusPrinter will handle this
		}
	}
	//System.setOut(new PrintStream(new LoggerOutputStream(Level.INFO, Logger.underlying()), true));
	System.setErr(new PrintStream(new LoggerOutputStream(Level.OFF, Logger.underlying()), true));
	Logger.info("Logback Setup");
	StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}
 
Example 3
Source File: LoggingConfiguration.java    From chassis with Apache License 2.0 6 votes vote down vote up
/**
 * Reloads logging.
 * 
 * @param logbackConfig XML containing the logback configuration
 */
public void reloadLogging(String logbackConfig) {
	LoggerContext logContext = (LoggerContext) LoggerFactory.getILoggerFactory();
	
	try {
		JoranConfigurator configurator = new JoranConfigurator();
		configurator.setContext(logContext);
		logContext.reset();
		configurator.doConfigure(new ByteArrayInputStream(logbackConfig.getBytes(Charsets.UTF_8)));
	} catch (JoranException je) {
		// StatusPrinter will handle this
	} catch (Exception ex) {
		ex.printStackTrace(); // Just in case, so we see a stacktrace
	}
	
	StatusPrinter.printInCaseOfErrorsOrWarnings(logContext);
	
	applicationContext.publishEvent(new LoggingReloadedApplicationEvent(this));
}
 
Example 4
Source File: LoggingUtilities.java    From waltz with Apache License 2.0 6 votes vote down vote up
/**
 * Initialises logging for Waltz.  This will attempt to locate a
 * file called <code>waltz-logback.xml</code> from either:
 * <ul>
 *     <li>
 *         root of classpath
 *     </li>
 *     <li>
 *         directory: <code>${user.home}/.waltz</code>
 *     </li>
 * </ul>
 *
 * Note: this file is allowed to use System.out to communicate
 * failures to the operator.
 */
public static void configureLogging() {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    try {
        JoranConfigurator configurator = new JoranConfigurator();
        context.reset();
        configurator.setContext(context);
        System.out.println("Attempting to load logback configuration file: " + LOG_CONFIG_FILE_NAME
                            + " from classpath or " + System.getProperty("user.home") + "/.waltz/");

        Resource logbackConfigFile = IOUtilities.getFileResource(LOG_CONFIG_FILE_NAME);

        if (logbackConfigFile.exists()) {
            System.out.println("Found logback configuration file: " + logbackConfigFile);
            try (InputStream configInputStream = logbackConfigFile.getInputStream()) {
                configurator.doConfigure(configInputStream);
            }
        } else {
            System.out.println("Logback configuration file not found..");
        }
    } catch (IOException | JoranException e) {
        // StatusPrinter will handle this
    }
    StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}
 
Example 5
Source File: LogbackPlugin.java    From dapeng-soa with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
    LOGGER.warn("Plugin::" + getClass().getSimpleName() + "::start");

    try (InputStream logbackCnfgStream = new BufferedInputStream(DapengContainer.loadInputStreamInClassLoader("logback.xml"))) {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(lc);
        lc.reset();
        configurator.doConfigure(logbackCnfgStream);

        StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
    } catch (Exception e) {
        LOGGER.error("LogbackContainer failed, ignoring ..." + e.getMessage(), e);
        // throw new RuntimeException(e);
    }
}
 
Example 6
Source File: InitLogBack.java    From es_data_export with Apache License 2.0 6 votes vote down vote up
public static void init() throws Exception{
		//String configFilepathName = FilePathHelper.getFilePathWithJar("logback.xml");
       File file = new File(Constant.LOGBACK_CONFIG_NAME);
       if(!file.exists()){
       	throw new NullPointerException("logback.xml为空");
       }
       LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
       //注意包不要引错
       JoranConfigurator joranConfigurator = new JoranConfigurator();
       joranConfigurator.setContext(loggerContext);
       loggerContext.reset();
       try {
           joranConfigurator.doConfigure(file);
       } catch (Exception e) {
       	throw e;
       }
       StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
}
 
Example 7
Source File: Application.java    From acme_client with MIT License 6 votes vote down vote up
private static void configureLogger(String logDir, String logLevel, String logbackConf) {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    try {
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(context);
        context.reset();
        if (!logDir.endsWith(File.separator))
            logDir+= File.separator;
        context.putProperty("LOG_DIR", logDir);
        context.putProperty("LOG_LEVEL", logLevel);

        InputStream is = classloader.getResourceAsStream(logbackConf);
        configurator.doConfigure(is);
    } catch (JoranException je) {
        LOG.warn("Cannot configure logger. Continue to execute the command.", je);
    }
    StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}
 
Example 8
Source File: Generator.java    From gd-generator with MIT License 6 votes vote down vote up
public static void load() throws JoranException {
    final String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<configuration debug=\"true\">\n" +
            "\t<appender name=\"STDOUT\" class=\"ch.qos.logback.core.ConsoleAppender\">\n" +
            "\t\t<encoder>\n" +
            "\t\t\t<charset>UTF-8</charset>\n" +
            "\t\t\t<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>\n" +
            "\t\t</encoder>\n" +
            "\t</appender>\n" +
            "\t<root level=\"INFO\">\n" +
            "\t\t<appender-ref ref=\"STDOUT\"/>\n" +
            "\t</root>\n" +
            "</configuration>";
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);
    lc.reset();
    configurator.doConfigure(new ByteArrayInputStream(s.getBytes(Charset.forName("UTF-8"))));
    StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
}
 
Example 9
Source File: RedisAppenderTest.java    From logback-redis-appender with Apache License 2.0 5 votes vote down vote up
protected void configLogger(String loggerxml) {
	LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

	try {
		JoranConfigurator configurator = new JoranConfigurator();
		configurator.setContext(context);
		context.reset();
		configurator.doConfigure(this.getClass().getResourceAsStream(loggerxml));
	} catch (JoranException je) {
		// StatusPrinter will handle this
	}
	StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}
 
Example 10
Source File: LogbackTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws JoranException {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);
    lc.reset();
    configurator.doConfigure(new File("src/test/resources/logback-example.xml"));
    StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
}
 
Example 11
Source File: LogModule.java    From guacamole-client with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {

    // Only load logback configuration if GUACAMOLE_HOME exists
    File guacamoleHome = environment.getGuacamoleHome();
    if (!guacamoleHome.isDirectory())
        return;

    // Check for custom logback.xml
    File logbackConfiguration = new File(guacamoleHome, "logback.xml");
    if (!logbackConfiguration.exists())
        return;

    logger.info("Loading logback configuration from \"{}\".", logbackConfiguration);

    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.reset();

    try {

        // Initialize logback
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(context);
        configurator.doConfigure(logbackConfiguration);

        // Dump any errors that occur during logback init
        StatusPrinter.printInCaseOfErrorsOrWarnings(context);

    }
    catch (JoranException e) {
        logger.error("Initialization of logback failed: {}", e.getMessage());
        logger.debug("Unable to load logback configuration..", e);
    }

}
 
Example 12
Source File: WarServerInitializer.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
private void setupLogging(Path homeDir) {
	LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
	try {
		JoranConfigurator configurator = new JoranConfigurator();
		configurator.setContext(context);
		context.reset();
		configurator.doConfigure(homeDir.resolve("logback.xml").toFile());
	} catch (JoranException je) {
		// StatusPrinter will handle this
	}
	StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}
 
Example 13
Source File: TplCLI.java    From LibScout with Apache License 2.0 5 votes vote down vote up
private static void initLogging() {
	LoggerContext context = (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory();
	    
	try {
		JoranConfigurator configurator = new JoranConfigurator();
	    configurator.setContext(context);
	    context.reset();  // clear any previous configuration 
	    configurator.doConfigure(LibScoutConfig.log4jConfigFileName);
	    
    	ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    	switch (LibScoutConfig.logType) {
			case CONSOLE:
				rootLogger.detachAppender("FILE");
				break;
			case FILE:
				rootLogger.detachAppender("CONSOLE");
				break;
			case NONE:
				rootLogger.detachAndStopAllAppenders();
				break;
    	}
	} catch (JoranException je) {
		// StatusPrinter will handle this
	}
	
	StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}
 
Example 14
Source File: LogbackTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws JoranException {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);
    lc.reset();
    configurator.doConfigure(new File("src/test/resources/logback-example.xml"));
    StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
}
 
Example 15
Source File: LogbackTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws JoranException {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);
    lc.reset();
    configurator.doConfigure(new File("src/test/resources/logback-example.xml"));
    StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
}
 
Example 16
Source File: LogModule.java    From guacamole-client with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {

    // Only load logback configuration if GUACAMOLE_HOME exists
    File guacamoleHome = environment.getGuacamoleHome();
    if (!guacamoleHome.isDirectory())
        return;

    // Check for custom logback.xml
    File logbackConfiguration = new File(guacamoleHome, "logback.xml");
    if (!logbackConfiguration.exists())
        return;

    logger.info("Loading logback configuration from \"{}\".", logbackConfiguration);

    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.reset();

    try {

        // Initialize logback
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(context);
        configurator.doConfigure(logbackConfiguration);

        // Dump any errors that occur during logback init
        StatusPrinter.printInCaseOfErrorsOrWarnings(context);

    }
    catch (JoranException e) {
        logger.error("Initialization of logback failed: {}", e.getMessage());
        logger.debug("Unable to load logback configuration..", e);
    }

}
 
Example 17
Source File: BookstoreLogger.java    From spring-data-rest-acl with Apache License 2.0 5 votes vote down vote up
private void init() {
	JoranConfigurator configurator = new JoranConfigurator();
	LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
       configurator.setContext(loggerContext);
       loggerContext.reset();
       try {
		configurator.doConfigure(logbackconfig);
	} catch (JoranException e) {
		// StatusPrinter will handle this
	}
       StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
       
       logger.info("Test Log");		
       logger.error("error msg");
}
 
Example 18
Source File: LogbackTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws JoranException {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);
    lc.reset();
    configurator.doConfigure(new File("src/test/resources/logback-example.xml"));
    StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
}
 
Example 19
Source File: LogbackTest.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws JoranException {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);
    lc.reset();
    configurator.doConfigure(new File("src/test/resources/logback-example.xml"));
    StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
}
 
Example 20
Source File: LogbackTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws JoranException {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);
    lc.reset();
    configurator.doConfigure(new File("src/test/resources/logback-example.xml"));
    StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
}