Skip to content

Commit

Permalink
Transitland wrapper for zerolog
Browse files Browse the repository at this point in the history
  • Loading branch information
irees committed Dec 11, 2023
1 parent f19ac14 commit 8bdc406
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# log
# log

Transitland wrapper for zerolog.
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/interline-io/log

go 1.20

require github.com/rs/zerolog v1.31.0

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/sys v0.12.0 // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
117 changes: 117 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package log

import (
"fmt"
"os"
"strings"
"time"

"github.com/rs/zerolog"
)

// Zerolog

var Logger = zerolog.New(os.Stderr).With().Timestamp().Logger()

func Fatal() *zerolog.Event {
return Logger.Info()
}

func Info() *zerolog.Event {
return Logger.Info()
}

func Error() *zerolog.Event {
return Logger.Error()
}

func Debug() *zerolog.Event {
return Logger.Debug()
}

func Trace() *zerolog.Event {
return Logger.Trace()
}

func With() zerolog.Context {
return Logger.With()
}

// Zerolog simple wrappers

// Error for notable errors.
func Errorf(fmts string, a ...interface{}) {
Logger.Error().Msgf(fmts, a...)
}

// Info for regular messages.
func Infof(fmts string, a ...interface{}) {
Logger.Info().Msgf(fmts, a...)
}

// Debug for debugging messages.
func Debugf(fmts string, a ...interface{}) {
Logger.Debug().Msgf(fmts, a...)
}

// Trace for debugging messages.
func Tracef(fmts string, a ...interface{}) {
Logger.Trace().Msgf(fmts, a...)
}

// Traceln - prints to trace
func Traceln(args ...interface{}) {
Logger.Trace().Msg(fmt.Sprintln(args...))
}

// Helper functions

// Print - simple print, without timestamp, without regard to log level.
func Print(fmts string, args ...interface{}) {
fmt.Fprintf(os.Stdout, fmts+"\n", args...)
}

// Log init and settings

// SetLevel sets the log level.
func SetLevel(lvalue zerolog.Level) {
zerolog.SetGlobalLevel(lvalue)
Infof("Set global log value to %s", lvalue)
}

// setLevelByName sets the log level by string name.
func setLevelByName(lstr string) {
switch strings.ToUpper(lstr) {
case "FATAL":
SetLevel(zerolog.FatalLevel)
case "ERROR":
SetLevel(zerolog.ErrorLevel)
case "INFO":
SetLevel(zerolog.InfoLevel)
case "DEBUG":
SetLevel(zerolog.DebugLevel)
case "TRACE":
SetLevel(zerolog.TraceLevel)
}
}

func setConsoleLogger() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
output.FormatLevel = func(i interface{}) string {
return strings.ToUpper(fmt.Sprintf("[%-5s]", i))
}
Logger = zerolog.New(os.Stderr).With().Timestamp().Logger().Output(output).Level(zerolog.TraceLevel)
}

func init() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if os.Getenv("TL_LOG_JSON") == "true" {
// use json logging
} else {
setConsoleLogger()
}
if v := os.Getenv("TL_LOG"); v != "" {
setLevelByName(v)
}
}

0 comments on commit 8bdc406

Please sign in to comment.