ppkarwasz commented on issue #3704: URL: https://github.com/apache/logging-log4j2/issues/3704#issuecomment-3084857545
Hi @sidhantmourya, > I'm now looking at how to implement PatternLayout support for headers similar to how SubjectSerializer works. > https://github.com/apache/logging-log4j2/blob/8ec5703670fc24d8883db39df8be244c34c8e0bd/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/SmtpAppender.java#L283-L286 > The issue is that while there's only one subject per email, there can be multiple headers each with their own potential patterns. You could approach it like this: ```java String[] headerNames = headers.stream() .map(Property::getName) .toArray(String[]::new); Serializer[] headerSerializers = headers.stream() .map(header -> PatternLayout.newSerializerBuilder() .setConfiguration(getConfiguration()) .setPattern(header.getValue()) .build()) .toArray(Serializer[]::new); ``` This would give you a pair of arrays — one for names and one for serializers — that you can zip together at runtime when generating headers. Of course you can zip them in any other way you like, I wouldn't just use `Map<String, Serializer>`, since it prevents users from configuring repeated headers. To expose a new `headers` property in the builder, you just need to annotate it like this: ```java @PluginElement("Headers") private List<Property> headers = new ArrayList<>(); ``` Note: the argument to `@PluginElement` is ignored, but it's required for compatibility reasons. You might also want to add a setter method and an `addHeader(Property header)` method to simplify usage in tests and manual configurations. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
