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
Configurable contextItems for Klogger #80
Comments
Hi @skotsj Thanks for checking out Klogging! Your suggestion sounds good but I am not sure I understand it. Do you mean have selected fields from other elements in the coroutine context automatically included in log messages? I think that is a great idea, and using lambdas sounds like a good mechanism for implementing it. Here‘s how it might work.
Did I get that right? |
That is correct. |
@skotsj The newest This code enables Klogging to call a lambda on a You configure it with the configuration DSL like this: val reactorContextItems: (ReactorContext) -> Map<String, Any?> = { context ->
// Items to be included in log events
}
loggingConfiguration {
// Other configuration using DSL
addContextItemExtractor(ReactorContext, reactorContextItems)
} If you configure Klogging using JSON, you can add to that configuration by specifying class LoggingSetup {
private val logger = logger<LoggingSetup>() // Loads JSON configuration
init {
val reactorContextItems: (ReactorContext) -> Map<String, Any?> = { context ->
// Items to be included in log events
}
loggingConfiguration(append = true) {
addContextItemExtractor(ReactorContext, reactorContextItems)
}
}
} |
Looks good, can't really test as I'm currently working in a proxied environment without snapshots. |
I will include the functionality into a release soon (possibly within a day). I realised that using the existing Klogging configuration is the wrong model. I will provide an object to manage this kind of configuration. I am also thinking of defining the lambda as an extension function on // `EventItems` is a type alias for `Map<String, Any?>`
val reactorContextItems: ReactorContext.() -> EventItems {
// Items to include in log events
} Do you prefer that to |
I have made the following changes, which are available in klogging-jvm:0.4.7 (also via slfj-klogging:0.2.6 and klogging-spring-boot-starter:0.2.3):
I will document these in more detail soon. |
Oh, I didn't notice/forgot, but the extractor needs to be a coroutine, accessing context var is suspended. Shouldn't be an issue to fix I guess seeing as contextITems is already a coroutine. |
Of course, I should have thought of that. My tests were a bit dumb. |
I have made two changes to the extraction function/lambda in
I changed This is a tricky generic type problem I have not solved yet. |
Tested 0.4.8. Works like a charm, much appreciate the quick implementation ;) |
That is good news. It made my day 😄 I would like to know how Klogging is being used. Are you able to share some code? Are you using Spring Boot or Ktor? Are you logging to stdout and collecting logs or sending direct to an aggregator? |
I have released |
Can't share any code unfortunately, I'm using spring boot with webflux using coroutines and logging json to stdout on k8s gathered by elk |
It would be great if extraction of contextItems could be configurable, ability to provide a lambda etc.
This way I could implement my own function to extract fields from for example ReactorContext (kotlinx.coroutines.reactor.ReactorContext) to avoid adding klogging context and mapping fields there on all log invocations.
I suppose I could implement my own Klogger for this, but base support seems nice to have.
The text was updated successfully, but these errors were encountered: