Module org.apache.logging.log4j
Package org.apache.logging.log4j.message
Class StructuredDataMessage
- java.lang.Object
-
- org.apache.logging.log4j.message.MapMessage<StructuredDataMessage,String>
-
- org.apache.logging.log4j.message.StructuredDataMessage
-
- All Implemented Interfaces:
Message
,MultiformatMessage
,MultiFormatStringBuilderFormattable
,StringBuilderFormattable
@AsynchronouslyFormattable public class StructuredDataMessage extends MapMessage<StructuredDataMessage,String>
Represents a Message that conforms to an RFC 5424 StructuredData element along with the syslog message.Thread-safety note: the contents of this message can be modified after construction. When using asynchronous loggers and appenders it is not recommended to modify this message after the message is logged, because it is undefined whether the logged message string will contain the old values or the modified values.
- See Also:
- RFC 5424
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StructuredDataMessage.Format
Supported formats.-
Nested classes/interfaces inherited from class org.apache.logging.log4j.message.MapMessage
MapMessage.MapFormat
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
StructuredDataMessage()
Basic constructor.StructuredDataMessage(String id, String msg, String type)
Creates a StructuredDataMessage using an ID (max 32 characters), message, and type (max 32 characters).StructuredDataMessage(String id, String msg, String type, int maxLength)
Creates a StructuredDataMessage using an ID (user specified max characters), message, and type (user specified maximum number of characters).StructuredDataMessage(String id, String msg, String type, Map<String,String> data)
Creates a StructuredDataMessage using an ID (max 32 characters), message, type (max 32 characters), and an initial map of structured data to include.StructuredDataMessage(String id, String msg, String type, Map<String,String> data, int maxLength)
Creates a StructuredDataMessage using an (user specified max characters), message, and type (user specified maximum number of characters, and an initial map of structured data to include.StructuredDataMessage(StructuredDataId id, String msg, String type)
Creates a StructuredDataMessage using a StructuredDataId, message, and type (max 32 characters).StructuredDataMessage(StructuredDataId id, String msg, String type, int maxLength)
Creates a StructuredDataMessage using a StructuredDataId, message, and type (max 32 characters).StructuredDataMessage(StructuredDataId id, String msg, String type, Map<String,String> data)
Creates a StructuredDataMessage using a StructuredDataId, message, type (max 32 characters), and an initial map of structured data to include.StructuredDataMessage(StructuredDataId id, String msg, String type, Map<String,String> data, int maxLength)
Creates a StructuredDataMessage using a StructuredDataId, message, type (max 32 characters), and an initial map of structured data to include.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
asString()
Formats the structured data as described in RFC 5424.String
asString(String format)
Formats the structured data as described in RFC 5424.String
asString(StructuredDataMessage.Format format, StructuredDataId structuredDataId)
Formats the structured data as described in RFC 5424.void
asString(StructuredDataMessage.Format format, StructuredDataId structuredDataId, StringBuilder sb)
Formats the structured data as described in RFC 5424.boolean
equals(Object o)
void
formatTo(String[] formats, StringBuilder buffer)
Writes a text representation of this object into the specifiedStringBuilder
, ideally without allocating temporary objects.void
formatTo(StringBuilder buffer)
Writes a text representation of this object into the specifiedStringBuilder
, ideally without allocating temporary objects.String
getFormat()
Returns the message.String[]
getFormats()
Returns the supported formats.String
getFormattedMessage()
Formats the message and return it.String
getFormattedMessage(String[] formats)
Formats the message according to the specified format.StructuredDataId
getId()
Returns this message id.String
getType()
Returns this message type.int
hashCode()
StructuredDataMessage
newInstance(Map<String,String> map)
Constructs a new instance based on an existing Map.protected void
setId(String id)
Sets the id from a String.protected void
setId(StructuredDataId id)
Sets the id.protected void
setMessageFormat(String msg)
protected void
setType(String type)
String
toString()
protected void
validate(String key, boolean value)
Default implementation does nothing.protected void
validate(String key, byte value)
Default implementation does nothing.protected void
validate(String key, char value)
Default implementation does nothing.protected void
validate(String key, double value)
Default implementation does nothing.protected void
validate(String key, float value)
Default implementation does nothing.protected void
validate(String key, int value)
Default implementation does nothing.protected void
validate(String key, long value)
Default implementation does nothing.protected void
validate(String key, short value)
Default implementation does nothing.protected void
validate(String key, Object value)
Default implementation does nothing.protected void
validate(String key, String value)
Default implementation does nothing.protected void
validateKey(String key)
-
Methods inherited from class org.apache.logging.log4j.message.MapMessage
appendMap, asJava, asJavaUnquoted, asJson, asXml, clear, containsKey, forEach, forEach, get, getData, getIndexedReadOnlyStringMap, getParameters, getThrowable, put, putAll, remove, toKey, with, with, with, with, with, with, with, with, with, with
-
-
-
-
Constructor Detail
-
StructuredDataMessage
public StructuredDataMessage(String id, String msg, String type)
Creates a StructuredDataMessage using an ID (max 32 characters), message, and type (max 32 characters).- Parameters:
id
- The String id.msg
- The message.type
- The message type.
-
StructuredDataMessage
public StructuredDataMessage(String id, String msg, String type, int maxLength)
Creates a StructuredDataMessage using an ID (user specified max characters), message, and type (user specified maximum number of characters).- Parameters:
id
- The String id.msg
- The message.type
- The message type.maxLength
- The maximum length of keys;- Since:
- 2.9
-
StructuredDataMessage
public StructuredDataMessage(String id, String msg, String type, Map<String,String> data)
Creates a StructuredDataMessage using an ID (max 32 characters), message, type (max 32 characters), and an initial map of structured data to include.- Parameters:
id
- The String id.msg
- The message.type
- The message type.data
- The StructuredData map.
-
StructuredDataMessage
public StructuredDataMessage(String id, String msg, String type, Map<String,String> data, int maxLength)
Creates a StructuredDataMessage using an (user specified max characters), message, and type (user specified maximum number of characters, and an initial map of structured data to include.- Parameters:
id
- The String id.msg
- The message.type
- The message type.data
- The StructuredData map.maxLength
- The maximum length of keys;- Since:
- 2.9
-
StructuredDataMessage
public StructuredDataMessage(StructuredDataId id, String msg, String type)
Creates a StructuredDataMessage using a StructuredDataId, message, and type (max 32 characters).- Parameters:
id
- The StructuredDataId.msg
- The message.type
- The message type.
-
StructuredDataMessage
public StructuredDataMessage(StructuredDataId id, String msg, String type, int maxLength)
Creates a StructuredDataMessage using a StructuredDataId, message, and type (max 32 characters).- Parameters:
id
- The StructuredDataId.msg
- The message.type
- The message type.maxLength
- The maximum length of keys;- Since:
- 2.9
-
StructuredDataMessage
public StructuredDataMessage(StructuredDataId id, String msg, String type, Map<String,String> data)
Creates a StructuredDataMessage using a StructuredDataId, message, type (max 32 characters), and an initial map of structured data to include.- Parameters:
id
- The StructuredDataId.msg
- The message.type
- The message type.data
- The StructuredData map.
-
StructuredDataMessage
public StructuredDataMessage(StructuredDataId id, String msg, String type, Map<String,String> data, int maxLength)
Creates a StructuredDataMessage using a StructuredDataId, message, type (max 32 characters), and an initial map of structured data to include.- Parameters:
id
- The StructuredDataId.msg
- The message.type
- The message type.data
- The StructuredData map.maxLength
- The maximum length of keys;- Since:
- 2.9
-
StructuredDataMessage
protected StructuredDataMessage()
Basic constructor.
-
-
Method Detail
-
getFormats
public String[] getFormats()
Returns the supported formats.- Specified by:
getFormats
in interfaceMultiformatMessage
- Overrides:
getFormats
in classMapMessage<StructuredDataMessage,String>
- Returns:
- An array of the supported format names.
-
getId
public StructuredDataId getId()
Returns this message id.- Returns:
- the StructuredDataId.
-
setId
protected void setId(String id)
Sets the id from a String. This ID can be at most 32 characters long.- Parameters:
id
- The String id.
-
setId
protected void setId(StructuredDataId id)
Sets the id.- Parameters:
id
- The StructuredDataId.
-
getType
public String getType()
Returns this message type.- Returns:
- the type.
-
setType
protected void setType(String type)
-
formatTo
public void formatTo(StringBuilder buffer)
Description copied from interface:StringBuilderFormattable
Writes a text representation of this object into the specifiedStringBuilder
, ideally without allocating temporary objects.- Specified by:
formatTo
in interfaceStringBuilderFormattable
- Overrides:
formatTo
in classMapMessage<StructuredDataMessage,String>
- Parameters:
buffer
- the StringBuilder to write into
-
formatTo
public void formatTo(String[] formats, StringBuilder buffer)
Description copied from interface:MultiFormatStringBuilderFormattable
Writes a text representation of this object into the specifiedStringBuilder
, ideally without allocating temporary objects.- Specified by:
formatTo
in interfaceMultiFormatStringBuilderFormattable
- Overrides:
formatTo
in classMapMessage<StructuredDataMessage,String>
- Parameters:
formats
- An array of Strings that provide extra information about how to format the message. Each MultiFormatStringBuilderFormattable implementation is free to use the provided formats however they choose.buffer
- the StringBuilder to write into
-
getFormat
public String getFormat()
Returns the message.- Specified by:
getFormat
in interfaceMessage
- Overrides:
getFormat
in classMapMessage<StructuredDataMessage,String>
- Returns:
- the message.
-
setMessageFormat
protected void setMessageFormat(String msg)
-
asString
public String asString()
Formats the structured data as described in RFC 5424.- Overrides:
asString
in classMapMessage<StructuredDataMessage,String>
- Returns:
- The formatted String.
-
asString
public String asString(String format)
Formats the structured data as described in RFC 5424.- Overrides:
asString
in classMapMessage<StructuredDataMessage,String>
- Parameters:
format
- The format identifier. Ignored in this implementation.- Returns:
- The formatted String.
-
asString
public final String asString(StructuredDataMessage.Format format, StructuredDataId structuredDataId)
Formats the structured data as described in RFC 5424.- Parameters:
format
- "full" will include the type and message. null will return only the STRUCTURED-DATA as described in RFC 5424structuredDataId
- The SD-ID as described in RFC 5424. If null the value in the StructuredData will be used.- Returns:
- The formatted String.
-
asString
public final void asString(StructuredDataMessage.Format format, StructuredDataId structuredDataId, StringBuilder sb)
Formats the structured data as described in RFC 5424.- Parameters:
format
- "full" will include the type and message. null will return only the STRUCTURED-DATA as described in RFC 5424structuredDataId
- The SD-ID as described in RFC 5424. If null the value in the StructuredData will be used.sb
- The StringBuilder to append the formatted message to.
-
getFormattedMessage
public String getFormattedMessage()
Formats the message and return it.- Specified by:
getFormattedMessage
in interfaceMessage
- Overrides:
getFormattedMessage
in classMapMessage<StructuredDataMessage,String>
- Returns:
- the formatted message.
-
getFormattedMessage
public String getFormattedMessage(String[] formats)
Formats the message according to the specified format.- Specified by:
getFormattedMessage
in interfaceMultiformatMessage
- Overrides:
getFormattedMessage
in classMapMessage<StructuredDataMessage,String>
- Parameters:
formats
- An array of Strings that provide extra information about how to format the message. StructuredDataMessage accepts only a format of "FULL" which will cause the event type to be prepended and the event message to be appended. Specifying any other value will cause only the StructuredData to be included. The default is "FULL".- Returns:
- the formatted message.
-
toString
public String toString()
- Overrides:
toString
in classMapMessage<StructuredDataMessage,String>
-
newInstance
public StructuredDataMessage newInstance(Map<String,String> map)
Description copied from class:MapMessage
Constructs a new instance based on an existing Map.- Overrides:
newInstance
in classMapMessage<StructuredDataMessage,String>
- Parameters:
map
- The Map.- Returns:
- A new MapMessage
-
equals
public boolean equals(Object o)
- Overrides:
equals
in classMapMessage<StructuredDataMessage,String>
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classMapMessage<StructuredDataMessage,String>
-
validate
protected void validate(String key, boolean value)
Description copied from class:MapMessage
Default implementation does nothing.- Overrides:
validate
in classMapMessage<StructuredDataMessage,String>
- Parameters:
key
- The key.value
- The boolean value.
-
validate
protected void validate(String key, byte value)
Description copied from class:MapMessage
Default implementation does nothing.- Overrides:
validate
in classMapMessage<StructuredDataMessage,String>
- Parameters:
key
- The key.value
- The byte value.- Since:
- 2.9
-
validate
protected void validate(String key, char value)
Description copied from class:MapMessage
Default implementation does nothing.- Overrides:
validate
in classMapMessage<StructuredDataMessage,String>
- Parameters:
key
- The key.value
- The char value.- Since:
- 2.9
-
validate
protected void validate(String key, double value)
Description copied from class:MapMessage
Default implementation does nothing.- Overrides:
validate
in classMapMessage<StructuredDataMessage,String>
- Parameters:
key
- The key.value
- The double value.- Since:
- 2.9
-
validate
protected void validate(String key, float value)
Description copied from class:MapMessage
Default implementation does nothing.- Overrides:
validate
in classMapMessage<StructuredDataMessage,String>
- Parameters:
key
- The key.value
- The float value.- Since:
- 2.9
-
validate
protected void validate(String key, int value)
Description copied from class:MapMessage
Default implementation does nothing.- Overrides:
validate
in classMapMessage<StructuredDataMessage,String>
- Parameters:
key
- The key.value
- The integer value.- Since:
- 2.9
-
validate
protected void validate(String key, long value)
Description copied from class:MapMessage
Default implementation does nothing.- Overrides:
validate
in classMapMessage<StructuredDataMessage,String>
- Parameters:
key
- The key.value
- The long value.- Since:
- 2.9
-
validate
protected void validate(String key, Object value)
Description copied from class:MapMessage
Default implementation does nothing.- Overrides:
validate
in classMapMessage<StructuredDataMessage,String>
- Parameters:
key
- The key.value
- The Object value.- Since:
- 2.9
-
validate
protected void validate(String key, short value)
Description copied from class:MapMessage
Default implementation does nothing.- Overrides:
validate
in classMapMessage<StructuredDataMessage,String>
- Parameters:
key
- The key.value
- The short value.- Since:
- 2.9
-
validate
protected void validate(String key, String value)
Description copied from class:MapMessage
Default implementation does nothing.- Overrides:
validate
in classMapMessage<StructuredDataMessage,String>
- Parameters:
key
- The key.value
- The string value.
-
validateKey
protected void validateKey(String key)
-
-