-
Notifications
You must be signed in to change notification settings - Fork 129
/
file_logger.go
130 lines (107 loc) · 3.48 KB
/
file_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
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package events
import (
"fmt"
"github.com/natefinch/lumberjack"
"github.com/pkg/errors"
"io"
"os"
"strings"
)
type fabricFormatterFactory struct{}
func (f fabricFormatterFactory) NewLoggingHandler(format string, buffer int, out io.WriteCloser) (interface{}, error) {
if strings.EqualFold(format, "json") {
return NewJsonFormatter(buffer, NewWriterEventSink(out)), nil
}
return nil, errors.Errorf("invalid 'format' for event log output file: %v", format)
}
type StdOutLoggerFactory struct{}
func (StdOutLoggerFactory) NewEventHandler(config map[interface{}]interface{}) (interface{}, error) {
return NewFileEventLogger(fabricFormatterFactory{}, true, config)
}
type FileEventLoggerFactory struct{}
func (FileEventLoggerFactory) NewEventHandler(config map[interface{}]interface{}) (interface{}, error) {
return NewFileEventLogger(fabricFormatterFactory{}, false, config)
}
func NewFileEventLogger(formatterFactory LoggingHandlerFactory, stdout bool, config map[interface{}]interface{}) (interface{}, error) {
// allow config to increase the buffer size
bufferSize := 10
if value, found := config["bufferSize"]; found {
if size, ok := value.(int); ok {
bufferSize = size
}
}
var output io.WriteCloser = os.Stdout
if !stdout {
// allow config to override the max file size
maxsize := 10
if value, found := config["maxsizemb"]; found {
if maxsizemb, ok := value.(int); ok {
maxsize = maxsizemb
}
}
// allow config to override the max file size
maxBackupFiles := 0
if value, found := config["maxbackups"]; found {
if maxbackups, ok := value.(int); ok {
maxBackupFiles = maxbackups
}
}
// set the path or die if not specified
filepath := ""
if value, found := config["path"]; found {
if testpath, ok := value.(string); ok {
f, err := os.OpenFile(testpath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
if err != nil {
return nil, fmt.Errorf("cannot write to log file path: %s", testpath)
} else {
filepath = testpath
_ = f.Close()
}
} else {
return nil, errors.New("invalid event FileLogger 'path' value")
}
} else {
return nil, errors.New("missing required 'path' config for events FileLogger handler")
}
output = &newlineWriter{
out: &lumberjack.Logger{
Filename: filepath,
MaxSize: maxsize,
MaxBackups: maxBackupFiles,
},
}
}
if value, found := config["format"]; found {
if format, ok := value.(string); ok {
return formatterFactory.NewLoggingHandler(format, bufferSize, output)
}
return nil, errors.New("invalid 'format' for event log output file")
}
return nil, errors.New("'format' must be specified for event handler")
}
type newlineWriter struct {
out io.WriteCloser
}
func (self newlineWriter) Close() error {
return self.out.Close()
}
func (self newlineWriter) Write(p []byte) (int, error) {
n, err := self.out.Write(p)
if err != nil {
return n, err
}
m, err := self.out.Write([]byte("\n"))
return n + m, err
}