decorate method

String decorate(
  1. String message,
  2. LogRecordEntity record
)

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:

  1. It starts with the message parameter as the initial value.
  2. It uses the fold method on the decoration list to iterate over each LogDecorator function.
  3. In each iteration, the current LogDecorator function is invoked with the previousValue (initially set to the message) and the record.
  4. The result of each decorator invocation becomes the previousValue for the next iteration.
  5. 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;
}