go-playground / log Goto Github PK
View Code? Open in Web Editor NEW:green_book: Simple, configurable and scalable Structured Logging for Go.
License: MIT License
:green_book: Simple, configurable and scalable Structured Logging for Go.
License: MIT License
What is the recommended way to use one collection of fields to log multiple entries? The obvious way:
func respond(requestID string) {
l := log.WithFields(
log.F("requestID", requestID),
)
l.Info("Everything goes ok")
l.Warn("Something's wrong")
}
But it's completely wrong because of the way the package works -- each time I call WithFields(...)
, I get an Entry from pool, and when I call Info(...)
or Warn(...)
I put Entry back into pool. There's no guarantee that I get the same Entry next time I log with the same "context".
I tried to approach this problem from a different angle by aliasing a slice and implementing LeveledLogger
, so I can mutate and copy fields as I like:
type Fields []log.Field
func (fs Fields) Info(v ...interface{}) {
log.WithFields(fs...).Info(v...)
}
func (fs Fields) WithFields(other ...log.Field) log.LeveledLogger {
return append(fs, other...)
}
...
func respond(requestID string) {
l := Fields{
log.F("requestID", requestID),
}
l.Info("Everything goes ok")
l[:].WithFields(log.F("temperature", 100)).Warn("Something's wrong")
l.Info("Back to normal") // no "temperature" field
}
I wonder if I missed something and there's a simpler solution.
kkp-api | โจ http server started on [::]:1323
kkp-api | 2019/08/07 17:07:57 DEBUG debug
kkp-api | 2019/08/07 17:07:57 INFO info
kkp-api | 2019/08/07 17:07:57 NOTICE notice
kkp-api | 2019/08/07 17:07:57 WARN warn
kkp-api | 2019/08/07 17:07:57 ERROR error
kkp-api | 2019/08/07 17:07:57 ALERT alert
kkp-api | Detail: rsp: created_at:<seconds:-62135596800 > updated_at:<seconds:-62135596800 > deleted_at:<seconds:-62135596800 >
kkp-api | {"time":"2019-08-07T17:07:57.543187+08:00","id":"","remote_ip":"172.23.0.1","host":"localhost:11323","method":"GET","uri":"/user/detail","user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36","status":200,"error":"","latency":121904600,"latency_human":"121.9046ms","bytes_in":0,"bytes_out":110}
kkp-api | 2019/08/07 17:07:57 INFO time to run duration=121.403ms
kkp-api | 2019/08/07 17:08:01 DEBUG debug
kkp-api | 2019/08/07 17:08:01 DEBUG debug
kkp-api | 2019/08/07 17:08:01 INFO info
kkp-api | 2019/08/07 17:08:01 INFO info
kkp-api | 2019/08/07 17:08:01 NOTICE notice
kkp-api | 2019/08/07 17:08:01 NOTICE notice
kkp-api | 2019/08/07 17:08:01 WARN warn
kkp-api | 2019/08/07 17:08:01 WARN warn
kkp-api | 2019/08/07 17:08:01 ERROR error
kkp-api | 2019/08/07 17:08:01 ERROR error
kkp-api | 2019/08/07 17:08:01 ALERT alert
kkp-api | 2019/08/07 17:08:01 ALERT alert
kkp-api | Detail: rsp: created_at:<seconds:-62135596800 > updated_at:<seconds:-62135596800 > deleted_at:<seconds:-62135596800 >
kkp-api | {"time":"2019-08-07T17:08:01.2394095+08:00","id":"","remote_ip":"172.23.0.1","host":"localhost:11323","method":"GET","uri":"/user/detail","user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36","status":200,"error":"","latency":3925600,"latency_human":"3.9256ms","bytes_in":0,"bytes_out":110}
kkp-api | 2019/08/07 17:08:01 INFO time to run duration=3.8317ms
kkp-api | 2019/08/07 17:08:01 INFO time to run duration=3.8317ms
kkp-api | 2019/08/07 17:08:12 DEBUG debug
kkp-api | 2019/08/07 17:08:12 DEBUG debug
kkp-api | 2019/08/07 17:08:12 DEBUG debug
kkp-api | 2019/08/07 17:08:12 INFO info
kkp-api | 2019/08/07 17:08:12 INFO info
kkp-api | 2019/08/07 17:08:12 INFO info
kkp-api | 2019/08/07 17:08:12 NOTICE notice
kkp-api | 2019/08/07 17:08:12 NOTICE notice
kkp-api | 2019/08/07 17:08:12 NOTICE notice
kkp-api | 2019/08/07 17:08:12 WARN warn
kkp-api | 2019/08/07 17:08:12 WARN warn
kkp-api | 2019/08/07 17:08:12 WARN warn
kkp-api | 2019/08/07 17:08:12 ERROR error
kkp-api | 2019/08/07 17:08:12 ERROR error
kkp-api | 2019/08/07 17:08:12 ERROR error
kkp-api | 2019/08/07 17:08:12 ALERT alert
kkp-api | 2019/08/07 17:08:12 ALERT alert
kkp-api | 2019/08/07 17:08:12 ALERT alert
kkp-api | Detail: rsp: created_at:<seconds:-62135596800 > updated_at:<seconds:-62135596800 > deleted_at:<seconds:-62135596800 >
kkp-api | {"time":"2019-08-07T17:08:12.7423301+08:00","id":"","remote_ip":"172.23.0.1","host":"localhost:11323","method":"GET","uri":"/user/detail","user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36","status":200,"error":"","latency":2952500,"latency_human":"2.9525ms","bytes_in":0,"bytes_out":110}
kkp-api | 2019/08/07 17:08:12 INFO time to run duration=2.7394ms
kkp-api | 2019/08/07 17:08:12 INFO time to run duration=2.7394ms
kkp-api | 2019/08/07 17:08:12 INFO time to run duration=2.7394ms
cLog := console.New(true)
cLog.SetTimestampFormat("2006/01/02 15:04:05")
log.AddHandler(cLog, log.AllLevels...)
// Trace
defer log.WithTrace().Info("time to run")
log.Debug("debug")
log.Info("info")
log.Notice("notice")
log.Warn("warn")
log.Error("error")
// log.Panic("panic") // this will panic
log.Alert("alert")
// log.Fatal("fatal") // this will call os.Exit(1)
Hi there!
There is any particular reason to force UTC timestamp?
Regards,
Sebastian
Hello All,
I could definitely use some help adding even more log Handlers for other services such as Graylog, Email, InfluxDB, ElasticSearch... list goes on.
Thanks in Advance for anyone whom can help! ๐
Add configurable skip level for file + line number; this allows for library to be wrapped by another package + still report accurately.
Being able to disable or change the log levels for a handler is important for projects which may rely on toggle-able features.
Related: bakape/meguca#730
package main
import (
"bytes"
"fmt"
"github.com/go-playground/log/v8"
)
type CustomHandler struct {
}
func (c *CustomHandler) Log(e log.Entry) {
b := new(bytes.Buffer)
b.Reset()
b.WriteString(e.Message)
fmt.Println("-------------------- " + b.String())
}
func main() {
log.RedirectGoStdLog(true)
cLog := new(CustomHandler)
log.AddHandler(cLog, log.AllLevels...)
log.Debug("debug")
log.Info("info")
log.Notice("notice")
log.Warn("warn")
log.Error("error")
log.Alert("alert")
}
2023-06-20T16:42:19.821153700+08:00 DEBUG debug
-------------------- debug
2023-06-20T16:42:19.827476000+08:00 INFO info
-------------------- info
2023-06-20T16:42:19.827989800+08:00 NOTICE notice
-------------------- notice
2023-06-20T16:42:19.829013000+08:00 WARN warn
-------------------- warn
2023-06-20T16:42:19.829522600+08:00 ERROR error
-------------------- error
2023-06-20T16:42:19.829522600+08:00 ALERT alert
-------------------- alert
how to show
-------------------- debug
-------------------- info
-------------------- notice
-------------------- warn
-------------------- error
-------------------- alert
Though SetFormatFunc
method is exported in every handler, users actually can do nothing with this. Since none of the handlers' struct have any exported field, users can't customize their own format function.
Either making some fields exported or adding some getters to make this method usable. What do you think about it?
package main
import (
"io"
"os"
"github.com/go-playground/log"
"github.com/go-playground/log/handlers/json"
"github.com/go-playground/errors"
)
func main() {
log.AddHandler(json.New(os.Stderr), log.AllLevels...)
err1 := errors.Wrap(io.EOF, "prefix 1")
err2 := errors.Wrap(err1, "prefix 2")
log.WithError(err2).Error("test")
}
prints out
{
"message": "test",
"timestamp": "2018-04-04T17:54:51.338592537-07:00",
"fields": [
{
"key": "error",
"value": "prefix 1: EOF"
},
{
"key": "source",
"value": "main: /Users/andrey/go/sandbox/src/sandbox/main.go:13"
}
],
"level": "ERROR"
}
I was expecting the value of the "error" field to be prefix 2: prefix 1: EOF
it seems there is a problem with the Go playground- when I try to run this from the Golang tutorial https://go.dev/tour/flowcontrol/12 I get an error ("can't connect to server) but when I copy and paste the code to another playground window, it works. Also obviously the tutorial /playground environment is being changed by the development team - when will it be stable?
Using this file:
package main
import "github.com/go-playground/log"
func main() {
log.Warn("aaaaa bbbbb")
}
I get this result:
2019-05-21 06:52:06.780406600-05:00 [33;1m WARN [0m aaaaa bbbbb
Essentially, the go.mod
definition is malformed and causes projects attempting to consume the module by importing the latest version using import "github.com/go-playground/log/v7"
. Also, the sub-modules need to be updated to use the proper import path. Otherwise they will be importing the v6.3.0
version of the module.
Hi there,
there is possible group log entries? Im trying to print multiple log entries for a unique action, eg:
INFO running command 1
INFO output is bla
INFO running command 2
INFO output is bla
but when I use the same logic in multiple goroutines, become insane:
INFO running command 1
INFO running command 1
INFO running command 1
INFO output is bla
INFO running command 1
INFO running command 1
INFO output is bla
INFO output is bla
ERROR error for command is : foo bar
INFO output is bla
INFO running command 2
INFO running command 2
INFO output is bla
INFO output is bla
INFO running command 2
INFO output is bla
Any ideia about this issue?
Thanks
For projects with toggle-able features, being able to stop the email handler from sending emails is important.
Related: bakape/meguca#730
addHandlers
just keeps the handler there forever with no way to remove it or change it.
removeHandlers
would be a great method for this.
Consider servers where you can change the error email live, the only thing you can do right now is change and restart the program.
$ go get -v github.com/go-playground/log
github.com/go-playground/log
# github.com/go-playground/log
C:\Users\Steven\go\src\github.com\go-playground\log\log.go:24:24: cannot use
syscall.Stdout (type syscall.Handle) as type int in argument to
terminal.IsTerminal
For example:
log.Info("Hello")
which output
2016-04-29 19:48:00 INFO main.go:85 Hello
First of all, thanks for your work. This library is so awesome!
Is there anyway to define log time? e.g. use Local Time instead of UTC
Gather file + line info on all error type logs such as log.Error(..), log.Fatal(...) etc..
This information is worth the overhead to know exactly what went wrong where.
but synchronization of the change is still the user's responsibility
Sorry to make another issue, but I just noticed that, and I'm a bit new to Go in general.
How would I synchronize the changes in the email handler (or log itself?), for instance?
I can't seem to find any way.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.