Go: proposal: log: modify `Logger` struct, make it supports multi output

Created on 4 Aug 2019  ·  4Comments  ·  Source: golang/go

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.

FrozenDueToAge Proposal

Most helpful comment

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.

All 4 comments

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.

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.

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.

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.

Was this page helpful?
0 / 5 - 0 ratings