This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
forked from rainycape/governator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
188 lines (173 loc) · 3.9 KB
/
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
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
package main
import (
"bytes"
"fmt"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/fiam/parseutil"
"github.com/fiam/stringutil"
)
const (
LogDir = "/var/log/governator"
)
var (
// Altered during tests
logDir = LogDir
lineBreak = []byte{'\n'}
)
type Out struct {
Logger *Logger
prefix string
buf []byte
}
func (o *Out) Write(b []byte) (int, error) {
if bytes.IndexByte(b, '\n') < 0 {
o.buf = append(o.buf, b...)
return len(b), nil
}
rem := b
for len(rem) > 0 {
p := bytes.LastIndex(rem, lineBreak)
if p < 0 {
break
}
line := rem[:p+1]
rem = rem[p+1:]
if len(o.buf) == 0 {
// Nothing in the buffer, just write the line
o.Logger.Write(o.prefix, line)
continue
}
// Construct a line from the current line and the buffer. Doesn't
// matter if we trash the buffer because we're going to write and then
// discard it.
o.buf = append(o.buf, line...)
o.Logger.Write(o.prefix, o.buf)
o.buf = o.buf[:0]
}
// Append remaining data to buffer
o.buf = append(o.buf, rem...)
return len(b), nil
}
type Writer interface {
Open(string) error
Close() error
Write(string, []byte) error
Flush() error
}
type LogMonitor func(string, []byte)
type Logger struct {
Name string
w Writer
Stdout *Out
Stderr *Out
Monitor LogMonitor
buf []byte
mu sync.Mutex
}
func (l *Logger) Open() error {
return l.w.Open(l.Name)
}
func (l *Logger) Close() error {
return l.w.Close()
}
func (l *Logger) Write(prefix string, b []byte) error {
if len(b) == 0 {
return nil
}
now := time.Now().Unix()
l.mu.Lock()
defer l.mu.Unlock()
l.buf = strconv.AppendInt(l.buf[:0], now, 10)
l.buf = append(l.buf, ' ', '-', ' ')
l.buf = append(l.buf, b...)
if b[len(b)-1] != '\n' {
l.buf = append(l.buf, '\n')
}
if err := l.w.Write(prefix, l.buf); err != nil {
return err
}
if err := l.w.Flush(); err != nil {
return err
}
if l.Monitor != nil {
l.Monitor(prefix, l.buf)
}
return nil
}
func (l *Logger) WriteString(prefix string, s string) {
l.Write(prefix, []byte(s))
}
func (l *Logger) Flush() {
l.w.Flush()
}
func (l *Logger) Parse(input string) error {
if input == "" {
input = "file"
}
args, err := stringutil.SplitFields(input, " ")
if err != nil {
return err
}
switch strings.ToLower(args[0]) {
case "file":
maxSize := uint64(500 * 1024 * 1024) // 500MB
count := 10 // 10 rotated files
switch len(args) {
case 1:
break
case 3:
c, err := strconv.Atoi(args[2])
if err != nil {
return fmt.Errorf("invalid file count %q, must be an integer", args[2])
}
count = c
fallthrough
case 2:
size, err := parseutil.Size(args[1])
if err != nil {
return err
}
maxSize = size
default:
return fmt.Errorf("invalid number of arguments for file logger - must be one or two, %d given", len(args)-1)
}
l.w = &fileWriter{dir: logDir, maxSize: maxSize, count: count}
case "syslog":
var scheme string
var addr string
switch len(args) {
case 1:
break
case 2:
u, err := url.Parse(args[1])
if err != nil {
return fmt.Errorf("invalid syslog URL %q: %s", args[1], err)
}
if u.User != nil || u.Path != "" || u.RawQuery != "" || u.Fragment != "" {
return fmt.Errorf("invalid syslog URL %q: must not have user, nor path, nor query, nor fragment", args[1])
}
if u.Scheme == "" {
return fmt.Errorf("invalid syslog URL %q: scheme can't be empty", args[1])
}
if u.Host == "" {
return fmt.Errorf("invalid syslog URL %q: host can't be empty", args[1])
}
scheme = u.Scheme
addr = u.Host
default:
return fmt.Errorf("invalid number of arguments for syslog logger - must be zero or one, %d given", len(args)-1)
}
l.w = &syslogWriter{scheme: scheme, addr: addr}
case "none":
l.w = &noneWriter{}
default:
return fmt.Errorf("invalid logger %s", args[0])
}
l.Stdout = &Out{Logger: l, prefix: "stdout"}
l.Stderr = &Out{Logger: l, prefix: "stderr"}
return nil
}