Class LoggingConfigurator


  • public class LoggingConfigurator
    extends Object
    Base configurator of logback for spotify services/tools. LoggingConfigurator.configureDefaults() should generally be called as soon as possible on start-up. The configured logging backend is logback. If the SPOTIFY_SYSLOG_HOST or SPOTIFY_SYSLOG_PORT environment variable is defined, configureDefaults() will use the syslog appender, otherwise it will use the console appender.

    One aspect of the logging is that we setup a general uncaught exception handler to log uncaught exceptions at info level, if syslog is chosen as logging backend.

    See Also:
    For some integration with JewelCLI
    • Constructor Detail

      • LoggingConfigurator

        public LoggingConfigurator()
    • Method Detail

      • configureNoLogging

        public static void configureNoLogging()
        Mute all logging.
      • configureService

        public static void configureService​(String serviceName)
        Configure logging with default behaviour for a service.

        Uses INFO logging level. If the USE_JSON_LOGGING environment variable is set to true, the LogstashEncoder will be used. Otherwise, if the SPOTIFY_SYSLOG_HOST or SPOTIFY_SYSLOG_PORT environment variable is defined, the syslog appender will be used. Otherwise console appender will be used.

        Parameters:
        serviceName - name of the service
      • configureDefaults

        public static void configureDefaults()
        Configure logging with default behaviour and log to stderr. Uses the DEFAULT_IDENT logging identity. Uses INFO logging level. If the SPOTIFY_SYSLOG_HOST or SPOTIFY_SYSLOG_PORT environment variable is defined, the syslog appender will be used, otherwise console appender will be.
      • configureDefaults

        public static void configureDefaults​(String ident)
        Configure logging with default behaviour and log to stderr using INFO logging level. If the SPOTIFY_SYSLOG_HOST or SPOTIFY_SYSLOG_PORT environment variable is defined, the syslog appender will be used, otherwise console appender will be.
        Parameters:
        ident - The logging identity.
      • configureDefaults

        public static void configureDefaults​(String ident,
                                             LoggingConfigurator.Level level)
        Configure logging with default behaviour and log to stderr. If the SPOTIFY_SYSLOG_HOST or SPOTIFY_SYSLOG_PORT environment variable is defined, the syslog appender will be used, otherwise console appender will be.
        Parameters:
        ident - The logging identity.
        level - logging level to use.
      • configureDefaults

        public static void configureDefaults​(String ident,
                                             LoggingConfigurator.Level level,
                                             LoggingConfigurator.ReplaceNewLines replaceNewLines)
        Configure logging with default behaviour and log to stderr. If the SPOTIFY_SYSLOG_HOST or SPOTIFY_SYSLOG_PORT environment variable is defined, the syslog appender will be used, otherwise console appender will be.
        Parameters:
        ident - The logging identity.
        level - logging level to use.
        replaceNewLines - configures new lines replacement in the messages
      • configureLogstashEncoderDefaults

        public static void configureLogstashEncoderDefaults​(LoggingConfigurator.Level level)
        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.

      • configureSyslogDefaults

        public static void configureSyslogDefaults​(String ident)
        Configure logging with default behavior and log to syslog using INFO logging level.
        Parameters:
        ident - Syslog ident to use.
      • configureSyslogDefaults

        public static void configureSyslogDefaults​(String ident,
                                                   LoggingConfigurator.Level level,
                                                   LoggingConfigurator.ReplaceNewLines replaceNewLines)
        Configure logging with default behavior and log to syslog.
        Parameters:
        ident - Syslog ident to use.
        level - logging level to use.
        replaceNewLines - configures new lines replacement in the messages
      • configureSyslogDefaults

        public static void configureSyslogDefaults​(String ident,
                                                   LoggingConfigurator.Level level,
                                                   @Nullable
                                                   String host,
                                                   int port,
                                                   LoggingConfigurator.ReplaceNewLines replaceNewLines)
        Configure logging with default behavior and log to syslog.
        Parameters:
        ident - Syslog ident to use.
        level - logging level to use.
        host - Hostname or IP address of syslog host.
        port - Port to connect to syslog on.
        replaceNewLines - configures new lines replacement in the messages
      • configureSyslogDefaults

        public static void configureSyslogDefaults​(String ident,
                                                   LoggingConfigurator.Level level,
                                                   @Nullable
                                                   String host,
                                                   int port,
                                                   String loggerName,
                                                   LoggingConfigurator.ReplaceNewLines replaceNewLines)
        Configure logging with default behavior and log to syslog.
        Parameters:
        ident - Syslog ident to use.
        level - logging level to use.
        host - Hostname or IP address of syslog host.
        port - Port to connect to syslog on.
        loggerName - Name of the logger to which the syslog appender will be added
        replaceNewLines - configures new lines replacement in the messages
      • addSentryAppender

        public static com.getsentry.raven.logback.SentryAppender addSentryAppender​(String dsn)
        Add a sentry appender for error log event.
        Parameters:
        dsn - the sentry dsn to use (as produced by the sentry webinterface).
        Returns:
        the configured sentry appender.
      • addSentryAppender

        public static com.getsentry.raven.logback.SentryAppender addSentryAppender​(String dsn,
                                                                                   LoggingConfigurator.Level logLevelThreshold)
        Add a sentry appender.
        Parameters:
        dsn - the sentry dsn to use (as produced by the sentry webinterface).
        logLevelThreshold - the threshold for log events to be sent to sentry.
        Returns:
        the configured sentry appender.
      • configure

        public static void configure​(File file)
        Configure logging using a logback configuration file.
        Parameters:
        file - A logback configuration file.
      • configure

        public static void configure​(File file,
                                     String defaultIdent)
        Configure logging using a logback configuration file.
        Parameters:
        file - A logback configuration file.
        defaultIdent - Fallback logging identity, used if not specified in config file.