-
Notifications
You must be signed in to change notification settings - Fork 383
/
stream.go
70 lines (63 loc) · 1.69 KB
/
stream.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
package logutil
import (
"go.uber.org/zap"
"moul.io/zapring"
)
const (
typeStd = "std"
typeRing = "ring"
typeFile = "file"
typeCustom = "custom"
)
type Stream struct {
kind string
filters string
format string
path string
ring *zapring.Core
sessionKind string
baseLogger *zap.Logger
}
func NewStdStream(filters, format, path string) Stream {
return Stream{
kind: typeStd,
filters: filters,
format: format,
path: path,
}
}
func NewRingStream(filters, format string, ring *zapring.Core) Stream {
return Stream{
kind: typeRing,
filters: filters,
format: format,
ring: ring,
}
}
// NewFileStream creates a new file stream backed by Lumberjack with sane default values.
//
// Usually, Lumberjack is used as a rolling log file and is intended to be reused from a session to another,
// In Berty, we want one file per session named with the start time instead of the rotation time.
//
// If the provided path is a directory, it will create files in that directory with the following pattern:
// `<path>/<session-kind>-<start-time>.log`.
//
// If the provided path is a path finishing with ".log", then, the path will be taken as it,
// instead of creating a new file, it will append new lines to the existing one;
// this can be particularly useful to keep a `tail -f` running.
func NewFileStream(filters, format, path, sessionKind string) Stream {
return Stream{
kind: typeFile,
filters: filters,
format: format,
path: path,
sessionKind: sessionKind,
}
}
func NewCustomStream(filters string, logger *zap.Logger) Stream {
return Stream{
kind: typeCustom,
filters: filters,
baseLogger: logger,
}
}