Skip to content

proposal: log: Logger should be able to create "child" logger #28327

Closed
@poy

Description

@poy

Problem

Creating a new logger is a useful pattern for using different prefixes to separate logs within a process (e.g., gRPC vs user-code).

If the output is always known (e.g., os.Stderr), this is not an issue as the developer could always use log.New(os.Stderr, "[NEW-PREFIX] ", log.LstdFlags).

However, the problem arises when instead of a known/generic value like os.Stderr, the developer wants the logger to derive from something like a Stackdriver logger.

Proposed Solution

Add a method to the Logger struct to allow a new Logger object be created with its own prefix and flags but the original writer. In the previously described example with a Stackdriver logger, the developer could propagate the logger around and create new loggers as necessary:

func Foo(ctx context.Context, param int, logger *log.Logger) {
  logger.Println("In Foo")
  Bar(ctx, logger.FromWriter("[BAR] ", log.LstdFlags))
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions