sentry-java
sentry-java copied to clipboard
[Spring Boot Log4j] Unable to output sdk debug logging to log4j2 file.
Integration
sentry-spring-boot-starter-jakarta, log4j2
Java Version
8
Version
7.3
Steps to Reproduce
I'm creating this on behalf of a user that is having trouble getting the Sentry debug logging to their log4j2 file. I tried myself but am also unable to do it.
I have a Spring boot Log4j app. Both sdks are installed and able to report exceptions. The logging is set up to output logging to a file. The Spring boot logs output properly to the file, but the Sentry Debug Mode logs still only output to console.
This is my current log4j2 xml file -
Screenshot of the log file up top, and the sdk debug logging still in the console -
Expected Result
Sentry sdk logs also outputting to the log file.
Actual Result
Sentry sdk logs do not output to the log file.
That's because we always set the logger to the System.out when debug
is configured, see here: https://github.com/getsentry/sentry-java/blob/a537f8a45828b751272940034c4a64ee5f8fd278/sentry/src/main/java/io/sentry/Sentry.java#L386
They would have to provide a custom ILogger
implementation which writes into the same file they use for log4j
I did set the options, but noticed it's complaining... As I posted in your ticket system:
@Configuration
@Slf4j
public class SentryConfiguration {
@EventListener(ApplicationStartedEvent.class)
void configureSentry() {
Sentry.init(options -> {
log.info("Configuring Sentry");
options.setEnableExternalConfiguration(true);
options.setLogger(new SentryLogger());
});
}
}
```}
I don't think it's using anymore the EVn and application.yml settings, as it's complaining that the DNS is not set and I should set an empty string. But that already happens through the application.yml, but even dot I indicate it should load external properties, it is already complaining during this call.
Can you post me an example that works please?
Hey @edbras ,
I got this to work in my test app. The DSN does have to be provided to the Sentry.init() for this to work. The other options can still be provided externally, but only through environment variables. Setting the other options in either the application.properties or application.yml both caused it to stop working. Also, if I provide the DSN as an environment option as well, it also stopped working. This is my setup that works for me to output the debug logging to a file.
Sentry.init(options -> {
options.setEnableExternalConfiguration(true);
options.setDsn("https://[email protected]/4504181117091840");
options.setLogger(new Log4jSentryLogger());
});
export SENTRY_DEBUG=true
export SENTRY_TRACES_SAMPLE_RATE=1.0
Is there a reason, you're calling Sentry.init
manually? You should be able to set the logger, e.g. by exposing a Sentry.OptionsConfiguration<SentryOptions>
bean that'll let you customize options programatically without having to fall back to manual init.
@Bean
public Sentry.OptionsConfiguration<SentryOptions> optionsConfiguration() {
return options -> {
options.setLogger(new Log4jSentryLogger());
};
}
Thanks for your answer:
Is there a reason, you're calling Sentry.init manually? You should be able to set the logger, e.g. by exposing a
That was also my question in the Sentry issue tracker, as I did not want to call it as I was afraid that my env/properties were ignored when calling that, which was also the case I think. That is how we landed here on Github to get more help.
The code you are suggesting, looks like the correct way to do it, I will give a try later, thanks.
We're closing this now as it seems everyone has got it to work. Feel free to reopen if you need more help.