Skip to content

log: Provide a simple concurrent safe Logger wrapper. #246

@ChrisHines

Description

@ChrisHines

Proposal

Add a new exported function to Go-kit/log.

// NewMutexLogger returns a logger that uses a sync.Mutex to synchronize
// concurrent use of the wrapped logger.
func NewMutexLogger(logger Logger) Logger

Motivation

Go-kit/log should not require external code to use safely in a concurrent manner.

Status Quo

Go-kit/log carefully documents that Logger implementations must be safe for concurrent use. NewLogfmtLogger and NewJSONLogger document that they require an io.Writer safe for concurrent use if the logger will be used concurrently. That is to say that the Logfmt and JSON loggers are safe, but their side effect of writing data may not be safe. Unfortunately many implementations of io.Writer are not safe for concurrent use.

Alternatives

  • A mutex based concurrent safe io.Writer, or a
  • A channel based asynchronous Logger.

A MutexWriter allows the log event formatting to occur concurrently outside the scope of the mutex. A channel based AsyncLogger offloads log event formatting to a single background goroutine and also guarantees concurrent safe writing of the formatted data without additional locking, but it also complicates error handling and application shutdown.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions