Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for outputting slf4j markers in the default configuration #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/main/java/com/spotify/logging/LoggingConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -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<ILoggingEvent> 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);
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/spotify/logging/LoggingConfiguratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down