-
Notifications
You must be signed in to change notification settings - Fork 453
/
logging.go
90 lines (73 loc) · 2.4 KB
/
logging.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package runtime
import (
"context"
"errors"
"io"
"time"
"go.uber.org/zap/zapcore"
)
// LoggingManager provides unified interface to publish and consume logs.
type LoggingManager interface {
// ServiceLog privides a log handler for a given service (that may not exist).
ServiceLog(service string) LogHandler
// SetSenders sets log senders for all derived log handlers
// and returns the previous ones for closing.
//
// SetSenders should be thread-safe.
SetSenders(senders []LogSender) []LogSender
// RegisteredLogs returns a list of registered logs containers.
RegisteredLogs() []string
}
// LogOptions for LogHandler.Reader.
type LogOptions struct {
Follow bool
TailLines *int
}
// LogOption provides functional options for LogHandler.Reader.
type LogOption func(*LogOptions) error
// WithFollow enables follow mode for the logs.
func WithFollow() LogOption {
return func(o *LogOptions) error {
o.Follow = true
return nil
}
}
// WithTailLines starts log reading from lines from the tail of the log.
func WithTailLines(lines int) LogOption {
return func(o *LogOptions) error {
o.TailLines = &lines
return nil
}
}
// LogHandler provides interface to access particular log source.
type LogHandler interface {
Writer() (io.WriteCloser, error)
Reader(opt ...LogOption) (io.ReadCloser, error)
}
// LogEvent represents a log message to be send.
type LogEvent struct {
Msg string
Time time.Time
Level zapcore.Level
Fields map[string]interface{}
}
// ErrDontRetry indicates that log event should not be resent.
var ErrDontRetry = errors.New("don't retry")
// LogSender provides common interface for log senders.
type LogSender interface {
// Send tries to send the log event once, exiting on success, error, or context cancelation.
//
// Returned error is nil on success, non-nil otherwise.
// As a special case, Send can return (possibly wrapped) ErrDontRetry if the log event should not be resent
// (if it is invalid, if it was sent partially, etc).
//
// Send should be thread-safe.
Send(ctx context.Context, e *LogEvent) error
// Close stops the sender gracefully if possible, or forcefully on context cancelation.
//
// Close should be thread-safe.
Close(ctx context.Context) error
}