Java Code Examples for ch.qos.logback.core.ConsoleAppender

The following examples show how to use ch.qos.logback.core.ConsoleAppender. These examples are extracted from open source projects. 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
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
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 Project: emissary   Source File: Emissary.java    License: Apache License 2.0 6 votes vote down vote up
static void setupLogbackForConsole() {
    // So it looks better when commands are run
    ch.qos.logback.classic.Logger root =
            (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
    root.detachAndStopAllAppenders();
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    lc.reset();

    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    ple.setPattern("%msg%n");
    ple.setContext(lc);
    ple.start();

    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setEncoder(ple);
    consoleAppender.setContext(lc);
    consoleAppender.start();

    root.addAppender(consoleAppender);
    root.setLevel(Level.INFO);
    root.setAdditive(false);
}
 
Example 4
Source Project: nuls-v2   Source File: LogAppender.java    License: MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static Appender<ILoggingEvent> createConsoleAppender(Level level){
    ConsoleAppender appender = new ConsoleAppender();
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    appender.setContext(context);
    //这里设置级别过滤器
    LogFilter levelController = new LogFilter();
    ThresholdFilter levelFilter = levelController.getThresholdFilter(level);
    levelFilter.start();
    appender.addFilter(levelFilter);

    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    //设置上下文,每个logger都关联到logger上下文,默认上下文名称为default。
    //但可以使用<contextName>设置成其他名字,用于区分不同应用程序的记录。一旦设置,不能修改。
    encoder.setContext(context);
    //设置格式
    encoder.setPattern("%d %p [%t] - %msg%n");
    encoder.start();
    //加入下面两个节点
    appender.setEncoder(encoder);
    appender.start();
    return appender;
}
 
Example 5
Source Project: butterfly   Source File: LogbackLogConfigurator.java    License: MIT License 6 votes vote down vote up
@Override
    public void setVerboseMode(boolean verboseMode) {
        this.verboseMode = verboseMode;

        if (verboseMode) {
            PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
            patternLayoutEncoder.setPattern("[%d{HH:mm:ss.SSS}] [%highlight(%level)] %msg%n");
            patternLayoutEncoder.setContext(loggerContext);
            patternLayoutEncoder.start();

            Appender<ILoggingEvent> consoleAppender = new ConsoleAppender();
            ((OutputStreamAppender) consoleAppender).setEncoder(patternLayoutEncoder);
            consoleAppender.setContext(loggerContext);
            consoleAppender.start();

            loggerContext.getLogger("com.paypal.butterfly.cli").detachAppender("CONSOLE");
            loggerContext.getLogger("ROOT").addAppender(consoleAppender);
//        } else {
            // TODO
        }
    }
 
Example 6
Source Project: MMDownloader   Source File: MaruLoggerFactory.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 개행 없이 메시지 자체만 출력하는 프린트용 Logger 를 가져온다.
 *
 * @return 출력용 Logger 객체
 */
public static Logger getPrintLogger() {
	if (printLogger != null) {
		return printLogger;
	}

	LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
	PatternLayoutEncoder encoder = getEncoder(loggerContext, "%msg");

	String loggerName = "printLogger";
	ConsoleAppender consoleAppender = getConsoleAppender(loggerContext, encoder);

	printLogger = loggerContext.getLogger(loggerName);
	printLogger.addAppender(consoleAppender);
	printLogger.setAdditive(false);

	return printLogger;
}
 
Example 7
Source Project: qpid-broker-j   Source File: BrokerConsoleLoggerImpl.java    License: 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 8
Source Project: qpid-broker-j   Source File: StartupAppender.java    License: Apache License 2.0 6 votes vote down vote up
public void logToConsole()
{
    Context context = getContext();
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setContext(context);
    PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
    patternLayoutEncoder.setContext(context);

    // added MDC variable 'qpid.log.prefix' for test purposes
    patternLayoutEncoder.setPattern("%X{qpid.log.prefix}%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
    patternLayoutEncoder.start();

    consoleAppender.addFilter(new Filter<ILoggingEvent>()
    {
        @Override
        public FilterReply decide(final ILoggingEvent event)
        {
            return event.getLevel().isGreaterOrEqual(_consoleAppenderAcceptLogLevel) ? FilterReply.ACCEPT : FilterReply.DENY;
        }
    });

    consoleAppender.setEncoder(patternLayoutEncoder);
    consoleAppender.start();
    replayAccumulatedEvents(consoleAppender);
    consoleAppender.stop();
}
 
Example 9
public static void configure(KonkerLoggerContext lc) {
    StatusManager sm = lc.getStatusManager();
    if(sm != null) {
        sm.add(new InfoStatus("Setting up default configuration.", lc));
    }

    ConsoleAppender ca = new ConsoleAppender();
    ca.setContext(lc);
    ca.setName("console");
    PatternLayoutEncoder pl = new PatternLayoutEncoder();
    pl.setContext(lc);
    pl.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
    pl.start();
    ca.setEncoder(pl);
    ca.start();
    KonkerLogger rootLogger = lc.getLogger("ROOT");
    rootLogger.addAppender(ca);
}
 
Example 10
Source Project: baleen   Source File: BaleenLoggerBuilderTest.java    License: Apache License 2.0 6 votes vote down vote up
/** Test abstract functions (using console implementation) and the functions of console */
@Test
public void testAbstractAndConsole() {
  BaleenConsoleLoggerBuilder builder =
      new BaleenConsoleLoggerBuilder(
          NAME, BaleenLogging.DEFAULT_PATTERN, new MinMaxFilter(Level.INFO, Level.WARN));

  LoggerContext context = new LoggerContext();
  Encoder<ILoggingEvent> encoder = new PatternLayoutEncoder();

  Appender<ILoggingEvent> appender = builder.build(context, encoder);

  assertEquals(NAME, builder.getName());

  assertTrue(appender instanceof ConsoleAppender);
  assertEquals(encoder, ((ConsoleAppender<ILoggingEvent>) appender).getEncoder());

  assertNotNull(appender);
  assertEquals(NAME, appender.getName());
  Filter<ILoggingEvent> filter = appender.getCopyOfAttachedFiltersList().get(0);
  assertTrue(filter instanceof MinMaxFilter);
  assertEquals(Level.INFO, ((MinMaxFilter) filter).getMin());
  assertEquals(Level.WARN, ((MinMaxFilter) filter).getMax());
}
 
Example 11
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 12
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 13
Source Project: terracotta-platform   Source File: LocalMainCommand.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
  Logger rootLogger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER_NAME);

  if (verbose) {
    ConsoleAppender<ILoggingEvent> appender = (ConsoleAppender<ILoggingEvent>) rootLogger.getAppender("STDOUT");
    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    ple.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %c{1}:%L - %msg%n");
    ple.setContext(appender.getContext());
    ple.start();

    appender.setEncoder(ple);
    rootLogger.setLevel(Level.TRACE);
    rootLogger.getLoggerContext().getLoggerList().forEach(logger -> logger.setLevel(Level.TRACE));
  }
}
 
Example 14
Source Project: bither-desktop-java   Source File: LogbackFactory.java    License: Apache License 2.0 6 votes vote down vote up
public static ConsoleAppender<ILoggingEvent> buildConsoleAppender(LoggingConfiguration.ConsoleConfiguration console,
                                                                  LoggerContext context,
                                                                  String logFormat) {
    final LogFormatter formatter = new LogFormatter(context, console.getTimeZone());
    if (logFormat != null) {
        formatter.setPattern(logFormat);
    }
    formatter.start();

    final ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
    appender.setContext(context);
    appender.setLayout(formatter);
    addThresholdFilter(appender, console.getThreshold());
    appender.start();

    return appender;
}
 
Example 15
Source Project: flo   Source File: StructuredLogging.java    License: 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 16
Source Project: bither-desktop-java   Source File: LogbackFactory.java    License: Apache License 2.0 6 votes vote down vote up
public static ConsoleAppender<ILoggingEvent> buildConsoleAppender(LoggingConfiguration.ConsoleConfiguration console,
                                                                  LoggerContext context,
                                                                  String logFormat) {
    final LogFormatter formatter = new LogFormatter(context, console.getTimeZone());
    if (logFormat != null) {
        formatter.setPattern(logFormat);
    }
    formatter.start();

    final ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
    appender.setContext(context);
    appender.setLayout(formatter);
    addThresholdFilter(appender, console.getThreshold());
    appender.start();

    return appender;
}
 
Example 17
Source Project: seed   Source File: LogbackLogManager.java    License: 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 18
Source Project: logging-java   Source File: LoggingConfigurator.java    License: 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 19
Source Project: logging-java   Source File: LoggingConfigurator.java    License: 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 20
Source Project: alchemy   Source File: LoggingConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
private void addJsonConsoleAppender(LoggerContext context) {
    log.info("Initializing Console logging");
    
    // More documentation is available at: https://github.com/logstash/logstash-logback-encoder
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setContext(context);
    consoleAppender.setEncoder(compositeJsonEncoder(context));
    consoleAppender.setName(CONSOLE_APPENDER_NAME);
    consoleAppender.start();

    context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME).detachAppender(CONSOLE_APPENDER_NAME);
    context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME).addAppender(consoleAppender);
}
 
Example 21
Source Project: milkman   Source File: LogbackConfiguration.java    License: MIT License 5 votes vote down vote up
private void setupConsoleAppender(LoggerContext loggerContext) {
	ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>();
	ca.setContext(loggerContext);
	ca.setName("STDOUT");
	
	var filter = new ThresholdFilter();
	filter.setLevel("INFO");
	filter.start();
	ca.addFilter(filter);
	
	LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<ILoggingEvent>();
	encoder.setContext(loggerContext);

	
	// same as
	// PatternLayout layout = new PatternLayout();
	// layout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
	TTLLLayout layout = new TTLLLayout();

	layout.setContext(loggerContext);
	layout.start();
	encoder.setLayout(layout);

	
	
	ca.setEncoder(encoder);
	ca.start();

	Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
	rootLogger.addAppender(ca);
}
 
Example 22
Source Project: spring-cloud-formula   Source File: SpacedLogbackSystem.java    License: Apache License 2.0 5 votes vote down vote up
private Appender<ILoggingEvent> consoleAppender() {
    ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    String logPattern = this.patterns.getProperty("logging.pattern.console", FILE_LOG_PATTERN);
    encoder.setPattern(OptionHelper.substVars(logPattern, context));
    encoder.setCharset(DEFAULT_CHARSET);
    appender.setEncoder(encoder);
    start(encoder);
    appender("console", appender);
    return appender;
}
 
Example 23
private void addJsonConsoleAppender(LoggerContext context) {
    log.info("Initializing Console logging");
    
    // More documentation is available at: https://github.com/logstash/logstash-logback-encoder
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setContext(context);
    consoleAppender.setEncoder(compositeJsonEncoder(context));
    consoleAppender.setName(CONSOLE_APPENDER_NAME);
    consoleAppender.start();

    context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME).detachAppender(CONSOLE_APPENDER_NAME);
    context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME).addAppender(consoleAppender);
}
 
Example 24
private void addJsonConsoleAppender(LoggerContext context) {
    log.info("Initializing Console logging");
    
    // More documentation is available at: https://github.com/logstash/logstash-logback-encoder
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setContext(context);
    consoleAppender.setEncoder(compositeJsonEncoder(context));
    consoleAppender.setName(CONSOLE_APPENDER_NAME);
    consoleAppender.start();

    context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME).detachAppender(CONSOLE_APPENDER_NAME);
    context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME).addAppender(consoleAppender);
}
 
Example 25
private void addJsonConsoleAppender(LoggerContext context) {
    log.info("Initializing Console logging");
    
    // More documentation is available at: https://github.com/logstash/logstash-logback-encoder
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setContext(context);
    consoleAppender.setEncoder(compositeJsonEncoder(context));
    consoleAppender.setName(CONSOLE_APPENDER_NAME);
    consoleAppender.start();

    context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME).detachAppender(CONSOLE_APPENDER_NAME);
    context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME).addAppender(consoleAppender);
}
 
Example 26
Source Project: konduit-serving   Source File: LogUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sets the appenders for command line.
 */
