2024-09-17 01:01:42 +00:00
|
|
|
package gologger
|
|
|
|
|
|
|
|
import (
|
2024-09-24 00:34:53 +00:00
|
|
|
"fmt"
|
2024-09-17 01:01:42 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2024-09-24 00:34:53 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2024-09-17 01:01:42 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
|
|
|
var once sync.Once
|
|
|
|
|
|
|
|
var log zerolog.Logger
|
|
|
|
|
|
|
|
func Get(l string) zerolog.Logger {
|
|
|
|
once.Do(func() {
|
|
|
|
zerolog.TimeFieldFormat = time.RFC3339Nano
|
|
|
|
|
|
|
|
var output io.Writer = zerolog.ConsoleWriter{
|
|
|
|
Out: os.Stdout,
|
|
|
|
TimeFormat: time.RFC3339,
|
2024-09-24 00:34:53 +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
|
|
|
|
},
|
2024-09-17 01:01:42 +00:00
|
|
|
}
|
|
|
|
level, err := zerolog.ParseLevel(l)
|
|
|
|
if err != nil {
|
|
|
|
panic("unable to get parse level for logger")
|
|
|
|
}
|
|
|
|
|
|
|
|
log = zerolog.New(output).
|
|
|
|
Level(zerolog.Level(level)).
|
|
|
|
With().
|
|
|
|
Timestamp().
|
|
|
|
Logger()
|
|
|
|
})
|
|
|
|
|
|
|
|
return log
|
|
|
|
}
|