Search in sources :

Example 66 with Logger

use of ch.qos.logback.classic.Logger in project cdap by caskdata.

the class LogAppenderInitializer method setLogLevels.

public void setLogLevels(Map<String, String> logPairs) {
    LoggerContext loggerContext = getLoggerContext();
    if (loggerContext != null) {
        for (Map.Entry<String, String> entry : logPairs.entrySet()) {
            String loggerName = entry.getKey();
            String logLevel = entry.getValue();
            Logger logger = loggerContext.getLogger(loggerName);
            LOG.info("Log level of {} changed from {} to {}", loggerName, logger.getLevel(), logLevel);
            logger.setLevel(Level.toLevel(logLevel));
        }
    }
}
Also used : Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext) Map(java.util.Map)

Example 67 with Logger

use of ch.qos.logback.classic.Logger in project cdap by caskdata.

the class LogAppenderInitializer method initialize.

@VisibleForTesting
public synchronized void initialize(String loggerName) {
    LoggerContext loggerContext = getLoggerContext();
    if (loggerContext != null) {
        Logger logger = loggerContext.getLogger(loggerName);
        // Check if the logger already contains the logAppender
        if (Iterators.contains(logger.iteratorForAppenders(), logAppender)) {
            LOG.warn("Log appender {} is already initialized.", logAppender.getName());
            return;
        }
        logAppender.setContext(loggerContext);
        LOG.info("Initializing log appender {}", logAppender.getName());
        // Display any errors during initialization of log appender to console
        StatusManager statusManager = loggerContext.getStatusManager();
        OnConsoleStatusListener onConsoleListener = new OnConsoleStatusListener();
        statusManager.add(onConsoleListener);
        logAppender.start();
        logger.addAppender(logAppender);
    }
}
Also used : StatusManager(ch.qos.logback.core.status.StatusManager) OnConsoleStatusListener(ch.qos.logback.core.status.OnConsoleStatusListener) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 68 with Logger

use of ch.qos.logback.classic.Logger in project cdap by caskdata.

the class Loggers method getEffectiveLogger.

/**
   * Returns the effective {@link Logger} for the given logger name. An
   * effective {@link Logger} is the most specific {@link Logger} in the logger
   * hierarchy based on the given logger name, which is already defined in the given {@link LoggerContext}.
   */
public static Logger getEffectiveLogger(LoggerContext context, String loggerName) {
    // If there is such logger, then just return the effective level
    Logger logger = context.exists(loggerName);
    if (logger != null) {
        return logger;
    }
    // Otherwise, search through the logger hierarchy. We don't call loggerContext.getLogger to avoid
    // caching of the logger inside the loggerContext, which there is no guarantee on the memory usage
    // and there is no way to clean it up.
    logger = context.getLogger(Logger.ROOT_LOGGER_NAME);
    int idx = LoggerNameUtil.getFirstSeparatorIndexOf(loggerName);
    while (idx >= 0) {
        Logger parentLogger = context.exists(loggerName.substring(0, idx));
        if (parentLogger != null) {
            logger = parentLogger;
        }
        idx = LoggerNameUtil.getSeparatorIndexOf(loggerName, idx + 1);
    }
    // Returns the most specific logger for the given logger name
    return logger;
}
Also used : Logger(ch.qos.logback.classic.Logger)

Example 69 with Logger

use of ch.qos.logback.classic.Logger in project dropwizard by dropwizard.

the class BootstrapLogging method bootstrap.

public static void bootstrap(Level level) {
    LoggingUtil.hijackJDKLogging();
    BOOTSTRAPPING_LOCK.lock();
    try {
        if (bootstrapped) {
            return;
        }
        final Logger root = LoggingUtil.getLoggerContext().getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
        root.detachAndStopAllAppenders();
        final DropwizardLayout formatter = new DropwizardLayout(root.getLoggerContext(), TimeZone.getDefault());
        formatter.start();
        final ThresholdFilter filter = new ThresholdFilter();
        filter.setLevel(level.toString());
        filter.start();
        final ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
        appender.addFilter(filter);
        appender.setContext(root.getLoggerContext());
        final LayoutWrappingEncoder<ILoggingEvent> layoutEncoder = new LayoutWrappingEncoder<>();
        layoutEncoder.setLayout(formatter);
        appender.setEncoder(layoutEncoder);
        appender.start();
        root.addAppender(appender);
        bootstrapped = true;
    } finally {
        BOOTSTRAPPING_LOCK.unlock();
    }
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) ThresholdFilter(ch.qos.logback.classic.filter.ThresholdFilter) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LayoutWrappingEncoder(ch.qos.logback.core.encoder.LayoutWrappingEncoder)

