-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
165 lines (142 loc) · 4.44 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2024, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webapp
import (
"context"
"io"
"net/http"
"os"
"path"
"time"
"github.com/go-pogo/buildinfo"
"github.com/go-pogo/healthcheck"
"github.com/go-pogo/healthcheck/healthclient"
"github.com/go-pogo/serv"
"github.com/go-pogo/serv/accesslog"
"github.com/rs/zerolog"
)
type LoggerConfig struct {
Level zerolog.Level `env:"LOG_LEVEL" default:"debug"`
WithTimestamp bool `env:"LOG_TIMESTAMP" default:"true"`
}
var (
_ registerRouteLogger = (*Logger)(nil)
_ serv.Logger = (*Logger)(nil)
_ accesslog.Logger = (*Logger)(nil)
_ healthcheck.Logger = (*Logger)(nil)
_ healthclient.Logger = (*Logger)(nil)
)
type Logger struct{ zerolog.Logger }
func NewProductionLogger(conf LoggerConfig) *Logger {
return newLogger(os.Stdout, conf)
}
func NewDevelopmentLogger(conf LoggerConfig) *Logger {
out := zerolog.NewConsoleWriter()
out.TimeFormat = time.StampMilli
return newLogger(out, conf)
}
func newLogger(out io.Writer, conf LoggerConfig) *Logger {
log := zerolog.New(out).Level(conf.Level)
if conf.WithTimestamp {
log = log.With().Timestamp().Logger()
}
return &Logger{log}
}
func (l *Logger) LogBuildInfo(bld *buildinfo.BuildInfo, modules ...string) {
event := l.Logger.Info().
Str("go_version", bld.GoVersion()).
Str("version", bld.Version()).
Str("vcs_revision", bld.Revision()).
Time("vcs_time", bld.Time())
for _, name := range modules {
if mod := bld.Module(name); mod.Version != "" {
event.Str("module_"+path.Base(mod.Path), mod.Version)
}
}
event.Msg("buildinfo")
}
func (l *Logger) LogRegisterRoute(route serv.Route) {
l.Logger.Debug().
Str("name", route.Name).
Str("method", route.Method).
Str("pattern", route.Pattern).
Msg("register route")
}
// LogServerStart is part of the [serv.Logger] interface.
func (l *Logger) LogServerStart(name, addr string) {
l.Logger.Info().
Str("name", name).
Str("addr", addr).
Msg("server starting")
}
// LogServerStartTLS is part of the [serv.Logger] interface.
func (l *Logger) LogServerStartTLS(name, addr, certFile, keyFile string) {
l.Logger.Info().
Str("name", name).
Str("addr", addr).
Str("cert_file", certFile).
Str("key_file", keyFile).
Msg("server starting")
}
// LogServerShutdown is part of the [serv.Logger] interface.
func (l *Logger) LogServerShutdown(name string) {
l.Logger.Info().
Str("name", name).
Msg("server shutting down")
}
// LogServerClose is part of the [serv.Logger] interface.
func (l *Logger) LogServerClose(name string) {
l.Logger.Info().
Str("name", name).
Msg("server closing")
}
// LogAccess is part of the [accesslog.Logger] interface. Default log level is
// [zerolog.InfoLevel]. Every status code indicating an error is logged as
// [zerolog.WarnLevel]. All remaining requests to the [HealthCheckRoute] are
// logged as [zerolog.DebugLevel]
func (l *Logger) LogAccess(_ context.Context, det accesslog.Details, req *http.Request) {
lvl := zerolog.InfoLevel
if det.StatusCode >= 400 {
lvl = zerolog.WarnLevel
} else if det.HandlerName == HealthCheckRoute {
lvl = zerolog.DebugLevel
}
l.Logger.WithLevel(lvl).
Str("server", det.ServerName).
Str("handler", det.HandlerName).
Str("user_agent", det.UserAgent).
Str("remote_addr", accesslog.RemoteAddr(req)).
Str("method", req.Method).
Str("request_uri", accesslog.RequestURI(req)).
Int("status_code", det.StatusCode).
Int64("request_count", det.RequestCount).
Int64("bytes_written", det.BytesWritten).
Dur("duration", det.Duration).
Msg(accesslog.Message)
}
// LogHealthChanged is part of the [healthcheck.Logger] interface.
func (l *Logger) LogHealthChanged(status, oldStatus healthcheck.Status, details map[string]healthcheck.Status) {
l.Logger.Info().
Stringer("status", status).
Stringer("old_status", oldStatus).
Msg("health changed")
for name, stat := range details {
l.Logger.Debug().
Str("name", name).
Stringer("status", stat).
Msg("health")
}
}
// LogHealthChecked is part of the [healthclient.Logger] interface.
func (l *Logger) LogHealthChecked(stat healthcheck.Status) {
l.Logger.Info().
Stringer("status", stat).
Msg("health checked")
}
// LogHealthCheckFailed is part of the [healthclient.Logger] interface.
func (l *Logger) LogHealthCheckFailed(stat healthcheck.Status, err error) {
l.Logger.Err(err).
Stringer("status", stat).
Msg("health check failed")
}