CachedLogger

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

Implements: 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) 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

Fields

Cache

List containing log messages.

Cache: []*LogMessage

Updated

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

Updated: bool

Interval

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

Interval: int

LastDumpTime

Time of the last dump

LastDumpTime: time.Time

MaxCacheSize

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

MaxCacheSize: int

Methods

Clear

Clears (removes) all cached log messages.

(c *CachedLogger) Clear(ctx context.Context)

  • ctx: context.Context - operation context.

Configure

Configures a component by passing configuration parameters.

(c *CachedLogger) Configure(ctx context.Context, cfg *config.ConfigParams

  • ctx: context.Context - operation context.
  • cfg: *config.ConfigParams - configuration parameters to be set.

Dump

Dumps (writes) the currently cached log messages.

(c *CachedLogger) Dump(ctx context.Context) error

  • ctx: context.Context - operation context.
  • returns: error - returned error if not dumped.

Update

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

(c *CachedLogger) Update(ctx context.Context)

  • ctx: context.Context - operation context.

Save

Saves log messages from the cache.

Save(messages []*LogMessage) error

  • messages: []*LogMessage - list with log messages.
  • returns: error - returned error if not saved.

Write

Writes a log message to the logger destination.

func (c *CachedLogger) Write(ctx context.Context, level LevelType, context IContext, err error, message string)

  • ctx: context.Context - operation context.
  • level: LevelType - a log level.
  • context: IContext - (optional) a context to trace execution through a call chain.
  • ex: error - an error object associated with this message.
  • message: string - a human-readable message to log.

See also