Last active
September 15, 2016 14:47
-
-
Save kryger/aa9e25999f17683f3bd1cec1256ddbbe to your computer and use it in GitHub Desktop.
Programmatic SentryAppender + profile-dependent LogstashEncoder in logback-spring.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<configuration> | |
<!-- local profile outputs logs in plain text for readability --> | |
<springProfile name="local"> | |
<include resource="org/springframework/boot/logging/logback/console-appender.xml" /> | |
</springProfile> | |
<!-- non-local profiles output logs in JSON --> | |
<springProfile name="!local"> | |
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | |
<encoder class="net.logstash.logback.encoder.LogstashEncoder"> | |
<includeCallerInfo>true</includeCallerInfo> | |
<!-- other LogstashEncoder config options--> | |
</encoder> | |
</appender> | |
</springProfile> | |
<root> | |
<appender-ref ref="CONSOLE" /> | |
<!-- In my app a Sentry appender gets added programmatically --> | |
</root> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class LogbackConfig { | |
@Value("${dsn.etc}") | |
private String dsn; | |
@Bean(initMethod = "start", destroyMethod = "stop") | |
public SentryAppender sentryAppender(){ | |
LoggerContext ctx = (LoggerContext) LoggerFactory.getILoggerFactory(); | |
SentryAppender sentryAppender = new SentryAppender(); | |
sentryAppender.setDsn(dsn); | |
// ...other SentryAppender options... | |
sentryAppender.setContext(ctx); | |
Logger root = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); | |
root.addAppender(sentryAppender); | |
return sentryAppender; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment