Class JsonBuffer
- All Implemented Interfaces:
LogEncoder.Buffer, AutoCloseable
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumExtended fields are just fields that have some special prefix for things like GELF and ECS.static enumJSON tokens. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault initial capacity of the raw JSON byte buffer, matching what this class has always used - big enough that most events never need to grow it.static final intDefault initial capacity ofgetFormattedMessageBuilder().static final intA flag to indicate this field is extended which means it will be prefixed withJsonBuffer.ExtendedFieldPrefix. -
Constructor Summary
ConstructorsConstructorDescriptionJsonBuffer(boolean prettyPrint, JsonBuffer.ExtendedFieldPrefix extendedFieldPrefix, int maxBufferSize) Create a JSON buffer. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Prepare the buffer for reuse.voidThe appender will call this usually within a lock to transfer content from the buffer to the output.Reusable String buffer for formatted messages.booleanWhether this buffer has grown large enough (its capacity, not how much of that capacity the last event actually used) that its backing storage is worth shrinking back down rather than kept at its grown size indefinitely - reused buffers only grow on their own, they never shrink back down without deliberate help.final voidwrite(JsonBuffer.JSONToken token) Writes a JSON token.final intWrites a string field.final intWrites a string field.final voidEnds an array element object previously started withwriteArrayElementObjectStart(int).final intwriteArrayElementObjectStart(int index) Starts an anonymous JSON object as an array element (no field name), e.g. the{in[{...},{...}].final voidEnds a nested JSON array previously started withwriteArrayStart(String, int, int).final intwriteArrayStart(String k, int index, int flag) Starts a nested JSON array as a field value, e.g.final intwriteDouble(String k, double v, int index, int flag) Writes a double field.final intWrites a string field.final voidEfficiently writes a line feed.final intWrites a long field.final voidEnds a nested JSON object previously started withwriteObjectStart(String, int, int).final intwriteObjectStart(String k, int index, int flag) Starts a nested JSON object as a field value, e.g.Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface LogEncoder.Buffer
close
-
Field Details
-
DEFAULT_INITIAL_JSON_CAPACITY
Default initial capacity of the raw JSON byte buffer, matching what this class has always used - big enough that most events never need to grow it.- See Also:
-
DEFAULT_INITIAL_MESSAGE_CAPACITY
Default initial capacity ofgetFormattedMessageBuilder(). Bigger thanStringBuilder's own default of 16 since most log messages exceed that immediately, but still small relative toDEFAULT_INITIAL_JSON_CAPACITYsince this buffer is just the formatted message, not the whole encoded event.- See Also:
-
EXTENDED_F
A flag to indicate this field is extended which means it will be prefixed withJsonBuffer.ExtendedFieldPrefix.- See Also:
-
-
Constructor Details
-
JsonBuffer
public JsonBuffer(boolean prettyPrint, JsonBuffer.ExtendedFieldPrefix extendedFieldPrefix, int maxBufferSize) Create a JSON buffer.- Parameters:
prettyPrint- whether or not to pretty print the JSON.extendedFieldPrefix- prefix for extended fields.maxBufferSize- a negative value disablesisOversized()entirely. Applied, for simplicity, as a single combined threshold across both the raw JSON byte buffer andgetFormattedMessageBuilder()rather than tracked separately per buffer.
-
-
Method Details
-
drain
Description copied from interface:LogEncoder.BufferThe appender will call this usually within a lock to transfer content from the buffer to the output.- Specified by:
drainin interfaceLogEncoder.Buffer- Parameters:
output- output to receive content.event- log event.
-
clear
Description copied from interface:LogEncoder.BufferPrepare the buffer for reuse.An appender may not call clear before being passed to the encoder so the encoder should do its own clearing.
Built-in implementations also shrink their own backing storage back down here if
LogEncoder.Buffer.isOversized()- see that method - so a single unusually large event does not permanently bloat a buffer that gets reused for many more events after it.- Specified by:
clearin interfaceLogEncoder.Buffer
-
isOversized
Description copied from interface:LogEncoder.BufferWhether this buffer has grown large enough (its capacity, not how much of that capacity the last event actually used) that its backing storage is worth shrinking back down rather than kept at its grown size indefinitely - reused buffers only grow on their own, they never shrink back down without deliberate help. This is a soft ceiling checked between events, not a hard cap enforced during one: nothing here stops a single event from growing the buffer past whatever threshold it was configured with while that event is being encoded. Built-in implementations consult this themselves insideLogEncoder.Buffer.clear()to decide whether to shrink their own backing storage in place (e.g.StringBuilder.trimToSize(), or reallocating a smallerByteBuffer) - most callers do not need to call this directly; it remains here mainly for tests/observability and for a customLogEncoder.Bufferimplementation that wants the same self-shrinking behavior.What "large enough" means, and whether it means anything at all, is entirely up to the buffer/encoder - the same way charset is an encoder concern rather than something an appender configures (see
LogEncoder.builder(LogFormatter)). The default implementation returnsfalse- a buffer implementation that does not override this, or an encoder that never configured a threshold, never shrinks.- Specified by:
isOversizedin interfaceLogEncoder.Buffer- Returns:
trueif this buffer's backing storage has grown past whatever threshold it was configured with.
-
getFormattedMessageBuilder
Reusable String buffer for formatted messages.- Returns:
- buffer.
-
write
Writes a JSON token.- Parameters:
token- token not null.
-
writeLineFeed
Efficiently writes a line feed. -
write
-
write
Writes a string field.- Parameters:
k- field namev- valueindex- the current index for comma determinationflag- seeEXTENDED_F- Returns:
- index + 1
-
writeDouble
Writes a double field.- Parameters:
k- field namev- valueindex- the current index for comma determinationflag- seeEXTENDED_F- Returns:
- index + 1
-
writeInt
Writes a string field.- Parameters:
k- field namev- valueindex- the current index for comma determinationflag- seeEXTENDED_F- Returns:
- index + 1
-
writeLong
Writes a long field.- Parameters:
k- field namev- valueindex- the current index for comma determinationflag- seeEXTENDED_F- Returns:
- index + 1
-
writeObjectStart
Starts a nested JSON object as a field value, e.g."key":{. Fields written after this and before the matchingwriteObjectEnd()should use the returned index (a fresh comma-counter local to the nested object) rather than the outer index.- Parameters:
k- field nameindex- the current index (of the enclosing object) for comma determinationflag- seeEXTENDED_F- Returns:
- 0, the starting index for fields inside the nested object
-
writeObjectEnd
Ends a nested JSON object previously started withwriteObjectStart(String, int, int). -
writeArrayStart
Starts a nested JSON array as a field value, e.g."key":[. Elements written after this and before the matchingwriteArrayEnd()should usewriteArrayElementObjectStart(int)(with a fresh, local index) rather than the outer index.- Parameters:
k- field nameindex- the current index (of the enclosing object) for comma determinationflag- seeEXTENDED_F- Returns:
- 0, the starting index for elements inside the nested array
-
writeArrayEnd
Ends a nested JSON array previously started withwriteArrayStart(String, int, int). -
writeArrayElementObjectStart
Starts an anonymous JSON object as an array element (no field name), e.g. the{in[{...},{...}].- Parameters:
index- the current index (within the array) for comma determination- Returns:
- 0, the starting index for fields inside the element object
-
writeArrayElementObjectEnd
Ends an array element object previously started withwriteArrayElementObjectStart(int).
-