Java Code Examples for ch.qos.logback.core.ConsoleAppender#setTarget()

The following examples show how to use ch.qos.logback.core.ConsoleAppender#setTarget() . 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: LogbackLoggingConfigurer.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void doFailSafeConfiguration() {
    // Not really fail-safe, just less likely to fail
    final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.reset();
    Logger rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME);
    rootLogger.setLevel(Level.INFO);

    ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
    rootLogger.addAppender(appender);
    appender.setContext(context);
    appender.setTarget("System.err");

    PatternLayout layout = new PatternLayout();
    appender.setLayout(layout);
    layout.setPattern("%msg%n%ex");
    layout.setContext(context);

    layout.start();
    appender.start();
}
 
Example 2
Source File: LogbackLoggingConfigurer.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void doFailSafeConfiguration() {
    // Not really fail-safe, just less likely to fail
    final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.reset();
    Logger rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME);
    rootLogger.setLevel(Level.INFO);

    ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
    rootLogger.addAppender(appender);
    appender.setContext(context);
    appender.setTarget("System.err");

    PatternLayout layout = new PatternLayout();
    appender.setLayout(layout);
    layout.setPattern("%msg%n%ex");
    layout.setContext(context);

    layout.start();
    appender.start();
}
 
Example 3
Source File: BrokerConsoleLoggerImpl.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@Override
protected Appender<ILoggingEvent> createAppenderInstance(Context context)
{
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();

    final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setPattern(getLayout());
    encoder.setContext(context);
    encoder.start();

    if (_consoleStreamTarget == ConsoleStreamTarget.STDERR)
    {
        consoleAppender.setTarget("System.err");
    }
    consoleAppender.setEncoder(encoder);

    return consoleAppender;
}
 
Example 4
Source File: LogbackLoggingConfigurer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void doFailSafeConfiguration() {
    // Not really fail-safe, just less likely to fail
    final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.reset();
    Logger rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME);
    rootLogger.setLevel(Level.INFO);

    ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
    rootLogger.addAppender(appender);
    appender.setContext(context);
    appender.setTarget("System.err");

    PatternLayout layout = new PatternLayout();
    appender.setLayout(layout);
    layout.setPattern("%msg%n%ex");
    layout.setContext(context);

    layout.start();
    appender.start();
}
 
Example 5
Source File: LogbackLoggingConfigurer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void doFailSafeConfiguration() {
    // Not really fail-safe, just less likely to fail
    final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.reset();
    Logger rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME);
    rootLogger.setLevel(Level.INFO);

    ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
    rootLogger.addAppender(appender);
    appender.setContext(context);
    appender.setTarget("System.err");

    PatternLayout layout = new PatternLayout();
    appender.setLayout(layout);
    layout.setPattern("%msg%n%ex");
    layout.setContext(context);

    layout.start();
    appender.start();
}
 
Example 6
Source File: StructuredLogging.java    From flo with Apache License 2.0 6 votes vote down vote up
public static void configureStructuredLogging(Level level) {
  final Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

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

  final StructuredLoggingEncoder encoder = new StructuredLoggingEncoder();
  encoder.start();

  final ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
  appender.setTarget("System.err");
  appender.setName("stderr");
  appender.setEncoder(encoder);
  appender.setContext(context);
  appender.start();

  rootLogger.addAppender(appender);
  rootLogger.setLevel(fromLocationAwareLoggerInteger(level.toInt()));

  SLF4JBridgeHandler.install();
}
 
Example 7
Source File: LogbackLogManager.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
private void configureConsole(LoggingConfig.ConsoleConfig consoleConfig, Logger rootLogger) {
    if (consoleConfig.isEnabled()) {
        ConsoleAppender<ILoggingEvent> logConsoleAppender = new ConsoleAppender<>();
        logConsoleAppender.setContext(context);
        switch (consoleConfig.getOutput()) {
            case STDOUT:
                logConsoleAppender.setTarget("System.out");
                break;
            case STDERR:
                logConsoleAppender.setTarget("System.err");
                break;
            default:
                logConsoleAppender.setTarget("System.out");
                break;
        }
        logConsoleAppender.setEncoder(buildEncoder(consoleConfig, DEFAULT_CONSOLE_PATTERN));
        logConsoleAppender.start();
        rootLogger.addAppender(logConsoleAppender);
    }
}
 
