Java Code Examples for ch.qos.logback.core.OutputStreamAppender#setContext()

The following examples show how to use ch.qos.logback.core.OutputStreamAppender#setContext() . 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: LogbackCapture.java    From emissary with Apache License 2.0 5 votes vote down vote up
private static OutputStreamAppender<ILoggingEvent> buildAppender(final Encoder<ILoggingEvent> encoder, final OutputStream outputStream) {
    OutputStreamAppender<ILoggingEvent> appender = new OutputStreamAppender<ILoggingEvent>();
    appender.setName("logcapture");
    appender.setContext(ContextSelectorStaticBinder.getSingleton().getContextSelector().getDefaultLoggerContext());
    appender.setEncoder(encoder);
    appender.setOutputStream(outputStream);
    appender.start();
    return appender;
}
 
Example 2
Source File: LogbackUtils.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
static void activateLogging(OutputStreamAppender<ILoggingEvent> appender) {
	PatternLayoutEncoder ple = new PatternLayoutEncoder();
	ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
	ple.setContext(getContext());
	ple.start();
	appender.setContext(getContext());
	appender.setEncoder(ple);
	appender.start();
	
	// add appender to any Logger (here is root)
	ch.qos.logback.classic.Logger rootLogger = getRootLogger();
	rootLogger.addAppender(appender);
}
 
Example 3
Source File: LogbackConfiguration.java    From portmapper with GNU General Public License v3.0 5 votes vote down vote up
private OutputStreamAppender<ILoggingEvent> createAppender(final OutputStream logMessageOutputStream,
        final Encoder<ILoggingEvent> encoder) {
    final OutputStreamAppender<ILoggingEvent> appender = new OutputStreamAppender<ILoggingEvent>();
    appender.setContext(loggerContext);
    appender.setEncoder(encoder);
    appender.setOutputStream(logMessageOutputStream);
    appender.setName(OUTPUT_STREAM_APPENDER_NAME);
    appender.start();
    return appender;
}
 
Example 4
Source File: OfframpConsole.java    From data-highway with Apache License 2.0 4 votes vote down vote up
/**
 * Helper function to configure where message and CLI output is directed as well as configure logging
 * This should be called first to ensure correct output of message and cli logging into the desired stream.
 */
private void configureOutput() {
  try {
    if (flipOutput) {
      PrintStream tmp = msgout;
      msgout = cliout;
      cliout = tmp;
    }

    // change output format from json to yaml
    if (format == Format.YAML) {
      mapper = new YAMLMapper();
    }

    // retrieve the ch.qos.logback.classic.LoggerContext
    LoggerContext logCtx = (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory();

    PatternLayoutEncoder logEncoder = new PatternLayoutEncoder();
    logEncoder.setContext(logCtx);
    logEncoder.setPattern("%d{HH:mm:ss.SSS} - %-5level %logger{35} - %msg%n");
    logEncoder.start();

    OutputStreamAppender logConsoleAppender = new OutputStreamAppender();
    logConsoleAppender.setContext(logCtx);
    logConsoleAppender.setOutputStream(cliout);
    logConsoleAppender.setName("console");
    logConsoleAppender.setEncoder(logEncoder);
    logConsoleAppender.start();

    Logger rootLogger = logCtx.getLogger("root");
    rootLogger.detachAndStopAllAppenders();
    rootLogger.setLevel(Level.WARN);
    rootLogger.addAppender(logConsoleAppender);

    Logger roadLogger = logCtx.getLogger("com.hotels.road");
    roadLogger.setLevel(Level.WARN);

    Logger clientLogger = logCtx.getLogger("com.hotels.road.offramp.client");
    clientLogger.setLevel(debug ? Level.DEBUG : Level.INFO);

    Logger cliLogger = logCtx.getLogger("com.hotels.road.tool.cli");
    cliLogger.setLevel(debug ? Level.DEBUG : Level.INFO);
  } catch (Exception e) {
    System.err.println("Error configuring the message and cli output:");
    e.printStackTrace();
    System.exit(Error.OUTPUT_CONFIGURATION.code);
  }
}