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

The following examples show how to use ch.qos.logback.core.ConsoleAppender#setEncoder() . 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: KonkerLoggerBasicConfigurator.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
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 2
Source File: StartupAppender.java    From qpid-broker-j with 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 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: LogAppender.java    From nuls-v2 with 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 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 6
Source File: Emissary.java    From emissary with 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 7
Source File: LoggingUtils.java    From jhipster with Apache License 2.0 5 votes vote down vote up
/**
 * <p>addJsonConsoleAppender.</p>
 *
 * @param context a {@link ch.qos.logback.classic.LoggerContext} object.
 * @param customFields a {@link java.lang.String} object.
 */
public static void addJsonConsoleAppender(LoggerContext context, String customFields) {
    log.info("Initializing Console loggingProperties");

    // More documentation is available at: https://github.com/logstash/logstash-logback-encoder
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setContext(context);
    consoleAppender.setEncoder(compositeJsonEncoder(context, customFields));
    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 8
Source File: LoggingConfiguration.java    From alchemy with 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 9
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 10
Source File: CombinatorialMaterializerTest.java    From tasmo with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void logger() {

    Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    LoggerContext loggerContext = rootLogger.getLoggerContext();
    loggerContext.reset();

    if (verbose) {

        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setContext(loggerContext);
        encoder.setPattern("[%thread]: %message%n");
        encoder.start();

        ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
        appender.setContext(loggerContext);
        appender.setEncoder(encoder);
        appender.start();

        rootLogger.addAppender(appender);

        ((Logger) LoggerFactory.getLogger("com.jivesoftware.os.tasmo")).setLevel(Level.TRACE);
        ((Logger) LoggerFactory.getLogger("com.jivesoftware.os.tasmo.lib.concur.ConcurrencyAndExistanceCommitChange")).setLevel(Level.TRACE);
        ((Logger) LoggerFactory.getLogger("com.jivesoftware.os.tasmo.reference.lib.ReferenceStore")).setLevel(Level.TRACE);
        ((Logger) LoggerFactory.getLogger("com.jivesoftware.os.tasmo.view.reader.service.writer.WriteToViewValueStore")).setLevel(Level.TRACE);
    } else {

        rootLogger.setLevel(Level.OFF);
    }
}
 
Example 11
Source File: EnhancedLogbackConfiguration.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 12
Source File: EnvCommand.java    From emissary with 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 13
Source File: CombinatorialMaterializerTest.java    From tasmo with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void logger() {

    Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    LoggerContext loggerContext = rootLogger.getLoggerContext();
    loggerContext.reset();

    if (verbose) {

        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setContext(loggerContext);
        encoder.setPattern("[%thread]: %message%n");
        encoder.start();

        ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
        appender.setContext(loggerContext);
        appender.setEncoder(encoder);
        appender.start();

        rootLogger.addAppender(appender);

        ((Logger) LoggerFactory.getLogger("com.jivesoftware.os.tasmo")).setLevel(Level.TRACE);
        ((Logger) LoggerFactory.getLogger("com.jivesoftware.os.tasmo.lib.concur.ConcurrencyAndExistanceCommitChange")).setLevel(Level.TRACE);
        ((Logger) LoggerFactory.getLogger("com.jivesoftware.os.tasmo.reference.lib.ReferenceStore")).setLevel(Level.TRACE);
        ((Logger) LoggerFactory.getLogger("com.jivesoftware.os.tasmo.view.reader.service.writer.WriteToViewValueStore")).setLevel(Level.TRACE);
    } else {

        rootLogger.setLevel(Level.OFF);
    }
}
 
Example 14
Source File: LogUtils.java    From konduit-serving with 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 15
Source File: LoggingConfiguration.java    From java-microservices-examples with 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 16
Source File: LoggingConfiguration.java    From java-microservices-examples with 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 17
Source File: LoggingConfiguration.java    From java-microservices-examples with 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 18
Source File: LoggerUtil.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
private static ConsoleAppender<ILoggingEvent> createConsoleAppender(final LoggerContext loggerContext) {
    final PatternLayoutEncoder ple = new PatternLayoutEncoder();
    ple.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{10} - %msg%n");
    ple.setContext(loggerContext);
    ple.start();

    final ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setContext(loggerContext);
    consoleAppender.setEncoder(ple);
    consoleAppender.setName("STDOUT");
    consoleAppender.start();
    return consoleAppender;
}
 
Example 19
Source File: LogbackConfiguration.java    From milkman with 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 20
Source File: AionLoggerFactory.java    From aion with MIT License 4 votes vote down vote up
private static List<Appender<ILoggingEvent>> constructAppenders(
        boolean shouldLogToFile, String _logDirectory) {
    List<Appender<ILoggingEvent>> appenders = new ArrayList<>();

    String logDirectory = DEFAULT_LOG_DIR;
    if (_logDirectory != null && !_logDirectory.trim().isEmpty()) logDirectory = _logDirectory;

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

    ConsoleAppender<ILoggingEvent> consoleSync = new ConsoleAppender<>();
    consoleSync.setContext(context);
    consoleSync.setName("consoleSyncAppender"); // for logger debugging
    consoleSync.setEncoder(encoder);
    consoleSync.start();

    AsyncAppender consoleAsync = new AsyncAppender();
    consoleAsync.setContext(context);
    consoleAsync.setName("consoleAsyncAppender"); // for logger debugging
    consoleAsync.addAppender(consoleSync);

    consoleAsync.setIncludeCallerData(ASYNC_LOGGER_INCLUDE_CALLER_DATA);
    consoleAsync.setNeverBlock(ASYNC_LOGGER_NEVER_BLOCK);
    consoleAsync.setMaxFlushTime(ASYNC_LOGGER_MAX_FLUSH_TIME_MS);
    consoleAsync.setDiscardingThreshold(ASYNC_LOGGER_DISCARDING_THRESHOLD);
    consoleAsync.setQueueSize(ASYNC_LOGGER_QUEUE_SIZE);
    consoleAsync.start();

    appenders.add(consoleAsync);
    if (!shouldLogToFile) return appenders;

    RollingFileAppender<ILoggingEvent> fileSync = new RollingFileAppender<>();

    SizeBasedTriggeringPolicy<ILoggingEvent> tp = new SizeBasedTriggeringPolicy<>();
    tp.setContext(context);
    tp.start();

    SizeAndTimeBasedRollingPolicy rp = new SizeAndTimeBasedRollingPolicy();
    rp.setContext(context);
    // roll-over each day
    // notice that we don't use the OS-agnostic File.separator here since logback is converts
    // the FileNamePattern
    // to a unix-style path using ch.qos.logback.core.rolling.helper.FileFilterUtil.slashify
    FileNamePattern fnp =
            new FileNamePattern(
                    logDirectory + "/%d{yyyy/MM, aux}/aion.%d{yyyy-MM-dd}.%i.log", context);
    rp.setFileNamePattern(fnp.getPattern());
    // max rollover file size = 100MB
    rp.setMaxFileSize(FileSize.valueOf("100mb"));
    rp.setParent(fileSync);
    rp.start();

    fileSync.setName("fileSyncAppender"); // for logger debugging
    fileSync.setContext(context);
    fileSync.setTriggeringPolicy(tp);
    fileSync.setRollingPolicy(rp);
    fileSync.setFile(logDirectory + File.separator + DEFAULT_LOG_FILE_CURRENT);
    fileSync.setEncoder(encoder);
    fileSync.setAppend(true);
    fileSync.start();

    AsyncAppender fileAsync = new AsyncAppender();
    fileAsync.setContext(context);
    fileAsync.setName("fileAsyncAppender"); // for logger debugging
    fileAsync.addAppender(fileSync);

    fileAsync.setIncludeCallerData(ASYNC_LOGGER_INCLUDE_CALLER_DATA);
    fileAsync.setNeverBlock(ASYNC_LOGGER_NEVER_BLOCK);
    fileAsync.setMaxFlushTime(ASYNC_LOGGER_MAX_FLUSH_TIME_MS);
    fileAsync.setDiscardingThreshold(ASYNC_LOGGER_DISCARDING_THRESHOLD);
    fileAsync.setQueueSize(ASYNC_LOGGER_QUEUE_SIZE);
    fileAsync.start();

    appenders.add(fileAsync);
    return appenders;
}