CachedLogger

Logger that caches captured log messages in memory and periodically dumps them.

Extends: Logger

Description

The CachedLogger class allows you to create a logger that caches captured log messages in memory and periodically dumps them.

Important points

  • Child classes implement saving cached messages to their specified destinations.

Configuration parameters

  • level: maximum log level to capture
  • source: source (context)’s name
  • options:
    • interval: interval in milliseconds to save log messages (default: 10 seconds)
    • max_cache_size: maximum number of messages stored in this cache (default: 100)

References

  • *:context-info:*:*:1.0 - (optional) ContextInfo to detect the context id and specify counters source

Constructors

Creates a new instance of the logger.

CachedLogger()

Fields

cache

List containing log messages.

cache: List<LogMessage>

updated

Boolean variable indicating whether there has been an update or not.

updated: bool = false

interval

Interval in milliseconds to save log messages (default: 10 seconds)

interval: int = 10000

lastDumpTime

Time of the last dump.

lastDumpTime: int

maxCacheSize

Maximum number of messages stored in the cache (default: 100).

maxCacheSize: int = 100

Instance methods

clear

Clears (removes) all cached log messages.

void clear()

configure

Configures a component by passing its configuration parameters.

@override

void configure(ConfigParams config)

  • config: ConfigParams - configuration parameters to be set.

dump

Dumps (writes) the currently cached log messages.

void dump()

update

Sets message cache as updated and dumps it when timeout expires.

void update()

save

Saves log messages from the cache.

Future save(List<LogMessage> messages)

  • messages: List<LogMessage> - list with log messages

write

Writes a log message to the logger destination.

@override

void write(LogLevel level, IContext? context, Exception? error, String message)

  • level: LogLevel - a log level.
  • context: IContext - (optional) a context to trace execution through a call chain.
  • error: Exception? - an error object associated with this message.
  • message: String - a human-readable message to log.

See also