logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Sicker <boa...@gmail.com>
Subject Re: Log4J2 PatternConverter for adding a custom field to the log record
Date Sun, 10 Sep 2017 16:35:09 GMT
The curly braces in this case would be when you use %p{foo}{bar} or
%pId{foo}{bar}. The String[] would be {"foo", "bar"}. The @ConverterKeys
specifies the name(s) of the %converterKey part. So if you specified just
%p or %pId in your pattern, you'd get an empty String[].

On 10 September 2017 at 01:44, Asma Zinneera Jabir <azinneera@gmail.com>
wrote:

> I want to append a custom field to the log record and used the
> LogEventPatternConverter for this. Referred the documentation
> <https://logging.apache.org/log4j/2.x/manual/extending.
> html#PatternConverters>
> and
> wrote the below code.
> Now the options happen to be a zero length array.
> The documentation says "String array are the values that are specified
> within the curly braces that can follow the converter key". Isn't this the
> {"p","pId"} in my code?
>
> @Plugin(name = "PIdConverter", category = "Converter")
> @ConverterKeys({"p","pId"})
>
> public class PIdConverter extends LogEventPatternConverter {
>     public PIdConverter(String[] options) {
>         super(options[0], options[0]);
>     }
>
>     public static PIdConverter newInstance(String[] options) {
>         return new PIdConverter(options);
>     }
>
>     @Override
>     public void format(LogEvent event, StringBuilder toAppendTo) {
>         toAppendTo.append(getpID());
>     }
>
>     public String getpID() {
>         String pId = "123";
>         if (pId == null) {
>             pId = "[]";
>         }
>         return pId;
>     }
> }
>



-- 
Matt Sicker <boards@gmail.com>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message