-
Notifications
You must be signed in to change notification settings - Fork 76
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
feat: support customization for MDC serializer #293
base: main
Are you sure you want to change the base?
feat: support customization for MDC serializer #293
Conversation
💚 CLA has been signed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution @aliaksandr-lavishak-tde ! I think this feature request is reasonable, I just have some objections on how we structure the extension mechanism.
log4j2-ecs-layout/src/main/java/co/elastic/logging/log4j2/MdcSerializer.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot to publish my second comment
log4j2-ecs-layout/src/main/java/co/elastic/logging/log4j2/MdcSerializer.java
Outdated
Show resolved
Hide resolved
851e620
to
7ee13d8
Compare
7ee13d8
to
3ebc7d5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just some small changes
return (MdcSerializer) clazz.getDeclaredConstructor().newInstance(); | ||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | ||
| NoSuchMethodException | InvocationTargetException e) { | ||
return resolveDefault(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would silently hide the misconfiguration, leaving users scratching their head why thinks don't work.
I think it is better to throw an exception which will usually abort the application start to show the problem to the user:
return resolveDefault(); | |
throw new IllegalArgumentException("Could not create MdcSerializer " + mdcSerializerFullClassName, e); |
* Implementations must have a public no-argument constructor to allow dynamic instantiation. | ||
* </p> | ||
*/ | ||
public interface MdcSerializer { | ||
void serializeMdc(LogEvent event, StringBuilder builder); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the following javadoc to the method:
/**
* Add MDC data for the give log event to the provided output string builder.
* The output written to the string builder must be a valid JSON-object without the surrounding curly braces
* and with a trailing comma. If this MDC serializer does not append any content, no comma shall be added.
* For example, the serializer could output the following content: "foo":"bar","key":"value",
*
* @param event the log event to write the MDC content for
* @param builder the output JSON string builder
*/
No description provided.