forked from rifflock/lfshook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfshook.go
194 lines (167 loc) · 4.73 KB
/
lfshook.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Package lfshook is hook for sirupsen/logrus that used for writing the logs to local files.
package lfshook
import (
"fmt"
"github.com/sirupsen/logrus"
"io"
"log"
"os"
"path/filepath"
"reflect"
"sync"
)
// We are logging to file, strip colors to make the output more readable.
var defaultFormatter = &logrus.TextFormatter{DisableColors: true}
// PathMap is map for mapping a log level to a file's path.
// Multiple levels may share a file, but multiple files may not be used for one level.
type PathMap map[logrus.Level]string
// WriterMap is map for mapping a log level to an io.Writer.
// Multiple levels may share a writer, but multiple writers may not be used for one level.
type WriterMap map[logrus.Level]io.Writer
// LfsHook is a hook to handle writing to local log files.
type LfsHook struct {
paths PathMap
writers WriterMap
levels []logrus.Level
lock *sync.Mutex
formatter logrus.Formatter
defaultPath string
defaultWriter io.Writer
hasDefaultPath bool
hasDefaultWriter bool
}
// NewHook returns new LFS hook.
// Output can be a string, io.Writer, WriterMap or PathMap.
// If using io.Writer or WriterMap, user is responsible for closing the used io.Writer.
func NewHook(output interface{}, formatter logrus.Formatter) *LfsHook {
hook := &LfsHook{
lock: new(sync.Mutex),
}
hook.SetFormatter(formatter)
switch output.(type) {
case string:
hook.SetDefaultPath(output.(string))
break
case io.Writer:
hook.SetDefaultWriter(output.(io.Writer))
break
case PathMap:
hook.paths = output.(PathMap)
for level := range output.(PathMap) {
hook.levels = append(hook.levels, level)
}
break
case WriterMap:
hook.writers = output.(WriterMap)
for level := range output.(WriterMap) {
hook.levels = append(hook.levels, level)
}
break
default:
panic(fmt.Sprintf("unsupported level map type: %v", reflect.TypeOf(output)))
}
return hook
}
// SetFormatter sets the format that will be used by hook.
// If using text formatter, this method will disable color output to make the log file more readable.
func (hook *LfsHook) SetFormatter(formatter logrus.Formatter) {
hook.lock.Lock()
defer hook.lock.Unlock()
if formatter == nil {
formatter = defaultFormatter
} else {
switch formatter.(type) {
case *logrus.TextFormatter:
textFormatter := formatter.(*logrus.TextFormatter)
textFormatter.DisableColors = true
}
}
hook.formatter = formatter
}
// SetDefaultPath sets default path for levels that don't have any defined output path.
func (hook *LfsHook) SetDefaultPath(defaultPath string) {
hook.lock.Lock()
defer hook.lock.Unlock()
hook.defaultPath = defaultPath
hook.hasDefaultPath = true
}
// SetDefaultWriter sets default writer for levels that don't have any defined writer.
func (hook *LfsHook) SetDefaultWriter(defaultWriter io.Writer) {
hook.lock.Lock()
defer hook.lock.Unlock()
hook.defaultWriter = defaultWriter
hook.hasDefaultWriter = true
}
// Fire writes the log file to defined path or using the defined writer.
// User who run this function needs write permissions to the file or directory if the file does not yet exist.
func (hook *LfsHook) Fire(entry *logrus.Entry) error {
hook.lock.Lock()
defer hook.lock.Unlock()
if hook.writers != nil || hook.hasDefaultWriter {
return hook.ioWrite(entry)
} else if hook.paths != nil || hook.hasDefaultPath {
return hook.fileWrite(entry)
}
return nil
}
// Write a log line to an io.Writer.
func (hook *LfsHook) ioWrite(entry *logrus.Entry) error {
var (
writer io.Writer
msg []byte
err error
ok bool
)
if writer, ok = hook.writers[entry.Level]; !ok {
if hook.hasDefaultWriter {
writer = hook.defaultWriter
} else {
return nil
}
}
// use our formatter instead of entry.String()
msg, err = hook.formatter.Format(entry)
if err != nil {
log.Println("failed to generate string for entry:", err)
return err
}
_, err = writer.Write(msg)
return err
}
// Write a log line directly to a file.
func (hook *LfsHook) fileWrite(entry *logrus.Entry) error {
var (
fd *os.File
path string
msg []byte
err error
ok bool
)
if path, ok = hook.paths[entry.Level]; !ok {
if hook.hasDefaultPath {
path = hook.defaultPath
} else {
return nil
}
}
dir := filepath.Dir(path)
os.MkdirAll(dir, os.ModePerm)
fd, err = os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
log.Println("failed to open logfile:", path, err)
return err
}
defer fd.Close()
// use our formatter instead of entry.String()
msg, err = hook.formatter.Format(entry)
if err != nil {
log.Println("failed to generate string for entry:", err)
return err
}
fd.Write(msg)
return nil
}
// Levels returns configured log levels.
func (hook *LfsHook) Levels() []logrus.Level {
return logrus.AllLevels
}