public static void setAppendersForCommandLine() {
    Logger rootLogger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER);

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

    rootLogger.detachAndStopAllAppenders();

    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();

    PatternLayoutEncoder consolePatternLayoutEncoder = new PatternLayoutEncoder();
    consolePatternLayoutEncoder.setPattern("%msg%n");
    consolePatternLayoutEncoder.setContext(context);
    consolePatternLayoutEncoder.start();

    consoleAppender.setEncoder(consolePatternLayoutEncoder);
    consoleAppender.start();

    rootLogger.addAppender(consoleAppender);

    ((Logger) LoggerFactory.getLogger("uk.org.lidalia")).setLevel(Level.ERROR);
    ((Logger) LoggerFactory.getLogger("org.nd4j")).setLevel(Level.ERROR);
    ((Logger) LoggerFactory.getLogger("org")).setLevel(Level.ERROR);
    ((Logger) LoggerFactory.getLogger("io")).setLevel(Level.ERROR);

    ((Logger) LoggerFactory.getLogger("ai")).setLevel(Level.INFO);
}
 
Example 27
@Override
public void reInitialize(final SpaceId spaceId, LoggerContext loggerContext,
                         final Properties properties, URL confFile) {
    if (isAlreadyReInitialized(loggerContext)) {
        return;
    }
    stopAndReset(loggerContext);
    loggerContext.getTurboFilterList().remove(DefaultLogbackFilterGenerator.FILTER);
    markAsReInitialized(loggerContext);
    initProperties(loggerContext, properties);
    if (isConsoleAppenderOpen(spaceId.getSpaceName(), properties)) {
        final ConsoleAppender appender = consoleAppender(loggerContext, properties);
        loggerContext.addTurboFilter(new TurboFilter() {
            @Override
            public FilterReply decide(Marker marker, Logger logger, Level level, String format,
                                      Object[] params, Throwable t) {
                if (!logger.isAttached(appender)) {
                    logger.detachAndStopAllAppenders();
                    logger.setLevel(getConsoleLevel(spaceId.getSpaceName(), properties));
                    logger.addAppender(appender);
                }
                return FilterReply.NEUTRAL;
            }
        });
    } else {
        try {
            new ContextInitializer(loggerContext).configureByResource(confFile);
        } catch (JoranException e) {
            throw new IllegalStateException("Logback loggerSpaceFactory re-build error", e);
        }
    }

}
 
Example 28
private ConsoleAppender consoleAppender(LoggerContext loggerContext, Properties properties) {
    SystemPropertiesGetter propertiesGetter = new SystemPropertiesGetter(properties);
    ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    String logPattern = propertiesGetter.getProperty(
        SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN,
        SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN_DEFAULT);
    encoder.setPattern(OptionHelper.substVars(logPattern, loggerContext));
    encoder.setContext(loggerContext);
    encoder.start();
    appender.setEncoder(encoder);
    appender.setName("CONSOLE");
    appender.start();
    return appender;
}
 
Example 29
Source Project: emissary   Source File: EnvCommand.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void run(JCommander jc) {
    String endpoint = getScheme() + "://" + getHost() + ":" + getPort() + "/api/env";

    if (getBashable()) {
        ch.qos.logback.classic.Logger root =
                (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
        root.detachAndStopAllAppenders();
        setup();
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        PatternLayoutEncoder ple = new PatternLayoutEncoder();

        ple.setPattern("%msg%n");
        ple.setContext(lc);
        ple.start();

        ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
        consoleAppender.setEncoder(ple);
        consoleAppender.setContext(lc);
        consoleAppender.start();

        root.addAppender(consoleAppender);
        root.setLevel(Level.INFO);
        root.setAdditive(false);

        // still gotta hide org.eclipse.jetty.util.log INFO
        ch.qos.logback.classic.Logger jettyUtilLogger = lc.getLogger("org.eclipse.jetty.util.log");
        jettyUtilLogger.setLevel(Level.WARN);

        // also add .sh to the endpoint
        endpoint = endpoint + ".sh";
        LOG.info("# generated from env command at {}", endpoint);
    } else {
        setup(); // go ahead an log it
    }
    EmissaryClient client = new EmissaryClient();
    LOG.info(client.send(new HttpGet(endpoint)).getContentString());
}
 
Example 30
private Appender<ILoggingEvent> consoleAppender(LogbackConfigurator config) {
	ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
	PatternLayoutEncoder encoder = new PatternLayoutEncoder();
	String logPattern = this.logging.getProperty("pattern.console", CONSOLE_LOG_PATTERN);
	encoder.setPattern(OptionHelper.substVars(logPattern, config.getContext()));
	encoder.setCharset(UTF8);
	config.start(encoder);
	appender.setEncoder(encoder);
	config.appender("CONSOLE", appender);
	return appender;
}