All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.turbospaces.logging.Logback Maven / Gradle / Ivy

There is a newer version: 2.0.33
Show newest version
package com.turbospaces.logging;

import java.io.InputStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Supplier;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.service.UriBasedServiceInfo;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.joran.spi.ConfigurationWatchList;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
import io.micrometer.core.instrument.MeterRegistry;
import io.sentry.SentryClient;

public interface Logback {
    //
    // ~ meta
    //
    String SPACE = "space";
    String HOST = "host";
    String SLOT = "slot";
    String SERVICE = "service";
    String RELEASE = "release";
    String CFG = "cfg";

    // ~ do not send logs to appenders
    String LOGGING_DRY_RUN = "logging-dryRun";

    // ~ do not report to sentry in case
    String ALERTS_DRY_RUN = "alerts-dryRun";

    // ~ metrics registry
    String METRICS_REGISTRY = "metrics-registry";

    //
    String SENTRY_CLIENT = "sentry-client";
    String UPS_SENTRY = "sentry";
    String SENTRY_LOGGING_FILTER = "sentry-logging-filter";
    String UPS_ELASTIC_SEARCH = "elastic-search";

    //
    // ~ template fields
    //
    String TIMESTAMP = "@timestamp";
    String SEQUENCE = "@sequence";

    static void configureFrom(
            LoggerContext loggerContext,
            AlertLoggingFilter alertLoggingFilter,
            InputStream io,
            Map options,
            SentryClient sentry,
            MeterRegistry meterRegistry,
            Map cfg,
            Supplier loggingDryRun,
            Supplier alertsDryRun,
            UriBasedServiceInfo sentryUps,
            UriBasedServiceInfo elasticUps
    ) throws JoranException {
        loggerContext.reset();

        for (Entry entry : options.entrySet()) {
            loggerContext.putProperty(entry.getKey(), entry.getValue());
        }

        loggerContext.putObject(Logback.SENTRY_CLIENT, sentry);
        loggerContext.putObject(Logback.UPS_SENTRY, sentryUps);
        loggerContext.putObject(Logback.UPS_ELASTIC_SEARCH, elasticUps);
        loggerContext.putObject(Logback.SENTRY_LOGGING_FILTER, alertLoggingFilter);
        loggerContext.putObject(Logback.LOGGING_DRY_RUN, loggingDryRun);
        loggerContext.putObject(Logback.ALERTS_DRY_RUN, alertsDryRun);
        loggerContext.putObject(Logback.ALERTS_DRY_RUN, alertsDryRun);
        loggerContext.putObject(Logback.CFG, cfg);
        loggerContext.putObject(Logback.METRICS_REGISTRY, meterRegistry);
        ConfigurationWatchListUtil.registerConfigurationWatchList(loggerContext, new ConfigurationWatchList());

        configure(loggerContext, io);
    }
    static void configure(LoggerContext loggerContext, InputStream io) throws JoranException {
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(loggerContext);

        configurator.addInfo(String.format("starting configuration of logback ctx(%s) using props", loggerContext.getName()));
        for (Entry entry : loggerContext.getCopyOfPropertyMap().entrySet()) {
            configurator.addInfo(entry.getKey() + "=" + entry.getValue());
        }

        configurator.doConfigure(io);
        configurator.addInfo(String.format("configured logback ctx(%s) ...", loggerContext.getName()));
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    static void resetToLevel(ch.qos.logback.classic.Level level) {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        lc.reset();

        PatternLayoutEncoder enc = new PatternLayoutEncoder();
        enc.setContext(lc);
        enc.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} %msg%n");
        enc.start();

        ConsoleAppender console = new ConsoleAppender<>();
        console.setContext(lc);
        console.setName("console");
        console.setEncoder(enc);
        console.start();

        ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
        root.setLevel(level);
        root.addAppender(console);
    }
    static void resetToInfo() {
        resetToLevel(ch.qos.logback.classic.Level.INFO);
    }
    static void resetToDebug() {
        resetToLevel(ch.qos.logback.classic.Level.DEBUG);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy