-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
Comments
Would it be sufficient to add a method to |
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. |
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? |
/cc @robpike |
@rsc That seems reasonable to me. If there is no concern about "leaking" the writer, this solves my given use-case. |
I like the idea of a |
Writer method returns the underlying io.Writer used by the given Logger object. resolves golang#28327
Writer method returns the underlying io.Writer used by the given Logger object. resolves golang#28327
Change https://golang.org/cl/144757 mentions this issue: |
Writer method returns the underlying io.Writer used by the given Logger object. resolves golang#28327
Writer method returns the underlying io.Writer used by the given Logger object. Fixes golang#28327
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 uselog.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 newLogger
object be created with its ownprefix
andflags
but the originalwriter
. In the previously described example with a Stackdriver logger, the developer could propagate the logger around and create new loggers as necessary:The text was updated successfully, but these errors were encountered: