Closed ccpaging closed 2 years ago
Change https://golang.org/cl/359994 mentions this issue: log: add Llevel flag and OutputL() function
Isn't this already possible with SetPrefix? I don't think you've sufficiently explained why this should be in the standard library and not just part of an external package
Isn't this already possible with SetPrefix? I don't think you've sufficiently explained why this should be in the standard library and not just part of an external package
Thanks for reply. I stronglly agree the point is which part should be in the std lib, and which is just part of external package.
SetPrefix is possible. I have try it in my project mlog, mlog.go. It needs four *log.Logger and a locker, for Debug, Warn, Info, Error. For example:
// A Logger represents an active logging object that generates lines of
// output to an io.Writer. Each logging operation makes a single call to
// the Writer's Write method. A Logger can be used simultaneously from
// multiple goroutines; it guarantees to serialize access to the Writer.
type Logger struct {
mu sync.Mutex // ensures atomic writes; protects the following fields
*stdlog.Logger
level int
lmap map[int]*stdlog.Logger
}
There is an other experiment, syslog.
syslog is heavy level depend logger.
If std log supplies OutputL(), syslog can be easy divided into level interface functions(Emerg, Alert, Crit, Err...) and a network connective Writer.
It is easy to keep same level interface functions, and replace with another Writer, for example, ansi color console, roolling file, NATS etc. Or using MultiWriter outputs log messages simultaneously.
See relevant https://github.com/golang/go/issues/48503
OutputL(n,l, s) doesn't really provide anything over Output(n, l +s) We've declined leveled logging before, and the usecases are covered by existing functionality such as #28327 #32062
if you want full control over the output you could always use fmt directly.
Some code I am working with take level string:
I have worked on log4go and nxlog4go for few years. The 3rd log alway face three problem:
After compare few solutions, I found:
The level type should be string. It is better than int. Do not implement too much interface functions in std log. Let the 3rd log do it. Do not implement level filter in std log. Let the 3rd log do it.
New OutputL function receives the level string and writes it to the beginning of the log line.