ch.qos.logback.classic.joran.JoranConfigurator Java Examples

The following examples show how to use ch.qos.logback.classic.joran.JoranConfigurator. 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: Syslog4jAppenderTest.java    From logback-syslog4j with MIT License 6 votes vote down vote up
public void testUdpSender() throws JoranException, InterruptedException {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    context.reset();
    configurator.doConfigure(this.getClass().getClassLoader().getResourceAsStream("logback-syslog4j-udp.xml"));

    Logger logger = context.getLogger("test-udp");
    logger.info("test message over udp");

    context.stop();
    Thread.sleep(100);

    final String serverData = serverStream.toString();
    assertTrue("Server received: " + serverData, serverData.contains("test message over udp"));
}
 
Example #3
Source File: App.java    From PeerWasp with MIT License 6 votes vote down vote up
/**
 * Initializes the logging framework.
 * The automatic configuration is reset and the logger is configured dynamically:
 * - The log folder is set
 * - The log configuration is loaded (not from resources, but from the working directory).
 *
 * This allows switching the folder and the configuration during development and at deployment.
 */
private void initializeLogging() {
	LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
	try {
		JoranConfigurator jc = new JoranConfigurator();
		jc.setContext(context);
		// override default configuration
		context.reset();
		// inject the location of the appdata log folder as "LOG_FOLDER"
		// property of the LoggerContext
		context.putProperty("LOG_FOLDER", AppData.getLogFolder().toString());
		jc.doConfigure(LOG_CONFIGURATION);
	} catch (JoranException je) {
		// status printer will handle printing of error
	}
	StatusPrinter.printInCaseOfErrorsOrWarnings(context);
	logger.debug("Initialized logging (LOG_FOLDER={})", context.getProperty("LOG_FOLDER"));
}
 
Example #4
Source File: ContextInitializer.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
public void configureByResource(URL url) throws JoranException {
    if (url == null) {
        throw new IllegalArgumentException("URL argument cannot be null");
    }
    final String urlString = url.toString();
    if (urlString.endsWith("groovy")) {
        if (EnvUtil.isGroovyAvailable()) {
            // avoid directly referring to GafferConfigurator so as to avoid
            // loading groovy.lang.GroovyObject . See also http://jira.qos.ch/browse/LBCLASSIC-214
            GafferUtil.runGafferConfiguratorOn(loggerContext, this, url);
        } else {
            StatusManager sm = loggerContext.getStatusManager();
            sm.add(new ErrorStatus("Groovy classes are not available on the class path. ABORTING INITIALIZATION.", loggerContext));
        }
    } else if (urlString.endsWith("xml")) {
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(loggerContext);
        configurator.doConfigure(url);
    } else {
        throw new LogbackException("Unexpected filename extension of file [" + url.toString() + "]. Should be either .groovy or .xml");
    }
}
 
Example #5
Source File: LogbackController.java    From GreenSummer with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @return The list of log levels configured and their settings
 */
@RequestMapping(value = "reset", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET, headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<LogResponse> reset() {
    final LoggerContext ctx = (LoggerContext) LoggerFactory.getILoggerFactory();
    synchronized (ctx) {
        File configuration = ConfigurationWatchListUtil.getConfigurationWatchList(ctx).getCopyOfFileWatchList().get(0);
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(ctx);
        ctx.reset();
        try {
            configurator.doConfigure(configuration);
        } catch (JoranException e) {
            log.error("Error re-applying the configuration");
        }
    }
    return new ResponseEntity<>(listLoggers(ctx), HttpStatus.OK);
}
 
Example #6
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 #7
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 #8
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 #9
Source File: GossipLogModule.java    From gossip with MIT License 6 votes vote down vote up
private void initializeLogback() {
    Path logbackFilePath = Paths.get(configPath, "logback.xml");
    if (logbackFilePath.toFile().exists()) {
        try {
            // Load logback configuration
            LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
            context.reset();
            JoranConfigurator configurator = new JoranConfigurator();
            configurator.setContext(context);
            configurator.doConfigure(logbackFilePath.toFile());

            // Install java.util.logging bridge
            SLF4JBridgeHandler.removeHandlersForRootLogger();
            SLF4JBridgeHandler.install();
        } catch (JoranException e) {
            throw new GossipInitializeException("Misconfiguration on logback.xml, check it.", e);
        }
    }
}
 
Example #10
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 #11
Source File: LogbackConfigurationListener.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
@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);
        }
    }
}
 
