Package com.spotify.logging
Class LoggingConfigurator
- java.lang.Object
-
- com.spotify.logging.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
LoggingConfigurator.Level
static class
LoggingConfigurator.ReplaceNewLines
Defines if new lines should be replaced in the logged message to avoid potential CRLF injection.
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_IDENT
static String
SPOTIFY_HOSTNAME
static String
SPOTIFY_SYSLOG_HOST
static String
SPOTIFY_SYSLOG_PORT
-
Constructor Summary
Constructors Constructor Description LoggingConfigurator()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static com.getsentry.raven.logback.SentryAppender
addSentryAppender(String dsn)
Add a sentry appender for error log event.static com.getsentry.raven.logback.SentryAppender
addSentryAppender(String dsn, LoggingConfigurator.Level logLevelThreshold)
Add a sentry appender.static void
configure(File file)
Configure logging using a logback configuration file.static void
configure(File file, String defaultIdent)
Configure logging using a logback configuration file.static void
configureDefaults()
Configure logging with default behaviour and log to stderr.static void
configureDefaults(String ident)
Configure logging with default behaviour and log to stderr using INFO logging level.static void
configureDefaults(String ident, LoggingConfigurator.Level level)
Configure logging with default behaviour and log to stderr.static void
configureDefaults(String ident, LoggingConfigurator.Level level, LoggingConfigurator.ReplaceNewLines replaceNewLines)
Configure logging with default behaviour and log to stderr.static void
configureLogstashEncoderDefaults(LoggingConfigurator.Level level)
Configure logging with the LogstashEncoder library.static void
configureNoLogging()
Mute all logging.static void
configureService(String serviceName)
Configure logging with default behaviour for a service.static void
configureSyslogDefaults(String ident)
Configure logging with default behavior and log to syslog using INFO logging level.static void
configureSyslogDefaults(String ident, LoggingConfigurator.Level level, LoggingConfigurator.ReplaceNewLines replaceNewLines)
Configure logging with default behavior and log to syslog.static void
configureSyslogDefaults(String ident, LoggingConfigurator.Level level, String host, int port, LoggingConfigurator.ReplaceNewLines replaceNewLines)
Configure logging with default behavior and log to syslog.static void
configureSyslogDefaults(String ident, LoggingConfigurator.Level level, String host, int port, String loggerName, LoggingConfigurator.ReplaceNewLines replaceNewLines)
Configure logging with default behavior and log to syslog.
-
-
-
Field Detail
-
DEFAULT_IDENT
public static final String DEFAULT_IDENT
- See Also:
- Constant Field Values
-
SPOTIFY_HOSTNAME
public static final String SPOTIFY_HOSTNAME
- See Also:
- Constant Field Values
-
SPOTIFY_SYSLOG_HOST
public static final String SPOTIFY_SYSLOG_HOST
- See Also:
- Constant Field Values
-
SPOTIFY_SYSLOG_PORT
public static final String SPOTIFY_SYSLOG_PORT
- See Also:
- Constant Field Values
-
-
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 theDEFAULT_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 addedreplaceNewLines
- 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.
-
-