-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Provide a hook for configuring Logback programatically #25847
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
Comments
Status update:
|
It looks like a We do already have a package-private Adding hook points for logging is quite a challenge since they're normally needed before the Spring |
I'd like to see this issue solved to accommodate what I think is a similar use case: OpenTelemetry java has logback and log4j appenders which bridge log data from those logging systems in the OpenTelemetry ecosystem. These appenders need to be configured with an OpenTelemetry instance, an object which contains APIs for bridging log records into OpenTelemetry, and which is programmatically configured by the user. The general workflow is for users to initialize an OpenTelemetry instance very early in the application lifecycle, and then use it when installing various bits of instrumentation. In spring environments, its typical to create an OpenTelemetry bean to participate in dependency injection. The programmatic configuration of the OpenTelemetry instance is at odds with typical static log configuration. But both log4j and logback allow for programmatic configuration as well, where you can modify a log4j.xml or logback.xml configuration at runtime by adding / editing appenders. It would nice to offer OpenTelemetry users a helper function This workflow doesn't quite work in spring boot because programmatic modifications to the log4j / logback configuration are erased when spring boot is initialized (I believe this line is the culprit). Users could do the modification after spring initializes, but they'll miss out on valuable logs detailing spring initialization. It would be great if spring boot could provide a hook for performing programmatic configuration of log4j / logback early enough that any appenders added are able to receive application startup logs. |
Thanks for the use case, @jack-berg. Unfortunately, unless I have misunderstood, I don't think what you have described is possible.
These goals seem to be contradictory. If we're using an
I don't think this belongs in Spring Boot. From what I understand, there's nothing Spring Boot-specific about wanting to configure Logback or Log4j with an Open Telemetry appender. For example, with Logback, it would be done through the standard |
Sorry I think I presented my case in a confusing way. I don't think spring boot should in any way be responsible for installing / configuring the opentelemetry appender. Rather, I was just trying to explain why programmatic configuration is important for the OpenTelemetry appenders, and express support for spring boot adding some sort of generic callback or hook that opentelemetry code can leverage to perform programmatic configuration soon after spring boot log configuration, such that the opentelemetry appenders can receive initialization logs. |
This might overlap with Logback's existing |
I created samples to automatically configure Logback. I'm not sure if it's the right way to do it, but it works for me in the following two use cases:
|
We have a slightly different use-case. We'd be happy with some extension point being a simplified version of what already exists in I'm not sure if something like a logback appender factory could be exposed, or the full logger context. But something that would let us programmatically construct an appender ...
public Appender<ILoggingEvent> getAppender(Context/Environment in) {
var appender = new RollbarAppender();
appender.setAccessToken(in.getConfig("logback.rollbar.access-token"));
return appender;
} then configure that as we currently configure log levels and structured logging options. As it is right now, we can't easily configure structured logging and our custom appenders, as spring doesn't apply the logging defaults in the presence of a logback.xml file. Also, extending a Configurator does not let us access the spring Environment, like how the StructuredLogEncoder currently can Environment environment = (Environment) getContext().getObject(Environment.class.getName());
Assert.state(environment != null, "Unable to find Spring Environment in logger context"); and pre-existing loggers are cleared when spring runs it's defaults. One potentially brittle way to extend existing support I've found would be to sub-class the |
At the moment we are considering migrating few applications to spring-native. Everything works fine excepts our Logback configuration. We use a
logback.xml
, and as stated in spring-attic/spring-native#625 it will not work as intended, nor does it seem to be an easy problem to tackle.We were already considering moving to the programmatic Logback configuration, using a Configurator, which would probably be easier to use with spring-native and would provide better application startup for regular applications. However, we cannot migrate all of our
logback.xml
configuration since we cannot import default spring configuration nor usespringProperty/springProfile
elements.The text was updated successfully, but these errors were encountered: