Package com.composum.sling.core.logging
Class MessageContainer
- java.lang.Object
-
- com.composum.sling.core.logging.MessageContainer
-
-
Field Summary
Fields Modifier and Type Field Description protected Object
lockObject
Synchronizing on this when accessing messages.protected @Nullable org.slf4j.Logger
log
A logger whereadd(Message)
automatically logs to.protected static Comparator<Message>
MESSAGE_TIME_COMPARATOR
Used to sort messages by their time.protected @Nullable List<Message>
messages
-
Constructor Summary
Constructors Constructor Description MessageContainer()
Default constructor which means we do not log added messages - usually it's better to useMessageContainer(Logger)
and log things immediately.MessageContainer(@Nullable org.slf4j.Logger log)
Constructor that allows to set a logger to whichadd(Message)
automatically writes the messages.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description @NotNull MessageContainer
add(@Nullable Message message)
Adds a message to the container, and logs it into the logger if one was specified for this container.@NotNull MessageContainer
add(@Nullable Message message, @Nullable Throwable throwable)
Adds a message to the container, and logs it into the logger if one was specified for this container.@NotNull MessageContainer
addAll(@Nullable MessageContainer messageContainer)
Adds all messages of aMessageContainer
to this container.@NotNull MessageContainer
clear()
Removes all messages.void
forEach(Consumer<? super Message> action)
Iterates over a readonly view.@NotNull List<Message>
getMessages()
A (unmodifiable) snapshot of the list of messages.boolean
hasError()
True if a MessageContainer contains a message with anMessage.error(java.lang.String, java.lang.Object...)
.boolean
isEmpty()
Whether there are any messages.Iterator<Message>
iterator()
Iterates over a readonly view.void
logInto(@Nullable org.slf4j.Logger logger)
Logs the messages into the given container.void
setLogger(org.slf4j.Logger logger)
Modifies the logger newlyadd(Message)
-ed /add(Message, Throwable)
-ed messages are logged to.Spliterator<Message>
spliterator()
Iterates over a readonly view.
-
-
-
Field Detail
-
MESSAGE_TIME_COMPARATOR
protected static final Comparator<Message> MESSAGE_TIME_COMPARATOR
Used to sort messages by their time.
-
log
@Nullable protected transient volatile @Nullable org.slf4j.Logger log
A logger whereadd(Message)
automatically logs to.
-
lockObject
protected final transient Object lockObject
Synchronizing on this when accessing messages.
-
messages
@Nullable protected @Nullable List<Message> messages
- See Also:
getMessages()
-
-
Constructor Detail
-
MessageContainer
public MessageContainer()
Default constructor which means we do not log added messages - usually it's better to useMessageContainer(Logger)
and log things immediately.
-
MessageContainer
public MessageContainer(@Nullable @Nullable org.slf4j.Logger log)
Constructor that allows to set a logger to whichadd(Message)
automatically writes the messages.- Parameters:
log
- an optional log to which added messages are logged.
-
-
Method Detail
-
getMessages
@NotNull public @NotNull List<Message> getMessages()
A (unmodifiable) snapshot of the list of messages.
-
add
@NotNull public @NotNull MessageContainer add(@Nullable @Nullable Message message, @Nullable @Nullable Throwable throwable)
Adds a message to the container, and logs it into the logger if one was specified for this container. The intended usecase is with a logger, so we log a warning if it's called with a throwable but we have no logger, so that Stacktraces don't disappear accidentially.- Returns:
- this MessageContainer, for builder-style operation chaining.
-
add
@NotNull public @NotNull MessageContainer add(@Nullable @Nullable Message message)
Adds a message to the container, and logs it into the logger if one was specified for this container. Like in SLF4J, if the last argument is a Throwable we use it for logging withadd(Message, Throwable)
.- Returns:
- this MessageContainer, for builder-style operation chaining.
-
addAll
@NotNull public @NotNull MessageContainer addAll(@Nullable @Nullable MessageContainer messageContainer)
Adds all messages of aMessageContainer
to this container.
-
logInto
public void logInto(@Nullable @Nullable org.slf4j.Logger logger)
Logs the messages into the given container. This is automatically done onadd(Message)
if a logger is set, but this might be necessary after deserializing the container.
-
clear
@NotNull public @NotNull MessageContainer clear()
Removes all messages.- Returns:
- this MessageContainer, for builder-style operation chaining.
-
isEmpty
public boolean isEmpty()
Whether there are any messages.
-
hasError
public boolean hasError()
True if a MessageContainer contains a message with anMessage.error(java.lang.String, java.lang.Object...)
.
-
spliterator
public Spliterator<Message> spliterator()
Iterates over a readonly view.- Specified by:
spliterator
in interfaceIterable<Message>
-
setLogger
public void setLogger(org.slf4j.Logger logger)
Modifies the logger newlyadd(Message)
-ed /add(Message, Throwable)
-ed messages are logged to.
-
-