Class JsonEncoder
- All Implemented Interfaces:
Encoder<ILoggingEvent>,ContextAware,LifeCycle
ILoggingEvent is encoded into a JSON object containing fields such as timestamp, level, message,
and optional elements like MDC properties, markers, and stack traces.
This encoder supports extensive configuration through boolean flags to include or exclude specific fields in the output, allowing customization for different logging needs. For example, you can enable/disable sequence numbers, nanoseconds, thread names, logger context, markers, MDC, key-value pairs, arguments, and throwable information.
The encoder is designed for extensibility: subclasses can override protected methods (e.g.,
appendLoggerContext(java.lang.StringBuilder, ch.qos.logback.classic.spi.LoggerContextVO), appendThrowableProxy(java.lang.StringBuilder, java.lang.String, ch.qos.logback.classic.spi.IThrowableProxy), appendMarkers(java.lang.StringBuilder, ch.qos.logback.classic.spi.ILoggingEvent)) to customize
how specific parts of the JSON are generated. Additionally, the appendCustomFields(java.lang.StringBuilder, ch.qos.logback.classic.spi.ILoggingEvent) hook
allows appending custom top-level fields to the JSON object.
Configuration
Use the setter methods (e.g., setWithSequenceNumber(boolean), setWithTimestamp(boolean)) to control
which fields are included. By default, most fields are enabled except withFormattedMessage.
Example Usage
<appender name="JSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.JsonEncoder">
<withSequenceNumber>false</withSequenceNumber>
<withNanoseconds>false</withNanoseconds>
<withThreadName>false</withThreadName>
</encoder>
</appender>
This produces output similar to the following (on a single line):
{"timestamp":1640995200000,"level":"INFO","loggerName":"com.example.MyClass","context":{"name":"default","birthdate":1640995200000,"properties":{}},"message":"Hello World"}- Since:
- 1.3.8/1.4.8
- Author:
- Ceki Gülcü
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringstatic final Stringprotected static final charprotected static final charprotected static final Stringstatic final Stringstatic final Stringstatic final Stringprotected static final charstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringprotected static final charprotected static final charprotected static final charprotected static final Stringstatic final Stringprotected static final charstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringprotected static final charFields inherited from class ch.qos.logback.core.encoder.EncoderBase
startedFields inherited from class ch.qos.logback.core.spi.ContextAwareBase
context -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidappendArgumentArray(StringBuilder sb, ILoggingEvent event) protected voidappendCustomFields(StringBuilder sb, ILoggingEvent event) Hook allowing subclasses to append additional fields into the root JSON object.protected voidappenderMember(StringBuilder sb, String key, String value) protected voidappenderMemberWithIntValue(StringBuilder sb, String key, int value) protected voidappenderMemberWithLongValue(StringBuilder sb, String key, long value) protected voidappendKeyValuePairs(StringBuilder sb, ILoggingEvent event) protected voidappendLoggerContext(StringBuilder sb, LoggerContextVO loggerContextVO) protected voidprotected voidappendMarkers(StringBuilder sb, ILoggingEvent event) protected voidappendMDC(StringBuilder sb, ILoggingEvent event) protected voidappendSTEPArray(StringBuilder sb, StackTraceElementProxy[] stepArray, int commonFrames) protected voidappendThrowableProxy(StringBuilder sb, String attributeName, IThrowableProxy itp) protected voidappendThrowableProxy(StringBuilder sb, String attributeName, IThrowableProxy itp, boolean appendValueSeparator) protected voidappendValueSeparator(StringBuilder sb, boolean... subsequentConditionals) Append a JSON value separator (a comma) to the providedStringBuilderwhen any of the supplied boolean flags indicate that a subsequent element is present.byte[]encode(ILoggingEvent event) Encode an event as bytes.byte[]Get footer bytes.byte[]Get header bytes.voidsetWithArguments(boolean withArguments) Enable or disable the inclusion of the event argument array in the encoded output.voidsetWithContext(boolean withContext) Enable or disable the inclusion of the logger context information.voidsetWithFormattedMessage(boolean withFormattedMessage) Enable or disable the inclusion of the formatted message in the encoded output.voidsetWithKVPList(boolean withKVPList) Enable or disable the inclusion of key-value pairs attached to the logging event.voidsetWithLevel(boolean withLevel) Enable or disable the inclusion of the log level in the encoded output.voidsetWithLoggerName(boolean withLoggerName) Enable or disable the inclusion of the logger name in the encoded output.voidsetWithMarkers(boolean withMarkers) Enable or disable the inclusion of markers in the encoded output.voidsetWithMDC(boolean withMDC) Enable or disable the inclusion of MDC properties in the encoded output.voidsetWithMessage(boolean withMessage) Enable or disable the inclusion of the raw message text in the encoded output.voidsetWithNanoseconds(boolean withNanoseconds) Set whether nanoseconds will be included in the timestamp output.voidsetWithSequenceNumber(boolean withSequenceNumber) Set whether the sequence number is included in each encoded event.voidsetWithThreadName(boolean withThreadName) Enable or disable the inclusion of the thread name in the encoded output.voidsetWithThrowable(boolean withThrowable) Enable or disable the inclusion of throwable information in the encoded output.voidsetWithTimestamp(boolean withTimestamp) Set whether the event timestamp is included in each encoded event.Methods inherited from class ch.qos.logback.core.encoder.EncoderBase
isStarted, start, stopMethods inherited from class ch.qos.logback.core.spi.ContextAwareBase
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, getDeclaredOrigin, getStatusManager, setContextMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ch.qos.logback.core.spi.ContextAware
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, setContext
-
Field Details
-
CONTEXT_ATTR_NAME
- See Also:
-
NAME_ATTR_NAME
- See Also:
-
BIRTHDATE_ATTR_NAME
- See Also:
-
CONTEXT_PROPERTIES_ATTR_NAME
- See Also:
-
TIMESTAMP_ATTR_NAME
- See Also:
-
NANOSECONDS_ATTR_NAME
- See Also:
-
SEQUENCE_NUMBER_ATTR_NAME
- See Also:
-
LEVEL_ATTR_NAME
- See Also:
-
MARKERS_ATTR_NAME
- See Also:
-
THREAD_NAME_ATTR_NAME
- See Also:
-
MDC_ATTR_NAME
- See Also:
-
LOGGER_ATTR_NAME
- See Also:
-
MESSAGE_ATTR_NAME
- See Also:
-
FORMATTED_MESSAGE_ATTR_NAME
- See Also:
-
ARGUMENT_ARRAY_ATTR_NAME
- See Also:
-
KEY_VALUE_PAIRS_ATTR_NAME
- See Also:
-
THROWABLE_ATTR_NAME
- See Also:
-
CAUSE_ATTR_NAME
- See Also:
-
SUPPRESSED_ATTR_NAME
- See Also:
-
COMMON_FRAMES_COUNT_ATTR_NAME
- See Also:
-
CLASS_NAME_ATTR_NAME
- See Also:
-
METHOD_NAME_ATTR_NAME
- See Also:
-
STEP_ARRAY_NAME_ATTRIBUTE
- See Also:
-
OPEN_OBJ
- See Also:
-
CLOSE_OBJ
- See Also:
-
OPEN_ARRAY
- See Also:
-
CLOSE_ARRAY
- See Also:
-
QUOTE
- See Also:
-
SP
- See Also:
-
ENTRY_SEPARATOR
- See Also:
-
COL_SP
-
QUOTE_COL
-
VALUE_SEPARATOR
- See Also:
-
-
Constructor Details
-
JsonEncoder
public JsonEncoder()
-
-
Method Details
-
headerBytes
Description copied from interface:EncoderGet header bytes. This method is typically called upon opening of an output stream.- Returns:
- header bytes. Null values are allowed.
-
encode
-
appendValueSeparator
Append a JSON value separator (a comma) to the providedStringBuilderwhen any of the supplied boolean flags indicate that a subsequent element is present.Callers pass a sequence of booleans that represent whether subsequent JSON members will be written. If at least one of those booleans is
true, this method appends a single comma (',') to separate JSON fields.This method is protected so subclasses that extend the encoder can reuse or override the logic for inserting separators between generated JSON members.
- Parameters:
sb- theStringBuilderto append the separator to; must not benullsubsequentConditionals- one or more booleans indicating whether subsequent JSON elements will be written. If any value istrue, a comma is appended.
-
appendLoggerContext
-
appendMap
-
appendThrowableProxy
-
appendThrowableProxy
protected void appendThrowableProxy(StringBuilder sb, String attributeName, IThrowableProxy itp, boolean appendValueSeparator) -
appendSTEPArray
protected void appendSTEPArray(StringBuilder sb, StackTraceElementProxy[] stepArray, int commonFrames) -
appendCustomFields
Hook allowing subclasses to append additional fields into the root JSON object. Default implementation is a no-op.Subclasses may append additional top-level JSON members here. If a subclass writes additional members it should prepend them with
VALUE_SEPARATOR(a comma) if necessary to keep the JSON valid. Implementations must not close the root JSON object or write the final line separator;JsonEncoderhandles those.- Parameters:
sb- the StringBuilder that accumulates the JSON output; never nullevent- the logging event being encoded; never null
-
appenderMember
-
appenderMemberWithIntValue
-
appenderMemberWithLongValue
-
appendKeyValuePairs
-
appendArgumentArray
-
appendMarkers
-
appendMDC
-
setWithSequenceNumber
Set whether the sequence number is included in each encoded event.- Parameters:
withSequenceNumber-trueto include the sequence number in the output- Since:
- 1.5.0
-
setWithTimestamp
Set whether the event timestamp is included in each encoded event.- Parameters:
withTimestamp-trueto include the event timestamp in the output- Since:
- 1.5.0
-
setWithNanoseconds
Set whether nanoseconds will be included in the timestamp output.- Parameters:
withNanoseconds-trueto include nanoseconds in the timestamp output- Since:
- 1.5.0
-
setWithLevel
Enable or disable the inclusion of the log level in the encoded output.- Parameters:
withLevel-trueto include the log level. Default istrue.
-
setWithThreadName
Enable or disable the inclusion of the thread name in the encoded output.- Parameters:
withThreadName-trueto include the thread name. Default istrue.
-
setWithLoggerName
Enable or disable the inclusion of the logger name in the encoded output.- Parameters:
withLoggerName-trueto include the logger name. Default istrue.
-
setWithContext
Enable or disable the inclusion of the logger context information.- Parameters:
withContext-trueto include the logger context. Default istrue.
-
setWithMarkers
Enable or disable the inclusion of markers in the encoded output.- Parameters:
withMarkers-trueto include markers. Default istrue.
-
setWithMDC
Enable or disable the inclusion of MDC properties in the encoded output.- Parameters:
withMDC-trueto include MDC properties. Default istrue.
-
setWithKVPList
Enable or disable the inclusion of key-value pairs attached to the logging event.- Parameters:
withKVPList-trueto include the key/value pairs list. Default istrue.
-
setWithMessage
Enable or disable the inclusion of the raw message text in the encoded output.- Parameters:
withMessage-trueto include the message. Default istrue.
-
setWithArguments
Enable or disable the inclusion of the event argument array in the encoded output.- Parameters:
withArguments-trueto include the argument array. Default istrue.
-
setWithThrowable
Enable or disable the inclusion of throwable information in the encoded output.- Parameters:
withThrowable-trueto include throwable/stacktrace information. Default istrue.
-
setWithFormattedMessage
Enable or disable the inclusion of the formatted message in the encoded output.- Parameters:
withFormattedMessage-trueto include the formatted message. Default isfalse.
-