Various functions that I use in projects
Using
- Zap logger by Uber, https://github.com/uber-go/zap
- Lumberjack https://pkg.go.dev/gopkg.in/natefinch/lumberjack.v2
Levels
- Debug
- Info
- Warn
- Error
- DPanic
- Panic
Init
var lg *zap.SugaredLogger
func init() {
lg = log.InitWithConsole(zapcore.DebugLevel)
}
Set level
lg = log.SetDebugLevel()
lg = log.SetInfoLevel()
etc...
Log
- Simple:
log.Info("hello")
- Formatted:
log.Infof("hello %s", "world")
- Structured:
log.Infow("hello", "var1", 12, "var2", 24)
time.StampTimeNow()
returns time in RFC3339Nano
ex: 2023-04-27T11:33:45.772006+02:00
Get local IP
network.GetIP()
Only tried on simple structs
Pretty print a struct
pp.PP(someStruct)
Return a pretty struct
struct, err := pp.PrettyStruct(someStruct)
uuid.GenerateUUID()
Get a UUID using KSUID, https://github.com/segmentio/ksuid
Why this one?
Because someone said it was good, see https://blog.kowalczyk.info/article/JyRZ/generating-good-unique-ids-in-go.html