Class JsonEncoder

  • All Implemented Interfaces:
    ch.qos.logback.core.encoder.Encoder<ch.qos.logback.classic.spi.ILoggingEvent>, ch.qos.logback.core.spi.ContextAware, ch.qos.logback.core.spi.LifeCycle

    public class JsonEncoder
    extends ch.qos.logback.core.encoder.EncoderBase<ch.qos.logback.classic.spi.ILoggingEvent>
    An Encoder implementation that encodes an ILoggingEvent as a JSON object.

    Under the hood, it's using Jackson to serialize the logging event into JSON. The encoder can be confiugred in the logback.xml:

     <appender name="STDOUT-JSON" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="com.sap.hcp.cf.logback.encoder.JsonEncoder"/>
     </appender>
     
    The encoder can be customized by several xml elements. See the Javadoc on the setter methods of this class.
    • Constructor Detail

      • JsonEncoder

        public JsonEncoder()
    • Method Detail

      • addCustomFieldMdcKeyName

        public void addCustomFieldMdcKeyName​(String name)

        Adds a field to the "#cf" object in the generated output. If the log event contains this field, it will be put into this nested object. It will not appear on top level in the generated JSON output, unless it is also added as retained field. See addRetainFieldMdcKeyName(String).

        This method is called by Joran for the xml tag <customFieldMdcKeyName> in the logback.xml configuration file.

        Parameters:
        name - the field name (key) to add as custom field
      • addRetainFieldMdcKeyName

        public void addRetainFieldMdcKeyName​(String name)

        Retains a copy of a custom field added by addCustomFieldMdcKeyName(String) at top level in the generated JSON output. This is useful when sending logs to different backend systems, that expect custom fields at different structures.

        This method is called by Joran for the xml tag <retainFieldMdcKeyName> in the logback.xml configuration file.

        Parameters:
        name - the field name (key) to add as custom field
      • setCharset

        public void setCharset​(String name)

        Use the given charset for message creation. Defaults to utf8. Note, that Jackson is using utf8 by default independently from this configuration. You need to change the JSON build with setJsonBuilder(String) to change the Jackson encoding.

        This method is called by Joran for the xml tag <charset> in the logback.xml configuration file.

        Parameters:
        name - the name of the charset to use
      • setMaxStacktraceSize

        public void setMaxStacktraceSize​(int maxStacktraceSize)

        Limit stacktraces to this number of characters. This is to prevent message sizes above the platform limit. The default value is 55*1024. It should result in messages below 64k in size. The size should be adjusted to fit the limit and the used fields of the messages. Stacktraces, that exceed the size will be shortened in the middle. Top and bottom most lines will be retained.

        This method is called by Joran for the xml tag <maxStacktraceSize> in the logback.xml configuration file.

        Parameters:
        maxStacktraceSize - the maximum number of characters to be allowed for stacktraces
      • setSendDefaultValues

        public void setSendDefaultValues​(boolean sendDefaultValues)

        Send default values. Fields with empty or default values, e.g. "-" for strings will not be added to the log message. This behaviour can be changed to always emit all fields.

        This method is called by Joran for the xml tag <sendDefaultValues> in the logback.xml configuration file.

        Parameters:
        sendDefaultValues - the maximum number of characters to be allowed for stacktraces
      • setJsonBuilder

        public void setJsonBuilder​(String className)

        Overwrites the default JsonBuilder with the given class. This can be used to modify the created JSON, e.g. with custom escaping or encoding.

        This method is called by Joran for the xml tag <jsonBuilder> in the logback.xml configuration file.

        Parameters:
        className - the maximum number of characters to be allowed for stacktraces
      • addLogbackContextFieldSupplier

        public void addLogbackContextFieldSupplier​(String className)

        Use this class to extract context fields from the logback Logevents. The provided classes are applied in order. Later instances can overwrite earlier generated fields. LogbackContextFieldSuppliers are executed after ContextFieldSuppliers.

        This method is called by Joran for the xml tag <logbackContextFieldSupplier> in the logback.xml configuration file.

        Parameters:
        className - the maximum number of characters to be allowed for stacktraces
      • addContextFieldSupplier

        public void addContextFieldSupplier​(String className)

        Use this class to to provide additional context fields, e.g. from the environment. The provided classes are applied in order. Later instances can overwrite earlier generated fields. LogbackContextFieldSuppliers are executed after ContextFieldSuppliers.

        This method is called by Joran for the xml tag <contextFieldSupplier> in the logback.xml configuration file.

        Parameters:
        className - the maximum number of characters to be allowed for stacktraces
      • start

        public void start()
        Specified by:
        start in interface ch.qos.logback.core.spi.LifeCycle
        Overrides:
        start in class ch.qos.logback.core.encoder.EncoderBase<ch.qos.logback.classic.spi.ILoggingEvent>
      • headerBytes

        public byte[] headerBytes()
      • footerBytes

        public byte[] footerBytes()
      • encode

        public byte[] encode​(ch.qos.logback.classic.spi.ILoggingEvent event)