sentry-java icon indicating copy to clipboard operation
sentry-java copied to clipboard

[Spring Boot Log4j] Unable to output sdk debug logging to log4j2 file.

Open Fwang36 opened this issue 1 year ago • 5 comments

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 -

log4j2 copy.xml.txt

Screenshot of the log file up top, and the sdk debug logging still in the console -

Screenshot 2024-02-12 at 12 05 35 PM

Expected Result

Sentry sdk logs also outputting to the log file.

Actual Result

Sentry sdk logs do not output to the log file.

Fwang36 avatar Feb 13 '24 15:02 Fwang36

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

romtsn avatar Feb 13 '24 16:02 romtsn

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?

edbras avatar Feb 13 '24 22:02 edbras

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

Fwang36 avatar Feb 16 '24 19:02 Fwang36

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());
    };
}

adinauer avatar Feb 19 '24 12:02 adinauer

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.

edbras avatar Feb 19 '24 12:02 edbras

We're closing this now as it seems everyone has got it to work. Feel free to reopen if you need more help.

adinauer avatar Mar 11 '24 13:03 adinauer