decorate method
The decorate method is a method that receives a non-decorated log message
as message
and a LogRecordEntity object as record.
It applies the decoration logic using the list of LogDecorator
functions specified in the decoration property.
Here's how the decorate method works:
- It starts with the
message
parameter as the initial value. - It uses the fold method on the decoration list to iterate over each LogDecorator function.
- In each iteration, the current LogDecorator function is invoked with the previousValue (initially set to the message) and the record.
- The result of each decorator invocation becomes the previousValue for the next iteration.
- Finally, the decorated message is returned as the output of the decorate method. This approach allows for a sequential application of decorators, where each decorator modifies the log message based on the previous decorator's output. The end result is a decorated message that has been processed by all the decorators in the decoration list.
Implementing the decorate method in this way provides a convenient and flexible mechanism to decorate log messages before they are recorded by the log recorder's main method.
Implementation
String decorate(String message, LogRecordEntity record) {
final output = decoration.fold(
message,
(previousValue, element) => element(previousValue, record),
);
return output;
}