-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
72 lines (59 loc) · 1.55 KB
/
logger.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
package logger
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const (
callerFieldName = "trace"
environmentProd = "prod"
)
type Logger struct {
env string
app string
version string
logger zerolog.Logger
service string
}
func New(environment, app, version string) *Logger {
l := &Logger{
env: environment,
app: app,
version: version,
}
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if environment != environmentProd {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
zerolog.CallerFieldName = callerFieldName
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMicro
l.logger = log.With().
Str("app", l.app).
Str("version", l.version).
Str("environment", l.env).
Logger()
return l
}
func (l *Logger) Debug(Message string, Context ...string) {
l.logger.Debug().Str("service", l.service).Strs("context", Context).Msg(Message)
}
func (l *Logger) Info(Message string, Context ...string) {
l.logger.Info().
Str("service", l.service).Strs("context", Context).Msg(Message)
}
func (l *Logger) Warning(Message string, Context ...string) {
l.logger.Warn().
Str("service", l.service).Strs("context", Context).Msg(Message)
}
func (l *Logger) Error(Error error, Context ...string) {
l.logger.Error().Str("service", l.service).Strs(
"context", Context).
Caller(1).Msg(Error.Error())
}
func (l *Logger) Critical(Error error, Context ...string) {
l.logger.Fatal().Str("service", l.service).Strs(
"context", Context).
Caller(1).Msg(Error.Error())
}
func (l *Logger) SetLogService(service string) {
l.service = service
}