Example #12
Source File: Syslog4jAppenderTest.java    From logback-syslog4j with MIT License 6 votes vote down vote up
public void testTlsSender() throws JoranException, InterruptedException {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    context.reset();
    configurator.doConfigure(this.getClass().getClassLoader().getResourceAsStream("logback-syslog4j-tls.xml"));

    Logger logger = context.getLogger("test-tls");
    logger.info("test message over tls");

    context.stop();
    Thread.sleep(100);

    final String serverData = serverStream.toString();
    assertTrue("Server received: " + serverData, serverData.contains("test message over tls"));
}
 
Example #13
Source File: LogInitUtil.java    From Juice with GNU General Public License v3.0 6 votes vote down vote up
public static void initLog() {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator jc = new JoranConfigurator();
    jc.setContext(context);
    context.reset();

    String env = System.getProperty("system.environment");
    if(StringUtils.isBlank(env)) {
        System.err.println("get system.environment error");
        throw new RuntimeException("can't get env, service stop!");
    }
    URL tmpConfigFIleStr = Startup.class.getResource("/logback-" + env + ".xml");
    try {
        System.out.println("start with configFile : " + tmpConfigFIleStr);
        jc.doConfigure(tmpConfigFIleStr);
        log.info("load logback config --> " + tmpConfigFIleStr.getFile());
    } catch (JoranException e) {
        System.err.println(tmpConfigFIleStr + " not exist");
        throw new RuntimeException(e);
    }
}
 
Example #14
Source File: LogbackConfiguratorListener.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent event) {
    // 从web.xml中加载指定文件名的日志配置文件
    String logbackConfigLocation = event.getServletContext().getInitParameter(CONFIG_LOCATION);
    InputStream fn = event.getClass().getClassLoader().getResourceAsStream(logbackConfigLocation);
    if (fn == null) {
        return;
    }
    try {
        LoggerContext loggerContext = (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory();
        loggerContext.reset();
        JoranConfigurator joranConfigurator = new JoranConfigurator();
        joranConfigurator.setContext(loggerContext);
        joranConfigurator.doConfigure(fn);
        logger.debug("loaded slf4j configure file from " + fn);
    } catch (JoranException e) {
        logger.error("can loading slf4j configure file from " + fn, e);
    }
}
 
Example #15
Source File: LogUtils.java    From litchi with Apache License 2.0 6 votes vote down vote up
public static void loadFileConfig(String configFilePath) throws IOException, JoranException {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    File externalConfigFile = new File(configFilePath);

    if (!externalConfigFile.isFile()) {
        throw new IOException("logback config file not exists. configFilePath = " + configFilePath);
    }

    if (!externalConfigFile.canRead()) {
        throw new IOException("logback config file cannot be read. configFilePath = " + configFilePath);
    }

    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);
    lc.reset();
    configurator.doConfigure(configFilePath);
    StatusPrinter.printInCaseOfErrorsOrWarnings(lc);

}
 
Example #16
Source File: PluginLoggerFactory.java    From stash-codesearch-plugin with Apache License 2.0 6 votes vote down vote up
private void init() {
    // Assumes LSF4J is bound to logback
    context = (LoggerContext) LoggerFactory.getILoggerFactory();

    // store the home dir to use for relative paths
    context.putProperty("stash.home", homeDir);

    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);

    InputStream is;
    is = this.getClass().getClassLoader().getResourceAsStream("logback-test.xml");
    if (is != null) {
        stashRootLogger.info("Using logback-test.xml for logger settings");
    } else {
        stashRootLogger.info("Using logback.xml for logger settings");
        is = this.getClass().getClassLoader().getResourceAsStream("logback.xml");
    }

    try {
        configurator.doConfigure(is);
    } catch (JoranException e) {
        System.err.println("Error configuring logging framework" + e);
    }
    stashRootLogger.info("Logger using stash.home of " + homeDir);
}
 
Example #17
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 #18
Source File: Syslog4jAppenderTest.java    From logback-syslog4j with MIT License 6 votes vote down vote up
public void testTcpSender() throws JoranException, InterruptedException {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    context.reset();
    configurator.doConfigure(this.getClass().getClassLoader().getResourceAsStream("logback-syslog4j-tcp.xml"));

    Logger logger = context.getLogger("test-tcp");
    logger.info("test message over tcp");

    context.stop();
    Thread.sleep(100);

    final String serverData = serverStream.toString();
    assertTrue("Server received: " + serverData, serverData.contains("test message over tcp"));
}
 
Example #19
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 #20
Source File: MQAdminStartup.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private static void initLogback() throws JoranException {
    String rocketmqHome = System.getProperty(MixAll.ROCKETMQ_HOME_PROPERTY, System.getenv(MixAll.ROCKETMQ_HOME_ENV));

    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);
    lc.reset();
    configurator.doConfigure(rocketmqHome + "/conf/logback_tools.xml");
}
 
