Java Code Examples for ch.qos.logback.classic.Logger#getLoggerContext()

The following examples show how to use ch.qos.logback.classic.Logger#getLoggerContext() . 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: 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 2
Source File: LoggingConfigurator.java    From logging-java with Apache License 2.0 6 votes vote down vote up
/**
 * Mute all logging.
 */
public static void configureNoLogging() {
  final Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
  final LoggerContext context = rootLogger.getLoggerContext();

  // Clear context, removing all appenders
  context.reset();

  // Set logging level to OFF
  for (final Logger logger : context.getLoggerList()) {
    if (logger != rootLogger) {
      logger.setLevel(null);
    }
  }
  rootLogger.setLevel(OFF);
}
 
Example 3
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 4
Source File: LoggingConfigurator.java    From logging-java with Apache License 2.0 6 votes vote down vote up
/**
 * Add a sentry appender.
 * @param dsn the sentry dsn to use (as produced by the sentry webinterface).
 * @param logLevelThreshold the threshold for log events to be sent to sentry.
 * @return the configured sentry appender.
 */
public static SentryAppender addSentryAppender(final String dsn, Level logLevelThreshold) {
  final Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

  final LoggerContext context = rootLogger.getLoggerContext();

  SentryAppender appender = new SentryAppender();
  appender.setDsn(dsn);

  appender.setContext(context);
  ThresholdFilter levelFilter = new ThresholdFilter();
  levelFilter.setLevel(logLevelThreshold.logbackLevel.toString());
  levelFilter.start();
  appender.addFilter(levelFilter);

  appender.start();

  rootLogger.addAppender(appender);

  return appender;
}
 
Example 5
Source File: LogbackSlowQueryLogProvider.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Override
public SlowQueryLog get() {
    Logger logger = (Logger) LoggerFactory.getLogger("sor.slow-query");
    logger.setAdditive(false);
    LoggerContext context = logger.getLoggerContext();
    logger.detachAndStopAllAppenders();

    PatternLayout logPatternLayout = new PatternLayout();
    logPatternLayout.setPattern("%-5p [%d{ISO8601," + _config.getTimeZone().getID() + "}] %m\n");
    logPatternLayout.setContext(context);
    logPatternLayout.start();

    ConsoleAppenderFactory consoleAppenderFactory = _config.getConsoleAppenderFactory();
    if (consoleAppenderFactory != null) {
        // Console is usually used only in development.  Use a synchronous appender so console output doesn't get re-ordered.
        logger.addAppender(consoleAppenderFactory.build(context, EMODB_APPLICATION_NAME, logPatternLayout));
    }

    FileAppenderFactory fileAppenderFactory = _config.getFileAppenderFactory();
    if (fileAppenderFactory != null) {
        AsyncAppender fileAsyncAppender = new AsyncAppender();
        fileAsyncAppender.addAppender(fileAppenderFactory.build(context, EMODB_APPLICATION_NAME, logPatternLayout));
        fileAsyncAppender.start();
        logger.addAppender(fileAsyncAppender);
    }

    SyslogAppenderFactory syslogAppenderFactory = _config.getSyslogAppenderFactory();
    if (syslogAppenderFactory != null) {
        AsyncAppender sysAsyncAppender = new AsyncAppender();
        sysAsyncAppender.addAppender(syslogAppenderFactory.build(context, EMODB_APPLICATION_NAME, logPatternLayout));
        sysAsyncAppender.start();
        logger.addAppender(sysAsyncAppender);
    }

    return new LogbackSlowQueryLog(logger, _config.getTooManyDeltasThreshold(), _metricRegistry);
}
 
Example 6
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 7
Source File: LoggingConfigurator.java    From logging-java with Apache License 2.0 5 votes vote down vote up
/**
 * Configure logging using a logback configuration file.
 *
 * @param file         A logback configuration file.
 * @param defaultIdent Fallback logging identity, used if not specified in config file.
 */
public static void configure(final File file, final String defaultIdent) {
  final Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

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

  // Log uncaught exceptions
  UncaughtExceptionLogger.setDefaultUncaughtExceptionHandler();

  // Load logging configuration from file
  try {
    final JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    configurator.doConfigure(file);
  } catch (JoranException je) {
    // StatusPrinter will handle this
  }

  context.putProperty("pid", getMyPid());
  final String hostname = getSpotifyHostname();
  if (hostname != null) {
    context.putProperty("hostname", hostname);
  }

  final String ident = context.getProperty("ident");
  if (ident == null) {
    context.putProperty("ident", defaultIdent);
  }

  StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}
 
Example 8
Source File: LoggingConfigurator.java    From logging-java with Apache License 2.0 5 votes vote down vote up
private static LoggerContext setupLoggerContext(Logger rootLogger, String ident) {
  final LoggerContext context = rootLogger.getLoggerContext();
  context.reset();
  context.putProperty("ident", ident);
  context.putProperty("pid", getMyPid());
  context.putProperty("hostname", getSpotifyHostname());
  return context;
}
 
Example 9
Source File: LoggingConfiguratorTest.java    From logging-java with Apache License 2.0 5 votes vote down vote up
private void assertDefault(final String ident, final Level level) {
  final Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
  final ConsoleAppender<?> stderr = (ConsoleAppender<?>) rootLogger.getAppender("stderr");
  assertTrue(stderr.getEncoder() instanceof PatternLayoutEncoder);
  assertEquals(level, rootLogger.getLevel());
  final LoggerContext context = rootLogger.getLoggerContext();
  assertEquals(ident, context.getProperty("ident"));
}
 
Example 10
Source File: LogCollector.java    From jadx with Apache License 2.0 5 votes vote down vote up
public static void register() {
	Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
	LoggerContext loggerContext = rootLogger.getLoggerContext();

	PatternLayout layout = new PatternLayout();
	layout.setContext(loggerContext);
	layout.setPattern("%-5level: %msg%n");
	layout.start();

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

	rootLogger.addAppender(INSTANCE);
}
 
Example 11
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 12
Source File: FlickrUploader.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
public static void flushLogs() {
    try {
        Logger logbackLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
        LoggerContext lc = logbackLogger.getLoggerContext();
        Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
        Appender<ILoggingEvent> appender = rootLogger.getAppender("file");
        appender.stop();
        appender.start();
    } catch (Throwable e) {
        Log.e("Flickr Uploader", e.getMessage(), e);
    }
}
 
Example 13
Source File: QpidLoggerTurboFilter.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
private static LoggerContext getRootContext()
{
    final Logger rootLogger = (Logger) (LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME));
    return rootLogger.getLoggerContext();
}
 
Example 14
Source File: LoggingConfiguratorTest.java    From logging-java with Apache License 2.0 4 votes vote down vote up
private String getLoggingContextHostnameProperty() {
  final Logger accessPointLogger = (Logger) LoggerFactory.getLogger("logger");
  final LoggerContext loggerContext = accessPointLogger.getLoggerContext();
  return loggerContext.getProperty("hostname");
}
 
Example 15
Source File: FlickrUploader.java    From flickr-uploader with GNU General Public License v2.0 4 votes vote down vote up
private static void initLogs() {
    Logger logbackLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    LoggerContext lc = logbackLogger.getLoggerContext();

    Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
    rootLogger.detachAndStopAllAppenders();

    TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
    rollingPolicy.setMaxHistory(3);
    SizeAndTimeBasedFNATP<ILoggingEvent> sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP<ILoggingEvent>();
    sizeAndTimeBasedFNATP.setMaxFileSize("2MB");
    rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(sizeAndTimeBasedFNATP);
    rollingPolicy.setFileNamePattern(context.getFilesDir().getPath() + "/logs/old/flickruploader.%d{yyyy-MM-dd}.%i.log");
    rollingPolicy.setContext(lc);

    RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
    fileAppender.setContext(lc);
    fileAppender.setFile(getLogFilePath());
    fileAppender.setRollingPolicy(rollingPolicy);
    fileAppender.setTriggeringPolicy(rollingPolicy);
    rollingPolicy.setParent(fileAppender);

    PatternLayoutEncoder pl = new PatternLayoutEncoder();
    pl.setContext(lc);
    pl.setCharset(Charset.defaultCharset());
    pl.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %class{0}.%method:%L > %msg%n");
    pl.setImmediateFlush(false);
    pl.start();

    fileAppender.setEncoder(pl);
    fileAppender.setName("file");

    rollingPolicy.start();
    fileAppender.start();

    if (Config.isDebug()) {
        final PatternLayoutEncoder logcatTagPattern = new PatternLayoutEncoder();
        logcatTagPattern.setContext(lc);
        logcatTagPattern.setPattern("%class{0}");
        logcatTagPattern.start();

        final PatternLayoutEncoder logcatPattern = new PatternLayoutEncoder();
        logcatPattern.setContext(lc);
        logcatPattern.setPattern("[%thread] %method:%L > %msg%n");
        logcatPattern.start();

        final LogcatAppender logcatAppender = new LogcatAppender();
        logcatAppender.setContext(lc);
        logcatAppender.setTagEncoder(logcatTagPattern);
        logcatAppender.setEncoder(logcatPattern);
        logcatAppender.start();

        rootLogger.addAppender(logcatAppender);
    }

    rootLogger.addAppender(fileAppender);

}