From 033de90cd631dabad6b659da353fbddf3844b5d6 Mon Sep 17 00:00:00 2001 From: Volodymyr Pavlenko Date: Wed, 2 Oct 2024 12:52:25 +0200 Subject: [PATCH] Add support for outputting slf4j markers in the default configuration --- .../spotify/logging/LoggingConfigurator.java | 28 +++++++++++++++++-- .../logging/LoggingConfiguratorTest.java | 25 +++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/spotify/logging/LoggingConfigurator.java b/src/main/java/com/spotify/logging/LoggingConfigurator.java index 0aadffa..144c582 100644 --- a/src/main/java/com/spotify/logging/LoggingConfigurator.java +++ b/src/main/java/com/spotify/logging/LoggingConfigurator.java @@ -189,6 +189,24 @@ public static void configureDefaults(final String ident, final Level level) { */ public static void configureDefaults( final String ident, final Level level, final ReplaceNewLines replaceNewLines) { + configureDefaults(ident, level, replaceNewLines, false); + } + + /** + * 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. + * + * @param ident The logging identity. + * @param level logging level to use. + * @param replaceNewLines configures new lines replacement in the messages + * @param appendMarkers append markers to the log message + */ + public static void configureDefaults( + final String ident, + final Level level, + final ReplaceNewLines replaceNewLines, + final boolean appendMarkers) { // Call configureSyslogDefaults if the SPOTIFY_SYSLOG_HOST or SPOTIFY_SYSLOG_PORT env var is // set. If this causes a problem, we could introduce a configureConsoleDefaults method which // users could call instead to avoid this behavior. @@ -205,7 +223,7 @@ public static void configureDefaults( final LoggerContext context = setupLoggerContext(rootLogger, ident); // Setup stderr output - rootLogger.addAppender(getStdErrAppender(context, replaceNewLines)); + rootLogger.addAppender(getStdErrAppender(context, replaceNewLines, appendMarkers)); // Setup logging level rootLogger.setLevel(level.logbackLevel); @@ -380,16 +398,20 @@ public static SentryAppender addSentryAppender( * Create a stderr appender. * * @param context The logger context to use. + * @param appendMarkers append markers to the log message * @return An appender writing to stderr. */ private static Appender getStdErrAppender( - final LoggerContext context, final ReplaceNewLines replaceNewLines) { + final LoggerContext context, + final ReplaceNewLines replaceNewLines, + final boolean appendMarkers) { // Setup format final PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(context); encoder.setPattern( "%date{HH:mm:ss.SSS} %property{ident}[%property{pid}]: %-5level [%thread] %logger{0}: " + + (appendMarkers ? "[%marker] " : "") + ReplaceNewLines.getMsgPattern(replaceNewLines) + "%n"); encoder.setCharset(StandardCharsets.UTF_8); @@ -487,7 +509,7 @@ static void configure(final JewelCliLoggingOptions opts) { rootLogger.addAppender( getSyslogAppender(context, syslogHost, syslogPort, ReplaceNewLines.OFF)); } else { - rootLogger.addAppender(getStdErrAppender(context, ReplaceNewLines.OFF)); + rootLogger.addAppender(getStdErrAppender(context, ReplaceNewLines.OFF, false)); } // Setup default logging level diff --git a/src/test/java/com/spotify/logging/LoggingConfiguratorTest.java b/src/test/java/com/spotify/logging/LoggingConfiguratorTest.java index 795c5b9..6fce6e1 100644 --- a/src/test/java/com/spotify/logging/LoggingConfiguratorTest.java +++ b/src/test/java/com/spotify/logging/LoggingConfiguratorTest.java @@ -49,6 +49,7 @@ import ch.qos.logback.classic.net.SyslogAppender; import ch.qos.logback.core.ConsoleAppender; import ch.qos.logback.core.status.Status; +import com.spotify.logging.LoggingConfigurator.ReplaceNewLines; import com.spotify.logging.logback.CustomLogstashEncoder; import io.sentry.logback.SentryAppender; import net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider; @@ -178,6 +179,30 @@ public void shouldConfigureDefaultWithIdentAndLevelWhenSyslogEnvVarIsNotSet() { assertDefault("some-ident", Level.DEBUG); } + @Test + public void shouldConfigureDefaultWithMarkerSupport() { + LoggingConfigurator.configureDefaults( + "some-ident", LoggingConfigurator.Level.DEBUG, ReplaceNewLines.OFF, true); + final Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); + final ConsoleAppender stderr = (ConsoleAppender) rootLogger.getAppender("stderr"); + assertTrue(stderr.getEncoder() instanceof PatternLayoutEncoder); + assertEquals( + "%date{HH:mm:ss.SSS} %property{ident}[%property{pid}]: %-5level [%thread] %logger{0}: [%marker] %msg%n", + ((PatternLayoutEncoder) stderr.getEncoder()).getPattern()); + } + + @Test + public void shouldConfigureDefaultWithoutMarkerSupport() { + LoggingConfigurator.configureDefaults( + "some-ident", LoggingConfigurator.Level.DEBUG, ReplaceNewLines.OFF, false); + final Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); + final ConsoleAppender stderr = (ConsoleAppender) rootLogger.getAppender("stderr"); + assertTrue(stderr.getEncoder() instanceof PatternLayoutEncoder); + assertEquals( + "%date{HH:mm:ss.SSS} %property{ident}[%property{pid}]: %-5level [%thread] %logger{0}: %msg%n", + ((PatternLayoutEncoder) stderr.getEncoder()).getPattern()); + } + @Test public void shouldConfigureLogstashEncoderWhenEnvVarIsSetToTrue() { environmentVariables.set("USE_JSON_LOGGING", "true");