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

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

Closed
ccpaging opened this issue Oct 30, 2021 · 6 comments
Closed

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

ccpaging opened this issue Oct 30, 2021 · 6 comments

Comments

@ccpaging
Copy link

ccpaging commented Oct 30, 2021

Some code I am working with take level string:

  • Add flag : Llevel
  • Add function OutputL:
    OutputL(calldepth int, level, s string) error
    outputl(calldepth int, level, s string) error

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
Copy link

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

@seankhliao
Copy link
Member

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
Copy link
Author

ccpaging commented Oct 30, 2021

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
Copy link
Author

ccpaging commented Oct 30, 2021

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
Copy link
Contributor

See relevant #48503

@seankhliao
Copy link
Member

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.

@golang golang locked and limited conversation to collaborators Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants