Log4J2 PatternConverter for adding a custom field to the log record

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Log4J2 PatternConverter for adding a custom field to the log record

Asma Zinneera Jabir
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;
    }
}
Reply | Threaded
Open this post in threaded view
|

Re: Log4J2 PatternConverter for adding a custom field to the log record

Matt Sicker
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 <[hidden email]>
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 <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Log4J2 PatternConverter for adding a custom field to the log record

Asma Zinneera Jabir
Does that mean I don't have to use options here? I can use "pid" in the
constructor which works fine. But then there is no use of the options
String array. Is this a good practice or is there a better way to implement
appending a custom field to the log event?

On Sep 10, 2017 10:05 PM, "Matt Sicker" <[hidden email]> wrote:

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 <[hidden email]>
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 <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Log4J2 PatternConverter for adding a custom field to the log record

Matt Sicker
Well, pid might not be a great example considering there's already a
converter for it. Any global variable like that can generally be
implemented via a converter like that, or potentially via various lookups <
https://logging.apache.org/log4j/2.x/manual/lookups.html>, especially when
they're available via conventional means such as environment variables,
system properties, arguments to main(), etc. For an arg-less converter, I
believe you still need to accept a String[] as your newInstance factory
method, though you can ignore its arguments.

I wouldn't suggest extending LogEvent unless you're trying to implement
serialization logic for binary logging or similar (see for example the
Jackson-annotated versions of LogEvent for serializing to JSON/XML/YAML).

On 10 September 2017 at 12:08, Asma Zinneera Jabir <[hidden email]>
wrote:

> Does that mean I don't have to use options here? I can use "pid" in the
> constructor which works fine. But then there is no use of the options
> String array. Is this a good practice or is there a better way to implement
> appending a custom field to the log event?
>
> On Sep 10, 2017 10:05 PM, "Matt Sicker" <[hidden email]> wrote:
>
> 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 <[hidden email]>
> 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 <[hidden email]>
>



--
Matt Sicker <[hidden email]>