Example 8
Source File: LoggingConfigurator.java    From logging-java with Apache License 2.0 6 votes vote down vote up
/**
 * Configure logging with the LogstashEncoder library.
 * (https://github.com/logstash/logstash-logback-encoder)
 *
 * An appender is configured to send the log messages to stdout. It is expected something like
 * a Docker container will capture these logs for further processing.
 *
 */
public static void configureLogstashEncoderDefaults(final Level level) {
  final Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
  final LoggerContext context = rootLogger.getLoggerContext();

  context.reset();

  final CustomLogstashEncoder encoder = new CustomLogstashEncoder().setupStackdriver();
  encoder.setContext(context);
  encoder.start();

  final ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
  appender.setTarget("System.out");
  appender.setName("stdout");
  appender.setEncoder(encoder);
  appender.setContext(context);
  appender.start();

  rootLogger.addAppender(appender);
  rootLogger.setLevel(level.logbackLevel);

  UncaughtExceptionLogger.setDefaultUncaughtExceptionHandler();
}
 
Example 9
Source File: LoggingConfigurator.java    From logging-java with Apache License 2.0 6 votes vote down vote up
/**
 * Create a stderr appender.
 *
 * @param context The logger context to use.
 * @return An appender writing to stderr.
 */
private static Appender<ILoggingEvent> getStdErrAppender(final LoggerContext context,
                                                         final ReplaceNewLines replaceNewLines) {

  // Setup format
  final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
  encoder.setContext(context);
  encoder.setPattern(
      "%date{HH:mm:ss.SSS} %property{ident}[%property{pid}]: %-5level [%thread] %logger{0}: "
          + ReplaceNewLines.getMsgPattern(replaceNewLines) + "%n");
  encoder.setCharset(Charsets.UTF_8);
  encoder.start();

  // Setup stderr appender
  final ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
  appender.setTarget("System.err");
  appender.setName("stderr");
  appender.setEncoder(encoder);
  appender.setContext(context);
  appender.start();

  return appender;
}
 
Example 10
Source File: AgentOutputAppender.java    From gocd with Apache License 2.0 5 votes vote down vote up
void writeTo(Outstream target) {
    ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
    appender.setTarget(target.target.getName());
    appender.setEncoder(LogHelper.encoder("%date{ISO8601} [" + target.marker + "] - %msg%n"));
    appender.start();
    appenders.add(appender);
}
 
Example 11
Source File: Logger.java    From metrics_publish_java with MIT License 4 votes vote down vote up
/**
 * Initialize Logback
 */
private static void initLogback() {
    // reset logger context
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.reset();
    
    // shared console appender
    ConsoleAppender.setContext(context);
    ConsoleAppender.setTarget("System.out");
    
    PatternLayoutEncoder consoleEncoder = new PatternLayoutEncoder();
    consoleEncoder.setContext(context);
    consoleEncoder.setPattern(LogPattern);
    consoleEncoder.start();
    ConsoleAppender.setEncoder(consoleEncoder);
    ConsoleAppender.start();
    
    // rolling file
    String logFile = getFilePath() + File.separatorChar + getFileName();
    FileAppender.setContext(context);
    FileAppender.setFile(logFile);
    
    // log pattern
    PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder();
    fileEncoder.setContext(context);
    fileEncoder.setPattern(LogPattern);
    fileEncoder.start();
    FileAppender.setEncoder(fileEncoder);
    
    // rolling policy
    FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
    rollingPolicy.setContext(context);
    rollingPolicy.setParent(FileAppender);
    rollingPolicy.setFileNamePattern(logFile + "%i.zip");
    rollingPolicy.setMinIndex(1);
    rollingPolicy.setMaxIndex(1);
    rollingPolicy.start();
    
    // file max size - if fileLimit is 0, set max file size to maximum allowed
    long fileLimit = getFileLimitInKBytes() != 0 ? getFileLimitInKBytes() * 1024 : Long.MAX_VALUE;
    SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>(String.valueOf(fileLimit));
    triggeringPolicy.start();
    
    FileAppender.setRollingPolicy(rollingPolicy);
    FileAppender.setTriggeringPolicy(triggeringPolicy);
    FileAppender.start();
}