-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugins.go
68 lines (59 loc) · 1.75 KB
/
plugins.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
package logging
import (
"errors"
"io"
"os"
"strconv"
)
// A WriterPlugin implements OutputPlugin by using a function to choose an io.Writer.
type WriterPlugin func(options map[string]string) (writer io.Writer, err error)
// Creates and returns a new StringOutputter. The format is taken from the "format" option, which must exist. The output
// StringWriter is obtained by calling the WriterPlugin (which is a function).
func (chooser WriterPlugin) CreateOutputter(options map[string]string) (result Outputter, err error) {
// Setup formatter
format := options["format"]
if format == "" {
return nil, errors.New("console formatting string not specified")
}
formatter := NewBasicFormatter(format)
// Determine output stream to use
output, err := chooser(options)
if err != nil {
return
}
return StringOutputter{
Writer: IOWriter{output},
Formatter: formatter,
}, nil
}
var consolePlugin = WriterPlugin(func(options map[string]string) (output io.Writer, err error) {
stream := options["stream"]
switch {
case stream == "stdout":
output = os.Stdout
case stream == "stderr":
output = os.Stderr
case stream == "":
err = errors.New("console stream not specified")
default:
if fd, err := strconv.Atoi(stream); err == nil {
output = os.NewFile(uintptr(fd), "logging_output")
} else {
err = errors.New("invalid console stream: " + stream)
}
}
return
})
var filePlugin = WriterPlugin(func(options map[string]string) (output io.Writer, err error) {
path := options["file"]
if path == "" {
err = errors.New("file option not specified")
} else {
output, err = os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
}
return
})
func init() {
RegisterOutputPlugin("console", consolePlugin)
RegisterOutputPlugin("file", filePlugin)
}