-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
113 lines (92 loc) · 2.4 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package log
import (
"fmt"
"io"
"strings"
"github.com/rs/zerolog"
)
type LogLevel string
const (
DebugLvl LogLevel = "debug"
InfoLvl LogLevel = "info"
ErrorLvl LogLevel = "error"
)
var parseLvl = map[string]LogLevel{
"debug": DebugLvl,
"info": InfoLvl,
"error": ErrorLvl,
}
type Loggeriface interface {
Debugf(format string, args ...any)
Debug(msg string)
Infof(format string, args ...any)
Info(msg string)
Errorf(format string, args ...any)
Error(err error)
}
type Logger struct {
Loggeriface
logger zerolog.Logger
lvl LogLevel
writer io.Writer
}
// New returns a configured instance of ZeroLog
// with minimal methods.
func New(writer io.Writer, lvl LogLevel) Logger {
locallvl, zloglvl := lvl, zerolog.ErrorLevel
zloglvl, err := zerolog.ParseLevel(string(lvl))
if err != nil {
fmt.Printf("loggerFailed: %v. setting error level", err)
locallvl = ErrorLvl
}
zl := zerolog.New(writer).With().Timestamp().CallerWithSkipFrameCount(zerolog.CallerSkipFrameCount + 1).Logger().Level(zloglvl)
return Logger{
logger: zl,
lvl: locallvl,
writer: writer,
}
}
// Writer returns the current writer of the logging instance
func (l Logger) Writer() io.Writer {
return l.writer
}
// Level returns the current logging level of the instance
func (l Logger) Level() LogLevel {
return l.lvl
}
// ParseLevel returns a LogLevel if found by string
// If not found will default to error
// possible values are `error`, `debug`, `info`
func ParseLevel(lvl string) LogLevel {
if flvl, found := parseLvl[strings.ToLower(lvl)]; found {
return flvl
}
return ErrorLvl
}
func (l Logger) Debugf(format string, args ...any) {
l.logger.Debug().Msg(fmt.Sprintf(format, args...))
}
func (l Logger) Debug(msg string) {
l.logger.Debug().Msg(msg)
}
func (l Logger) Infof(format string, args ...any) {
l.logger.Info().Msg(fmt.Sprintf(format, args...))
}
func (l Logger) Info(msg string) {
l.logger.Info().Msg(msg)
}
func (l Logger) Errorf(format string, args ...any) {
l.logger.Error().Err(fmt.Errorf(format, args...)).Msg("")
}
func (l Logger) Error(err error) {
l.logger.Error().Err(err).Msg("")
}
// func lShortMarshall() func(pc uintptr, file string, line int) string {
// return func(pc uintptr, file string, line int) string {
// short := strings.Split(file, "/")
// if len(short) > 1 {
// return fmt.Sprintf("%s:%v", short[len(short)-1], line)
// }
// return fmt.Sprintf("%s:%v", file, line)
// }
// }