-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.go
142 lines (118 loc) · 3.75 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package common_datalayer
import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/DataDog/datadog-go/v5/statsd"
)
/******************************************************************************/
type Metrics interface {
Incr(s string, tags []string, i int) LayerError
Timing(s string, timed time.Duration, tags []string, i int) LayerError
Gauge(s string, f float64, tags []string, i int) LayerError
}
type Logger interface {
Error(message string, args ...any)
Info(message string, args ...any)
Debug(message string, args ...any)
Warn(message string, args ...any)
With(name string, value string) Logger
}
/******************************************************************************/
type StatsdMetrics struct {
client statsd.ClientInterface
}
func (sm StatsdMetrics) Incr(name string, tags []string, rate int) LayerError {
return Err(sm.client.Incr(name, tags, float64(rate)), LayerErrorInternal)
}
func (sm StatsdMetrics) Timing(name string, value time.Duration, tags []string, rate int) LayerError {
return Err(sm.client.Timing(name, value, tags, float64(rate)), LayerErrorInternal)
}
func (sm StatsdMetrics) Gauge(name string, value float64, tags []string, rate int) LayerError {
return Err(sm.client.Gauge(name, value, tags, float64(rate)), LayerErrorInternal)
}
func newMetrics(conf *Config) (Metrics, error) {
var client statsd.ClientInterface
if conf.LayerServiceConfig.StatsdEnabled {
c, err := statsd.New(conf.LayerServiceConfig.StatsdAgentAddress,
statsd.WithNamespace(conf.LayerServiceConfig.ServiceName),
statsd.WithTags([]string{"application:" + conf.LayerServiceConfig.ServiceName}))
if err != nil {
return nil, err
}
client = c
} else {
client = &statsd.NoOpClient{}
}
return &StatsdMetrics{client: client}, nil
}
type logger struct {
log zerolog.Logger
}
func (l *logger) With(name string, value string) Logger {
subLogger := l.log.With().Str(name, value).Logger()
return &logger{subLogger}
}
func (l *logger) Warn(message string, args ...any) {
l.log.Warn().Fields(args).Msg(message)
}
func (l *logger) Error(message string, args ...any) {
l.log.Error().Fields(args).Msg(message)
}
func (l *logger) Info(message string, args ...any) {
l.log.Info().Fields(args).Msg(message)
}
func (l *logger) Debug(message string, args ...any) {
l.log.Debug().Fields(args).Msg(message)
}
func NewLogger(serviceName string, format string, level string) Logger {
var slevel zerolog.Level
switch strings.ToLower(level) {
case "debug":
slevel = zerolog.DebugLevel
case "info":
slevel = zerolog.InfoLevel
case "warn":
slevel = zerolog.WarnLevel
case "error":
slevel = zerolog.ErrorLevel
default:
slevel = zerolog.InfoLevel
}
// Default level for this example is info, unless debug flag is present
zerolog.SetGlobalLevel(slevel)
zerolog.TimestampFieldName = "ts"
zerolog.MessageFieldName = "msg"
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
pcs := make([]uintptr, 10)
runtime.Callers(2, pcs)
fs := runtime.CallersFrames(pcs)
f, more := fs.Next()
for more {
if strings.HasPrefix(f.Function, "github.com/rs/zerolog") || strings.Contains(f.Function, "(*logger).") {
f, more = fs.Next()
continue
}
shortFile := filepath.Base(f.File)
shortFunc := filepath.Base(f.Function)
return shortFunc + " (" + shortFile + ":" + strconv.Itoa(f.Line) + ")"
}
return file + ":" + strconv.Itoa(line)
}
base := zerolog.New(os.Stdout)
if format == "text" {
base = base.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339})
}
log := base.With().
Timestamp().
Caller().
Str("go.version", runtime.Version()).
Str("service", serviceName).
Logger()
return &logger{log}
}