golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.75k stars 17.64k forks source link

log: add Llevel flag and OutputL() function #49239

Closed ccpaging closed 2 years ago

ccpaging commented 2 years ago

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:

  1. The interface function. More in stdlog, more need to be modified.
  2. Override Output function and replace with 3rd formatter.
  3. Building and using own Writer easy.

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.

gopherbot commented 2 years ago

Change https://golang.org/cl/359994 mentions this issue: log: add Llevel flag and OutputL() function

seankhliao commented 2 years ago

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

ccpaging commented 2 years ago

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
}
ccpaging commented 2 years ago

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.

AlexanderYastrebov commented 2 years ago

See relevant https://github.com/golang/go/issues/48503

seankhliao commented 2 years ago

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.