-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
78 lines (67 loc) · 1.99 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
package log
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/asecurityteam/logevent/v2"
"github.com/asecurityteam/settings/v2"
)
const (
// OutputStdout sends logs to stdout.
OutputStdout = "STDOUT"
// OutputNull sends logs to /dev/null.
OutputNull = "NULL"
defaultLevel = "INFO"
defaultOutput = OutputStdout
)
// Config contains all configuration values for creating a system logger.
type Config struct {
Level string `description:"The minimum level of logs to emit. One of DEBUG, INFO, WARN, ERROR."`
Output string `description:"Destination stream of the logs. One of STDOUT, NULL."`
}
// Name of the configuration as it might appear in config files.
func (*Config) Name() string {
return "logger"
}
// Component enables creating configured loggers.
type Component struct{}
// NewComponent populates all the defaults.
func NewComponent() *Component {
return &Component{}
}
// Settings generates a LoggerConfig with default values applied.
func (*Component) Settings() *Config {
return &Config{
Level: defaultLevel,
Output: defaultOutput,
}
}
// New creates a configured logger instance.
func (*Component) New(_ context.Context, conf *Config) (Logger, error) {
var output io.Writer
switch {
case strings.EqualFold(conf.Output, OutputStdout):
output = os.Stdout
case strings.EqualFold(conf.Output, OutputNull):
output = ioutil.Discard
default:
return nil, fmt.Errorf("unknown logger output %s", conf.Output)
}
return logevent.New(logevent.Config{Level: conf.Level, Output: output}), nil
}
// Load is a convenience method for binding the source to the component.
func Load(ctx context.Context, source settings.Source, c *Component) (Logger, error) {
dst := new(Logger)
err := settings.NewComponent(ctx, source, c, dst)
if err != nil {
return nil, err
}
return *dst, nil
}
// New is the top-level entry point for creating a new log client.
func New(ctx context.Context, source settings.Source) (Logger, error) {
return Load(ctx, source, NewComponent())
}