-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
40 lines (34 loc) · 1016 Bytes
/
file.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
package vdlog
import (
"fmt"
"os"
)
func generateFileSink(filename string, minLevel, maxLevel EventLevel) func(e Event) error {
return func(e Event) error {
if e.Level < minLevel {
return nil
}
if e.Level > maxLevel {
return nil
}
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer file.Close()
_, err = fmt.Fprintln(file, e.String())
return err
}
}
//LogErrorsToFile outputs only errors to file provided
func LogErrorsToFile(filename string) {
AddSink(filename, generateFileSink(filename, LevelError, LevelError))
}
//LogNormalToFile outputs only WARN---DEBUG messages to file provided
func LogNormalToFile(filename string) {
AddSink(filename, generateFileSink(filename, LevelWarn, LevelDebug))
}
//LogToFile allows to output desired range of events by level intp file provided
func LogToFile(filename string, minLevel, maxLevel EventLevel) {
AddSink(filename, generateFileSink(filename, minLevel, maxLevel))
}