-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlogging.go
More file actions
89 lines (71 loc) · 2.85 KB
/
logging.go
File metadata and controls
89 lines (71 loc) · 2.85 KB
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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package sdk
import (
"log"
"testing"
)
// Logger is the logging interface used throughout the SDK.
// This interface is compatible with the fabric logger from fabric-lib-go.
//
// Users can provide any logger implementation that supports formatted logging at different levels.
type Logger interface {
Debugf(template string, args ...any)
Infof(template string, args ...any)
Warnf(template string, args ...any)
Errorf(template string, args ...any)
}
// NoOpLogger is a logger implementation that discards all log output.
// Use this when you want to disable logging entirely.
type NoOpLogger struct{}
func (NoOpLogger) Debugf(template string, args ...any) {}
func (NoOpLogger) Infof(template string, args ...any) {}
func (NoOpLogger) Warnf(template string, args ...any) {}
func (NoOpLogger) Errorf(template string, args ...any) {}
// StdLogger is a logger implementation that uses Go's standard library log package.
// It prefixes all messages with the component name and log level.
type StdLogger struct {
prefix string
}
// NewStdLogger creates a new logger that uses the standard library log package.
// All log messages will be prefixed with the given component name.
func NewStdLogger(component string) Logger {
return &StdLogger{prefix: component}
}
func (l *StdLogger) Debugf(template string, args ...any) {
log.Printf("[%s] [DEBUG] "+template, append([]any{l.prefix}, args...)...)
}
func (l *StdLogger) Infof(template string, args ...any) {
log.Printf("[%s] [INFO] "+template, append([]any{l.prefix}, args...)...)
}
func (l *StdLogger) Warnf(template string, args ...any) {
log.Printf("[%s] [WARN] "+template, append([]any{l.prefix}, args...)...)
}
func (l *StdLogger) Errorf(template string, args ...any) {
log.Printf("[%s] [ERROR] "+template, append([]any{l.prefix}, args...)...)
}
// TestLogger is a logger implementation that uses testing.T's Log function.
// Use this in tests to have log output captured and displayed by the test runner.
type TestLogger struct {
t *testing.T
prefix string
}
// NewTestLogger creates a new logger that writes to testing.T's log output.
// All log messages will be prefixed with the given component name.
func NewTestLogger(t *testing.T, component string) Logger {
return &TestLogger{t: t, prefix: component}
}
func (l *TestLogger) Debugf(template string, args ...any) {
l.t.Logf("[%s] [DEBUG] "+template, append([]any{l.prefix}, args...)...)
}
func (l *TestLogger) Infof(template string, args ...any) {
l.t.Logf("[%s] [INFO] "+template, append([]any{l.prefix}, args...)...)
}
func (l *TestLogger) Warnf(template string, args ...any) {
l.t.Logf("[%s] [WARN] "+template, append([]any{l.prefix}, args...)...)
}
func (l *TestLogger) Errorf(template string, args ...any) {
l.t.Logf("[%s] [ERROR] "+template, append([]any{l.prefix}, args...)...)
}