-
-
Notifications
You must be signed in to change notification settings - Fork 116
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
Can't get extra fields in json output without relying on deprecated methods #457
Comments
Thank you for reporting an issue. See the wiki for documentation and slack for questions. |
The method that is eventually called is addKeyValue: https://www.slf4j.org/api/org/slf4j/spi/DefaultLoggingEventBuilder.html#addKeyValue(java.lang.String,java.lang.Object) I think the pattern config should include the key name, not sure how it was printed before when it's not in the pattern, maybe it was added implicitly. |
Unfortunately, the log pattern is configured "company wide" here, and as it works fine with the underlying loggers (as evidenced by it working with the old method 🤔 ), its going to be a bit difficult to convince anyone it needs to be changed. :-( |
Please try to check if this is really solve the problem (hence the root cause), before we decide how to tackle it. |
Attempt 1
to the pattern, hoping the latter part of it would reference the key. This resulted in an error message:
Which is fair enough, thats just not how it works. Attempt 2
my key/values are indeed output, but as just one single json field, not the same behaviour as earlier. With other fields removed for brevity, it looks like this:
...which is not what is needed. I can't really see any other way of doing it using the pattern? I'll try to reproduce it using logback directly without the KotlinLogging layer, see if it works as I expect that way. |
Calling the underlying logger directly (relevant lines below) with the original log format results in the old behavior.
Which leads me to conclude this is the expected behaviour, that structured arguments are included even if not mentioned explicitly in the json output. Observation |
Some debugging later, it seems that with the old method, and when using the underlying slf logger, the "payload" is added to the So what I'm looking for is a non-deprecated way of passing arguments to the |
I think the cause is an issue in how slf4j works now with fluent logging and structured arguments. I can try a workaround of adding to the argument array. |
Considering the slf4j framework works the same way as before (and the methods there aren't deprecated), I'm not sure how they could help? |
Slf4j introduced a new fluent api which we now use: https://www.slf4j.org/manual.html#fluent Though the old api there is not deprecated, it seems the new api do not behave like the old one. |
Thank you! 🤩 I get a bit lost when looking at the inner workings of loggers, but my initial instinct is that the arguments could maybe be added as another parameter to the atError-etc methods? Meaning adding another map arguments to KLoggingEventBuilder, which could then be used to call LoggingEventBuilder.addArgument somewhere in the LocationAwareKLogger - possibly in at or logWithPayload? I'll try to check if using the addArgument in the LoggingEventBuilder (which, if I'm understanding your link correctly is part of the fluent api?) reproduces the desired behaviour. |
Ok. I now recall that we also have direct logback usage: #452 Either we can see if it works there, or a fix there might be simpler as it bypasses slf4j. |
It does.
outputs
So my suggestion then is to add a field arguments of type Set to KLoggingEventBuilder that is then used to call the addArgument method. That would allow us to log like this:
|
ok, I think that makes sense. It might conflict with #452 but I guess no one expects to use both positional arguments and kotlin style message inference. So I guess we can decide to use only one of those lists. Would you like to create a PR for that? |
I had a look at #452 earlier but I didn't quite understand it, I'm afraid. :-( I will try making a PR when I get the time, might be a day or two! |
Fixed in 7.0.3 |
Thanks for the PR! |
@oshai I would like to help document the changes I made, and some of the related stuff, that seem to be missing from the Wiki. What is the best way of contributing there? I tried searching, but I can't find any way to make a PR towards it? Should I just open a new issue and include proposed changes to it, that you can include manually, or is there a better way? |
Yes, sounds good, just include is as markdown so I can easily merge. |
Hi, and thank you for maintaining an excellent library!
We have unfortunately run into a little problem:
We noticed that the support for writing log statements on the form
log.error("Cake", StructuredArguments.keyValue("caketype", "chocolate"))
(with net.logstash.logback.argument.StructuredArguments) has been deprecated. We use that style to include extra fields for json logging.The replace-with in the deprecation creates a wholly different result. After some searching, we noticed the "atError" etc function that take a payload, and hoped they could be used to replace the old behaviour.
But while that does include the caketype/chocolate pair in the output for ordinary logging, it disappears when we use Json logging.
For a fairly minimal example, see the below files and the comments in main. The behaviour of atError seems to be opposite of the behavior of the old method, in that the old one doesn't include the extra field in "ordinary" logging, while the new one does - and vice versa when it comes to json logging.
Have I misunderstood what the payload argument of atInfo, atWarn etc functions are supposed to be used for? If so, how do we reproduce the old behaviour for including extra fields in json output without leaning on the deprecated method? (Or is this a bug?)
Thanks in advance for any hints or ideas, we're a bit stuck here. :-)
Main.kt
logback.xml
build.gradle.kts
The text was updated successfully, but these errors were encountered: