-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
48 lines (36 loc) · 1.23 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
// Copyright (c) 2023, 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 healthcheck
import (
"log"
)
type Logger interface {
LogHealthChanged(newStatus, oldStatus Status, statuses map[string]Status)
}
const panicNewNilLogger = "healthcheck.NewLogger: log.Logger should not be nil"
// NewLogger returns a [Logger] that uses a [log.Logger] to log health
// status events.
func NewLogger(l *log.Logger) Logger {
if l == nil {
panic(panicNewNilLogger)
}
return &logger{l}
}
// DefaultLogger returns a [Logger] that uses [log.Default] to log health
// status events.
func DefaultLogger() Logger { return &logger{log.Default()} }
// NopLogger returns a [Logger] that does nothing.
func NopLogger() Logger { return new(nopLogger) }
type logger struct{ *log.Logger }
func (l *logger) LogHealthChanged(status, oldStatus Status, statuses map[string]Status) {
l.Logger.Printf("health changed from %s to %s\n", oldStatus, status)
if statuses == nil {
return
}
for name, stat := range statuses {
l.Logger.Printf("health for %s is %s\n", name, stat)
}
}
type nopLogger struct{}
func (*nopLogger) LogHealthChanged(_, _ Status, _ map[string]Status) {}