Package com.composum.sling.core.logging
Class Message
- java.lang.Object
-
- com.composum.sling.core.logging.Message
-
- All Implemented Interfaces:
Cloneable
public class Message extends Object implements Cloneable
A container for a message, e.g. about internal processes, that can be presented to the user. It could be localized, and there might be details which can be suppressed, depending on the user / user settings or choices.For proper i18n in JSON serialization you need to register
MessageTypeAdapterFactory(SlingHttpServletRequest)withGsonBuilder.registerTypeAdapterFactory(TypeAdapterFactory). The defaultJsonAdapteris just a fallback that uses no i18n. CAUTION: careful when extending this class - theMessageTypeAdapterFactorymight not work for derived classes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMessage.LevelKind of message, also used as loglevel when this is logged.
-
Field Summary
Fields Modifier and Type Field Description protected @Nullable Object[]argumentsprotected @Nullable Stringcategoryprotected @Nullable Stringcontextprotected @Nullable List<Message>detailsprotected @Nullable Stringlabelprotected Message.Levellevelprotected @Nullable Message.LevellogLevelThe level at which this message is logged, if different from level.protected @Nullable StringpathOptional resource path this message is about.protected StringrawTextThe raw version of the text which can contain {}-placeholders and is used as i18n-key.protected StringtextIf set, an i18n-ed version ofrawTextwith all placeholders replaced.protected Longtimestamp
-
Constructor Summary
Constructors Constructor Description Message()Deprecated.only for JSON deserialization.Message(@Nullable Message.Level level, @NotNull String rawText, Object... arguments)Creates a message.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description @NotNull MessageaddDetail(@NotNull Message detailMessage)Adds a detailmessage.protected voidappendFormattedTo(StringBuilder buf, String indent, Message.Level baseLevel)Messageclone()static @NotNull Messagedebug(@NotNull String text, Object... arguments)Convenience-method - constructs withdebug(java.lang.String, java.lang.Object...).static @NotNull Messageerror(@NotNull String text, Object... arguments)Convenience-method - constructs witherror(java.lang.String, java.lang.Object...).@NotNull List<Object>getArguments()Optional arguments used in placeholders of thegetText().@Nullable StringgetCategory()Can optionally be used to categorize messages for filtering / sorting.@Nullable StringgetContext()Optional: a context of the message, such as the dialog tab in a validation message.@NotNull List<Message>getDetails()Optional unmodifiable collection of detail messages describing the problem further.@Nullable StringgetLabel()Optional: label of the field which the message is about, primarily in validation messages.@NotNull Message.LevelgetLevel()The kind of message - informational, warning, error.@Nullable Message.LevelgetLogLevel()The level with which we will log the message ifMessageContainerhas a logger.StringgetMessage(@Nullable org.apache.sling.api.SlingHttpServletRequest request)LikegetText(), but returns the text i18n-ed for the request and parameters replaced.@Nullable StringgetPath()Optional resource path this message is about.@NotNull StringgetRawText()The raw, un-i18n-ed, human readable message text, possibly with argument placeholders {}.@NotNull StringgetText()A human readable i18n-ed message text composed fromgetRawText(), all argument placeholders {} replaced by the correspondinggetArguments().protected @Nullable ThrowablegetThrowableArgument()Return the last argument if it is a Throwable.@Nullable LonggetTimestamp()Time the message was created, as inSystem.currentTimeMillis().@Nullable DategetTimestampAsDate()Time the message was created.static @NotNull Messageinfo(@NotNull String text, Object... arguments)Convenience-method - constructs withinfo(java.lang.String, java.lang.Object...).@NotNull MessagelogInto(@Nullable org.slf4j.Logger log)Logs the message into the specified logger.@NotNull MessagelogInto(@Nullable org.slf4j.Logger log, @Nullable Throwable cause)Logs the message into the specified logger, including the {cause}'s stacktrace.protected MessageprepareForJsonSerialization(org.apache.sling.api.SlingHttpServletRequest request)@NotNull MessagesetCategory(@Nullable String category)Sets the optional category: can optionally be used to categorize messages for filtering / sorting.@NotNull MessagesetContext(@Nullable String context)Sets a context: a context of the message, such as the dialog tab in a validation message.@NotNull MessagesetLabel(@Nullable String label)Sets the optional context: a label of the field which the message is about, primarily in validation messages.@NotNull MessagesetLogLevel(@Nullable Message.Level logLevel)Sets the level with which we will log the message ifMessageContainerhas a logger.@NotNull MessagesetPath(@Nullable String path)Sets the optional resource path this message is about, and returns this message for builder style construction.StringtoFormattedMessage()Return a full text representation of the message with replaced arguments and appended details.static MessagevalidationError(@Nullable String context, @Nullable String label, @NotNull String text, Object... args)Convenience-method - constructs a validation message (which is logged only at level debug) witherror(java.lang.String, java.lang.Object...).static MessagevalidationInfo(@Nullable String context, @Nullable String label, @NotNull String text, Object... args)Convenience-method - constructs a validation message (which is logged only at level debug) withinfo(java.lang.String, java.lang.Object...).static MessagevalidationWarn(@Nullable String context, @Nullable String label, @NotNull String text, Object... args)Convenience-method - constructs a validation message (which is logged only at level debug) withwarn(java.lang.String, java.lang.Object...).static @NotNull Messagewarn(@NotNull String text, Object... arguments)Convenience-method - constructs withwarn(java.lang.String, java.lang.Object...).
-
-
-
Field Detail
-
level
protected Message.Level level
- See Also:
getLevel()
-
logLevel
@Nullable protected @Nullable Message.Level logLevel
The level at which this message is logged, if different from level. This might be transient, but we make it non-transient because messages might be transmitted to a remote system and logged there again.- See Also:
getLogLevel()
-
context
@Nullable protected @Nullable String context
- See Also:
getContext()
-
label
@Nullable protected @Nullable String label
- See Also:
getLabel()
-
text
protected String text
If set, an i18n-ed version ofrawTextwith all placeholders replaced. This is modified during JSON-serialization.
-
rawText
protected String rawText
The raw version of the text which can contain {}-placeholders and is used as i18n-key.
-
path
@Nullable protected @Nullable String path
Optional resource path this message is about.
-
arguments
@Nullable protected @Nullable Object[] arguments
- See Also:
getArguments()
-
category
@Nullable protected @Nullable String category
- See Also:
getCategory()
-
details
@Nullable protected @Nullable List<Message> details
- See Also:
getDetails()
-
timestamp
protected Long timestamp
- See Also:
getTimestamp()
-
-
Constructor Detail
-
Message
@Deprecated public Message()
Deprecated.only for JSON deserialization.
-
Message
public Message(@Nullable @Nullable Message.Level level, @NotNull @NotNull String rawText, Object... arguments)Creates a message.- Parameters:
level- the level of the message, defaultinfo(java.lang.String, java.lang.Object...)rawText- the untranslated text of message, possibly with placeholders {} for argumentsarguments- optional arguments placed in placeholders. Caution: must be primitive types if this is to be transmitted with JSON!
-
-
Method Detail
-
error
@NotNull public static @NotNull Message error(@NotNull @NotNull String text, Object... arguments)
Convenience-method - constructs witherror(java.lang.String, java.lang.Object...).- Parameters:
text- the untranslated text of message, possibly with placeholders {} for argumentsarguments- optional arguments placed in placeholders. Caution: must be primitive types if this is to be transmitted with JSON!
-
validationError
public static Message validationError(@Nullable @Nullable String context, @Nullable @Nullable String label, @NotNull @NotNull String text, Object... args)
Convenience-method - constructs a validation message (which is logged only at level debug) witherror(java.lang.String, java.lang.Object...).- Parameters:
context- an optional context of the message, such as the dialog tab in a validation message.label- an optional: label of the field which the message is about, primarily in validation messages.text- the untranslated text of message, possibly with placeholders {} for argumentsarguments- optional arguments placed in placeholders. Caution: must be primitive types if this is to be transmitted with JSON!
-
warn
@NotNull public static @NotNull Message warn(@NotNull @NotNull String text, Object... arguments)
Convenience-method - constructs withwarn(java.lang.String, java.lang.Object...).- Parameters:
text- the untranslated text of message, possibly with placeholders {} for argumentsarguments- optional arguments placed in placeholders. Caution: must be primitive types if this is to be transmitted with JSON!
-
validationWarn
public static Message validationWarn(@Nullable @Nullable String context, @Nullable @Nullable String label, @NotNull @NotNull String text, Object... args)
Convenience-method - constructs a validation message (which is logged only at level debug) withwarn(java.lang.String, java.lang.Object...).- Parameters:
context- an optional context of the message, such as the dialog tab in a validation message.label- an optional: label of the field which the message is about, primarily in validation messages.text- the untranslated text of message, possibly with placeholders {} for argumentsarguments- optional arguments placed in placeholders. Caution: must be primitive types if this is to be transmitted with JSON!
-
info
@NotNull public static @NotNull Message info(@NotNull @NotNull String text, Object... arguments)
Convenience-method - constructs withinfo(java.lang.String, java.lang.Object...).- Parameters:
text- the untranslated text of message, possibly with placeholders {} for argumentsarguments- optional arguments placed in placeholders. Caution: must be primitive types if this is to be transmitted with JSON!
-
validationInfo
public static Message validationInfo(@Nullable @Nullable String context, @Nullable @Nullable String label, @NotNull @NotNull String text, Object... args)
Convenience-method - constructs a validation message (which is logged only at level debug) withinfo(java.lang.String, java.lang.Object...).- Parameters:
context- an optional context of the message, such as the dialog tab in a validation message.label- an optional: label of the field which the message is about, primarily in validation messages.text- the untranslated text of message, possibly with placeholders {} for argumentsarguments- optional arguments placed in placeholders. Caution: must be primitive types if this is to be transmitted with JSON!
-
debug
@NotNull public static @NotNull Message debug(@NotNull @NotNull String text, Object... arguments)
Convenience-method - constructs withdebug(java.lang.String, java.lang.Object...).- Parameters:
text- the untranslated text of message, possibly with placeholders {} for argumentsarguments- optional arguments placed in placeholders. Caution: must be primitive types if this is to be transmitted with JSON!
-
addDetail
@NotNull public @NotNull Message addDetail(@NotNull @NotNull Message detailMessage)
Adds a detailmessage.- Returns:
- this for builder style operation chaining.
-
getTimestamp
@Nullable public @Nullable Long getTimestamp()
Time the message was created, as inSystem.currentTimeMillis().
-
getTimestampAsDate
@Nullable public @Nullable Date getTimestampAsDate()
Time the message was created.
-
getLevel
@NotNull public @NotNull Message.Level getLevel()
The kind of message - informational, warning, error. Default isinfo(java.lang.String, java.lang.Object...).
-
getLogLevel
@Nullable public @Nullable Message.Level getLogLevel()
The level with which we will log the message ifMessageContainerhas a logger. Transient attribute - will not be transmitted in JSON. This can be different from thegetLevel(), e.g. in the case of validation messages which should only be logged as debug.Message.Level.nonemeans it will not be logged; null means thatgetLevel()is used.
-
setLogLevel
@NotNull public @NotNull Message setLogLevel(@Nullable @Nullable Message.Level logLevel)
Sets the level with which we will log the message ifMessageContainerhas a logger. Transient attribute - will not be transmitted in JSON. This can be different from thegetLevel(), e.g. in the case of validation messages which should only be logged as debug.Message.Level.nonemeans it will not be logged; null means thatgetLevel()is used.- Returns:
- {this} for chaining calls in builder-style
-
getRawText
@NotNull public @NotNull String getRawText()
The raw, un-i18n-ed, human readable message text, possibly with argument placeholders {}. If i18n is wanted, this is the key for the i18n - all variable parts should be put into the arguments. Mandatory part of a message.
-
getText
@NotNull public @NotNull String getText()
A human readable i18n-ed message text composed fromgetRawText(), all argument placeholders {} replaced by the correspondinggetArguments(). This is lazily created fromgetRawText()andgetArguments(). In JSON-serializations, thegetRawText()is i18n-ed to the request whenMessageTypeAdapterFactoryis correctly used.
-
getMessage
public String getMessage(@Nullable @Nullable org.apache.sling.api.SlingHttpServletRequest request)
LikegetText(), but returns the text i18n-ed for the request and parameters replaced.
-
getPath
@Nullable public @Nullable String getPath()
Optional resource path this message is about.
-
setPath
@NotNull public @NotNull Message setPath(@Nullable @Nullable String path)
Sets the optional resource path this message is about, and returns this message for builder style construction.- Returns:
- this
-
getArguments
@NotNull public @NotNull List<Object> getArguments()
Optional arguments used in placeholders of thegetText(). If transmission over JSON is needed, these must be serializable with GSON.
-
getCategory
@Nullable public @Nullable String getCategory()
Can optionally be used to categorize messages for filtering / sorting. This is not meant to be shown directly to the user.
-
setCategory
@NotNull public @NotNull Message setCategory(@Nullable @Nullable String category)
Sets the optional category: can optionally be used to categorize messages for filtering / sorting. This is not meant to be shown directly to the user.- Returns:
- this for builder style operation chaining.
-
getLabel
@Nullable public @Nullable String getLabel()
Optional: label of the field which the message is about, primarily in validation messages. (Not the human-readable but the programmatical id is meant.)
-
setLabel
@NotNull public @NotNull Message setLabel(@Nullable @Nullable String label)
Sets the optional context: a label of the field which the message is about, primarily in validation messages. (Not the human-readable but the programmatical id is meant.)- Returns:
- this for builder style operation chaining.
-
getContext
@Nullable public @Nullable String getContext()
Optional: a context of the message, such as the dialog tab in a validation message. (Not the human-readable but the programmatical id is meant.)
-
setContext
@NotNull public @NotNull Message setContext(@Nullable @Nullable String context)
Sets a context: a context of the message, such as the dialog tab in a validation message. (Not the human-readable but the programmatical id is meant.)- Returns:
- this for builder style operation chaining.
-
getDetails
@NotNull public @NotNull List<Message> getDetails()
Optional unmodifiable collection of detail messages describing the problem further. To add details useaddDetail(Message).- See Also:
addDetail(Message)
-
clone
public Message clone() throws CloneNotSupportedException
- Overrides:
clonein classObject- Throws:
CloneNotSupportedException
-
prepareForJsonSerialization
protected Message prepareForJsonSerialization(org.apache.sling.api.SlingHttpServletRequest request) throws CloneNotSupportedException
Clones theMessageand sets itstextto a properly i18n-ed version, and also makes Strings out of non-String and non-Numeric arguments to avoid problems with not JSON serializable arguments.Tradeoff: keeping numbers is more efficient but GSON turns numbers into doubles on deserialization which sometimes creates differences when formatting is repeated on a serialized and deserialized
Message.- Throws:
CloneNotSupportedException
-
logInto
@NotNull public @NotNull Message logInto(@Nullable @Nullable org.slf4j.Logger log)
Logs the message into the specified logger. Can be done automatically by aMessageContainerifMessageContainer(Logger)is used.- Parameters:
log- the log to write the message into- Returns:
- this message for builder-style operation-chaining.
- See Also:
MessageContainer(Logger)
-
logInto
@NotNull public @NotNull Message logInto(@Nullable @Nullable org.slf4j.Logger log, @Nullable @Nullable Throwable cause)
Logs the message into the specified logger, including the {cause}'s stacktrace. Can be done automatically by aMessageContainerifMessageContainer(Logger)is used.- Parameters:
log- the log to write the message intocause- optionally, an exception that is logged as a cause of the message- Returns:
- this message for builder-style operation-chaining.
-
getThrowableArgument
@Nullable protected @Nullable Throwable getThrowableArgument()
Return the last argument if it is a Throwable. This is the stacktrace logged if there was not given one withlogInto(Logger, Throwable)explicitly. Compatible with slf4j: use last argument if it is an exception http://slf4j.org/faq.html#paramException
-
toFormattedMessage
public String toFormattedMessage()
Return a full text representation of the message with replaced arguments and appended details. Mainly for logging / debugging purposes. Not i18n-ed.
-
appendFormattedTo
protected void appendFormattedTo(StringBuilder buf, String indent, Message.Level baseLevel)
-
-