Simply configuring zerolog with output to console, lumberjack.v2, sentry-go, http.
- Easy use
- Error Logging with stacktrace
- Contextual Logging to
Payload
sentry context - Set log level for each outputs
package main
import (
"github.com/mef13/zslog"
"time"
)
func main() {
fileConf := zslog.FileConfig{
MaxSize: 1,
MaxBackups: 10,
}
zslog.InitLogger(
zslog.StdOut(zslog.NewLevels().SetMaxLevel(zslog.WarnLevel)),
zslog.StdErr(zslog.NewLevels().SetMinLevel(zslog.ErrorLevel)),
zslog.File("/var/log/myapp/err.log", fileConf, zslog.NewLevels().SetMinLevel(zslog.ErrorLevel)),
zslog.Sentry(zslog.SentryConfig{
Dsn: "https://[email protected]/1",
Release: "0.0",
}, 3*time.Second, zslog.NewLevels().SetMinLevel(zslog.ErrorLevel)))
//Flush sentry buffer before exit
defer zslog.Close()
zslog.Info().Msg("hello world")
}
Set specific levels:
zslog.StdOut(zslog.NewLevels(zslog.ErrorLevel, zslog.WarnLevel))
or set maximum(minimum) level:
zslog.StdOut(zslog.NewLevels().SetMaxLevel(zslog.WarnLevel))
For add stacktrace use Stack()
function:
package main
import (
"github.com/mef13/zslog"
"github.com/pkg/errors"
"time"
)
func main() {
zslog.InitLogger(
zslog.Sentry(zslog.SentryConfig{
Dsn: "https://[email protected]/1",
Release: "0.0",
}, 3*time.Second, zslog.NewLevels().SetMinLevel(zslog.ErrorLevel)))
//Flush sentry buffer before exit
defer zslog.Close()
err = outer()
zslog.Error().Stack().Err(err).Msg("outer error")
}
func inner() error {
return errors.New("Ah s*, here we go again")
}
func middle() error {
err := inner()
if err != nil {
return err
}
return nil
}
func outer() error {
err := middle()
if err != nil {
return err
}
return nil
}