Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
poy opened this issue Oct 22, 2018 · 7 comments
Closed

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

poy opened this issue Oct 22, 2018 · 7 comments

Comments

@poy
Copy link
Contributor

poy commented Oct 22, 2018

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))
}
@poy poy changed the title log: Proposal for Logger should be able to create "child" logger log: Proposal - Logger should be able to create "child" logger Oct 22, 2018
@ianlancetaylor ianlancetaylor changed the title log: Proposal - Logger should be able to create "child" logger proposal: log: Logger should be able to create "child" logger Oct 23, 2018
@gopherbot gopherbot added this to the Proposal milestone Oct 23, 2018
@ianlancetaylor
Copy link
Contributor

Would it be sufficient to add a method to Logger that returns the out field?

@poy
Copy link
Contributor Author

poy commented Oct 23, 2018

That would work, however I wonder if there is any reason why it was not exported in the first place. I'm not sure if the intent was to prevent someone from writing to it directly.

@rsc
Copy link
Contributor

rsc commented Oct 24, 2018

Logger has SetFlags, SetPrefix, and SetOutput, and it has Flags and Prefix to get the flags and prefix back. Unfortunately Output does not return the writer passed to SetOutput - it is more like a Write method. But if we added a method to return the writer, it seems like that would be enough, as Ian said.

It would be nicer from an API perspective to just expose the writer and let you do what you need, instead of trying to define exactly what a "child logger" is and add that new complexity here.

We could add GetOutput but in general we avoid GetFoo names. Maybe just a new Writer() io.Writer method?

@rsc
Copy link
Contributor

rsc commented Oct 24, 2018

/cc @robpike

@poy
Copy link
Contributor Author

poy commented Oct 24, 2018

@rsc That seems reasonable to me. If there is no concern about "leaking" the writer, this solves my given use-case.

@robpike
Copy link
Contributor

robpike commented Oct 25, 2018

I like the idea of a Writer() io.Writer method.

poy pushed a commit to poy/go that referenced this issue Oct 25, 2018
Writer method returns the underlying io.Writer used by the given Logger
object.

resolves golang#28327
poy pushed a commit to poy/go that referenced this issue Oct 25, 2018
Writer method returns the underlying io.Writer used by the given Logger
object.

resolves golang#28327
@gopherbot
Copy link
Contributor

Change https://golang.org/cl/144757 mentions this issue: log: adds Writer method to Logger

poy pushed a commit to poy/go that referenced this issue Oct 25, 2018
Writer method returns the underlying io.Writer used by the given Logger
object.

resolves golang#28327
poy pushed a commit to poy/go that referenced this issue Oct 25, 2018
Writer method returns the underlying io.Writer used by the given Logger
object.

Fixes golang#28327
@golang golang locked and limited conversation to collaborators Oct 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants