gologger/gologger.go

47 lines
807 B
Go
Raw Permalink Normal View History

2023-08-03 17:45:40 +00:00
package gologger
import (
2024-09-24 00:33:18 +00:00
"fmt"
2023-08-03 17:45:40 +00:00
"io"
"os"
2024-09-24 00:33:18 +00:00
"runtime"
"strings"
2023-08-03 17:45:40 +00:00
"sync"
"time"
"github.com/rs/zerolog"
)
var once sync.Once
2023-08-03 21:26:17 +00:00
var log zerolog.Logger
2023-08-03 21:33:34 +00:00
func Get(l string) zerolog.Logger {
2023-08-03 17:45:40 +00:00
once.Do(func() {
zerolog.TimeFieldFormat = time.RFC3339Nano
var output io.Writer = zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC3339,
2024-09-24 00:33:18 +00:00
FormatCaller: func(i interface{}) string {
_, vpath, _, _ := runtime.Caller(0)
bpath, _, _ := strings.Cut(vpath, "vendor/")
_, ret, _ := strings.Cut(fmt.Sprintf("%s", i), bpath)
return ret
},
2023-08-03 17:45:40 +00:00
}
2023-08-03 19:52:57 +00:00
level, err := zerolog.ParseLevel(l)
if err != nil {
panic("unable to get parse level for logger")
}
2023-08-03 17:45:40 +00:00
2023-08-03 21:26:17 +00:00
log = zerolog.New(output).
2023-08-03 19:50:55 +00:00
Level(zerolog.Level(level)).
2023-08-03 17:45:40 +00:00
With().
Timestamp().
Logger()
})
return log
}