Example #21
Source File: MQAdminStartup.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
private static void initLogback() throws JoranException {
   // String rocketmqHome = System.getProperty(MixAll.ROCKETMQ_HOME_PROPERTY, System.getenv(MixAll.ROCKETMQ_HOME_ENV));

    String rocketmqHome = "D:\\\\eclipse-workspace\\\\rocketmq-rocketmq-all-4.1.0-incubating\\\\rocketmq-rocketmq-all-4.1.0-incubating\\\\distribution";

    // 初始化Logback
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);
    lc.reset();
    configurator.doConfigure(rocketmqHome + "/conf/logback_tools.xml");
}
 
Example #22
Source File: LogbackConfigurer.java    From icure-backend with GNU General Public License v2.0 5 votes vote down vote up
public static void initLogging(URL configUrl) {
	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(configUrl);
	} catch (JoranException je) {
		// StatusPrinter will handle this
	}
	StatusPrinter.printIfErrorsOccured(context);
}
 
Example #23
Source File: LoggingConfigurator.java    From logging-java with Apache License 2.0 5 votes vote down vote up
/**
 * Configure logging using a logback configuration file.
 *
 * @param file         A logback configuration file.
 * @param defaultIdent Fallback logging identity, used if not specified in config file.
 */
public static void configure(final File file, final String defaultIdent) {
  final Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

  // Setup context
  final LoggerContext context = rootLogger.getLoggerContext();
  context.reset();

  // Log uncaught exceptions
  UncaughtExceptionLogger.setDefaultUncaughtExceptionHandler();

  // Load logging configuration from file
  try {
    final JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    configurator.doConfigure(file);
  } catch (JoranException je) {
    // StatusPrinter will handle this
  }

  context.putProperty("pid", getMyPid());
  final String hostname = getSpotifyHostname();
  if (hostname != null) {
    context.putProperty("hostname", hostname);
  }

  final String ident = context.getProperty("ident");
  if (ident == null) {
    context.putProperty("ident", defaultIdent);
  }

  StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}
 
Example #24
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 #25
Source File: MultiLangDaemonTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testSuccessfulLoggingConfiguration() {
    LoggerContext loggerContext = spy((LoggerContext) LoggerFactory.getILoggerFactory());
    JoranConfigurator configurator = spy(new JoranConfigurator());

    String logConfiguration = this.getClass().getClassLoader().getResource("logback.xml").getPath();
    daemon.configureLogging(logConfiguration, loggerContext, configurator);

    verify(loggerContext).reset();
    verify(configurator).setContext(eq(loggerContext));
}
 
Example #26
Source File: MQAdminStartup.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
private static void initLogback() throws JoranException {
    String rocketmqHome =
            System.getProperty(MixAll.ROCKETMQ_HOME_PROPERTY, System.getenv(MixAll.ROCKETMQ_HOME_ENV));

    // 初始化Logback
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);
    lc.reset();
    configurator.doConfigure(rocketmqHome + "/conf/logback_tools.xml");
}
 
Example #27
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 #28
Source File: MQAdminStartup.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private static void initLogback() throws JoranException {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(lc);
    lc.reset();
    configurator.doConfigure(rocketmqHome + "/conf/logback_tools.xml");
}
 
Example #29
Source File: Slf4jLoggerFactoryTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Before
public void initLogback() throws JoranException {
    InternalLoggerFactory.setCurrentLoggerType(InternalLoggerFactory.LOGGER_SLF4J);
    System.setProperty("loggingDir", loggingDir);
    ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
    JoranConfigurator joranConfigurator = new JoranConfigurator();
    joranConfigurator.setContext((Context) iLoggerFactory);
    URL logbackConfigFile = Slf4jLoggerFactoryTest.class.getClassLoader().getResource("logback_test.xml");
    if (logbackConfigFile == null) {
        throw new RuntimeException("can't find logback_test.xml");
    } else {
        joranConfigurator.doConfigure(logbackConfigFile);
    }
}
 
Example #30
Source File: Main.java    From digdag with Apache License 2.0 5 votes vote down vote up
private static void configureLogging(String level, String logPath)
{
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    context.reset();

    // logback uses system property to embed variables in XML file
    Level lv = Level.toLevel(level.toUpperCase(), Level.DEBUG);
    System.setProperty("digdag.log.level", lv.toString());

    String name;
    if (logPath.equals("-")) {
        if (System.console() != null) {
            name = "/digdag/cli/logback-color.xml";
        } else {
            name = "/digdag/cli/logback-console.xml";
        }
    } else {
        System.setProperty("digdag.log.path", logPath);
        name = "/digdag/cli/logback-file.xml";
    }
    try {
        configurator.doConfigure(Main.class.getResource(name));
    } catch (JoranException ex) {
        throw new RuntimeException(ex);
    }
}