-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
74 lines (65 loc) · 1.41 KB
/
handler.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
// Package apexorc provides a handler for logging via
// github.com/apex/log to an ORC file.
package apexorc
import (
"os"
"sync"
"github.com/apex/log"
"github.com/scritchley/orc"
)
// Handler complies with the github.com/apex/log.Handler interface and
// can be passed to github.com/apex/log.SetHandler
type Handler struct {
mu sync.Mutex
path string
writer *orc.Writer
}
// NewHandler returns a Handler which can log to an ORC file at the
// provided path.
func NewHandler(path string) *Handler {
return &Handler{
path: path,
}
}
func (h *Handler) openORCFile() error {
f, err := os.Create(h.path)
if err != nil {
return err
}
w, err := newWriter(f)
if err != nil {
return err
}
h.writer = w
return nil
}
func (h *Handler) closeORCFile() error {
// If we never call HandleLog then there'll be no writer.
if h.writer != nil {
err := h.writer.Close()
if err != nil {
return err
}
h.writer = nil
}
return nil
}
// HandleLog recieves new log.Entrys and writes them to an ORC file or
// errors, as specified by the github.com/apex/log.Handler intefrace.
func (h *Handler) HandleLog(e *log.Entry) error {
h.mu.Lock()
defer h.mu.Unlock()
if h.writer == nil {
err := h.openORCFile()
if err != nil {
return err
}
}
return writeRecord(h.writer, e)
}
// Close finalises the underlying ORC file.
func (h *Handler) Close() error {
h.mu.Lock()
defer h.mu.Unlock()
return h.closeORCFile()
}