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: modify Logger struct, make it supports multi output #33454

Closed
kerbalwzy opened this issue Aug 4, 2019 · 4 comments
Closed

proposal: log: modify Logger struct, make it supports multi output #33454

kerbalwzy opened this issue Aug 4, 2019 · 4 comments

Comments

@kerbalwzy
Copy link

kerbalwzy commented Aug 4, 2019

What version of Go are you using (go version)?

$ go version
go1.12.1 

Does this issue reproduce with the latest release?

No, but this part of the source code is no different from the latest version.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN="/usr/local/Cellar/go/1.12.1/libexec//bin"
GOCACHE="/Users/wzy/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/wzy/GoPackages/"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.1/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/fd/ydjl7hd15j12f1hlymsj1rkm0000gp/T/go-build588650608=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I Modified the field out of the structure Logger in package log, reset it to outs, which type is []io.Writer, and the relevant places involving the operation outs.

The goal is to support multiple outputs of log information, such as simultaneous output to files and consoles, and more.

These changes do not affect the code execution of other completed projects that call the log package

PR_FilesChanged: #33450

What did you expect to see?

I want to set the output of log information to multiple oi.Writer at the same time through the built-in function of log package onveniently and quickly. Because in large projects nowadays, it is usually necessary to save logs in many format to many places, to do some data analysis of service operation by the log informations, etc.

What did you see instead?

At present, the only way to output log information to multiple places at the same time is to create multiple logger and then process the same log information by each logger.

@gopherbot gopherbot added this to the Proposal milestone Aug 4, 2019
@tmthrgd
Copy link
Contributor

tmthrgd commented Aug 4, 2019

The change you've proposed isn't at all backwards compatible and it's also unnecessary. You can achieve exactly this using io.MutliWriter. Take a look at this example: https://play.golang.org/p/F3p-Vt1CJbx.

@kerbalwzy
Copy link
Author

backwards compatible

I may have found out what caused incompatibility, and I'll fix it.
I saw the source code of io.MutliWriter. It's useful. But it doesn't feel so direct and simple to use here.
Thank you.

@mvdan
Copy link
Member

mvdan commented Aug 6, 2019

This is still a backwards-incompatible change; SetOutput used to replace the writer, while in your CL above it appends.

I agree with @tmthrgd that you should simply use an io.MultiWriter here. Having one way to achieve something with less API is simpler than having many ways to do the same.

@rsc
Copy link
Contributor

rsc commented Aug 6, 2019

As others have said, this is a backwards incompatible change, which makes it not something we can accept. Most people who use I/O in Go for a while end up finding io.MultiWriter to be direct, simple, and natural. I would suggest using it and finding out.

Note that you can build up the "add a new output to an existing logger" pattern for yourself by using

func AppendOutput(l *log.Logger, w io.Writer) {
    l.SetOutput(io.MultiWriter(l.Output(), w))
}

If that's something you do all the time, you could put it in your own library.

Closing because it is a backwards-incompatible change with an easy implementation outside the standard library.

@rsc rsc closed this as completed Aug 6, 2019
@golang golang locked and limited conversation to collaborators Aug 5, 2020
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