forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
105 lines (87 loc) · 2 KB
/
service.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
package logging
import (
"io"
"log"
"os"
"path"
"github.com/influxdata/wlog"
)
type Level wlog.Level
const (
_ Level = iota
DEBUG
INFO
WARN
ERROR
OFF
)
// Interface for creating new loggers
type Interface interface {
NewLogger(prefix string, flag int) *log.Logger
NewRawLogger(prefix string, flag int) *log.Logger
NewStaticLevelLogger(prefix string, flag int, l Level) *log.Logger
NewStaticLevelWriter(l Level) io.Writer
}
type Service struct {
f io.WriteCloser
c Config
stdout io.Writer
stderr io.Writer
}
func NewService(c Config, stdout, stderr io.Writer) *Service {
return &Service{
c: c,
stdout: stdout,
stderr: stderr,
}
}
func (s *Service) Open() error {
switch s.c.File {
case "STDERR":
s.f = &nopCloser{f: s.stderr}
case "STDOUT":
s.f = &nopCloser{f: s.stdout}
default:
dir := path.Dir(s.c.File)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
}
f, err := os.OpenFile(s.c.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0640)
if err != nil {
return err
}
s.f = f
}
// Configure default logger
log.SetPrefix("[log] ")
log.SetFlags(log.LstdFlags)
log.SetOutput(wlog.NewWriter(s.f))
wlog.SetLevelFromName(s.c.Level)
return nil
}
func (s *Service) Close() error {
if s.f != nil {
return s.f.Close()
}
return nil
}
func (s *Service) NewLogger(prefix string, flag int) *log.Logger {
return wlog.New(s.f, prefix, flag)
}
func (s *Service) NewRawLogger(prefix string, flag int) *log.Logger {
return log.New(s.f, prefix, flag)
}
func (s *Service) NewStaticLevelLogger(prefix string, flag int, l Level) *log.Logger {
return log.New(wlog.NewStaticLevelWriter(s.f, wlog.Level(l)), prefix, flag)
}
func (s *Service) NewStaticLevelWriter(l Level) io.Writer {
return wlog.NewStaticLevelWriter(s.f, wlog.Level(l))
}
type nopCloser struct {
f io.Writer
}
func (c *nopCloser) Write(b []byte) (int, error) { return c.f.Write(b) }
func (c *nopCloser) Close() error { return nil }