Example 70 with Logger

use of ch.qos.logback.classic.Logger in project dropwizard by dropwizard.

the class DefaultLoggingFactory method configureLoggers.

private Logger configureLoggers(String name) {
    final Logger root = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    loggerContext.reset();
    final LevelChangePropagator propagator = new LevelChangePropagator();
    propagator.setContext(loggerContext);
    propagator.setResetJUL(true);
    loggerContext.addListener(propagator);
    root.setLevel(level);
    final LevelFilterFactory<ILoggingEvent> levelFilterFactory = new ThresholdLevelFilterFactory();
    final AsyncAppenderFactory<ILoggingEvent> asyncAppenderFactory = new AsyncLoggingEventAppenderFactory();
    final LayoutFactory<ILoggingEvent> layoutFactory = new DropwizardLayoutFactory();
    for (Map.Entry<String, JsonNode> entry : loggers.entrySet()) {
        final Logger logger = loggerContext.getLogger(entry.getKey());
        final JsonNode jsonNode = entry.getValue();
        if (jsonNode.isTextual()) {
            // Just a level as a string
            logger.setLevel(Level.valueOf(jsonNode.asText()));
        } else if (jsonNode.isObject()) {
            // A level and an appender
            final LoggerConfiguration configuration;
            try {
                configuration = Jackson.newObjectMapper().treeToValue(jsonNode, LoggerConfiguration.class);
            } catch (JsonProcessingException e) {
                throw new IllegalArgumentException("Wrong format of logger '" + entry.getKey() + "'", e);
            }
            logger.setLevel(configuration.getLevel());
            logger.setAdditive(configuration.isAdditive());
            for (AppenderFactory<ILoggingEvent> appender : configuration.getAppenders()) {
                logger.addAppender(appender.build(loggerContext, name, layoutFactory, levelFilterFactory, asyncAppenderFactory));
            }
        } else {
            throw new IllegalArgumentException("Unsupported format of logger '" + entry.getKey() + "'");
        }
    }
    return root;
}
Also used : AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) AsyncAppenderFactory(io.dropwizard.logging.async.AsyncAppenderFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) DropwizardLayoutFactory(io.dropwizard.logging.layout.DropwizardLayoutFactory) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) ThresholdLevelFilterFactory(io.dropwizard.logging.filter.ThresholdLevelFilterFactory) AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) LevelChangePropagator(ch.qos.logback.classic.jul.LevelChangePropagator) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

Logger (ch.qos.logback.classic.Logger)144 Test (org.junit.Test)49 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)45 LoggerContext (ch.qos.logback.classic.LoggerContext)40 FileAppender (ch.qos.logback.core.FileAppender)17 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)16 Appender (ch.qos.logback.core.Appender)16 File (java.io.File)13 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)12 ArrayList (java.util.ArrayList)9 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)8 AsyncLoggingEventAppenderFactory (io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory)8 DropwizardLayoutFactory (io.dropwizard.logging.layout.DropwizardLayoutFactory)7 Before (org.junit.Before)7 PipeConfiguration (co.cask.cdap.filetailer.config.PipeConfiguration)6 Query (org.apache.lucene.search.Query)6 BaseIndexingTest (org.kie.workbench.common.services.refactoring.backend.server.BaseIndexingTest)6 SingleTermQueryBuilder (org.kie.workbench.common.services.refactoring.backend.server.query.builder.SingleTermQueryBuilder)6 Level (ch.qos.logback.classic.Level)5 Map (java.util.Map)5