-
Notifications
You must be signed in to change notification settings - Fork 88
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
Logging: print message
as JSON, instead of wrapping into a string
#1508
Comments
I think this is a breaking change ==> v2 |
This feels quite important. Is it really breaking and not a bug? |
@jdoherty I'm taking this one. I'm working on the logging v2 anyway. Not sure that make sense you spend time on a pure log4j solution as I'm using slf4j now. |
will be part of #1435, using the |
Discussion with Log4J: apache/logging-log4j2#2013 |
@jeromevdl we should probably update this with resolution once you're done 👼 |
Based on the discussions with log4j guys, on this issue, and after thinking a bit more about this, we won't log messages as JSON. Instead, we'll support advanced object as additional arguments (not using MDC which is limited to Strings), and log them as JSON (when that makes sense). Example: Product product = new Product("id123456", "Name of the product", 234.5);
LOGGER.info("Some message", StructuredArguments.entry("key", product));
// can also use static import to make it lighter
LOGGER.info("Some message", entry("product", product)); will produce: {
"message": "Some message",
"product": {
"id": "id123456",
"name": "Name of the product",
"price": 234.5
}
} Note there was no LOGGER.info("My super product={}", entry("product", product)); will produce: {
"message": "My super product=[...]", // will use the object.toString()
"product": {
"id": "id123456",
"name": "Name of the product",
"price": 234.5
}
} Also working on other
For the event and response, we'll use this instead of putting the event in the message. |
@jeromevdl what do we need to do this - is it simply a matter of documenting this practice? I gather the user's interface here will be through SLF4j APIs. |
No I need to implement some stuff. The |
My thoughts on this implementation are: What will I do if I only want to log an object, without a string message? |
You cannot log without a message. Or you can just log an empty message, but messages are always there (it's the basic of the loggers). you should do |
we could actually check if the message is empty and not adding it to the log... but a log without a message is a bit weird |
I am OK with this (would like to hear other views), even though I don't love the syntax.
Agree that it would be weird to print a log message, without any message |
Is your feature request related to a problem? Please describe.
The Powertools Logging functionality automatically wraps the messages into string, regardless of whether they are strings or objects.
Looking through the logs with CloudWatch logs is harder with the
message
being printed as a single string, and printing as JSON will also unlock other options (e.g. using CloudWatch insights to automatically discover fields).Describe the solution you'd like
Describe alternatives you've considered
Additional context
The text was updated successfully, but these errors were encountered: