forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_logger.go
70 lines (58 loc) · 1.85 KB
/
config_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
package evergreen
import (
"github.com/evergreen-ci/evergreen/db"
"github.com/mongodb/grip/level"
"github.com/mongodb/grip/send"
"github.com/pkg/errors"
"gopkg.in/mgo.v2/bson"
)
type LoggerConfig struct {
Buffer LogBuffering `bson:"buffer" json:"buffer" yaml:"buffer"`
DefaultLevel string `bson:"default_level" json:"default_level" yaml:"default_level"`
ThresholdLevel string `bson:"threshold_level" json:"threshold_level" yaml:"threshold_level"`
}
func (c LoggerConfig) Info() send.LevelInfo {
return send.LevelInfo{
Default: level.FromString(c.DefaultLevel),
Threshold: level.FromString(c.ThresholdLevel),
}
}
func (c *LoggerConfig) SectionId() string { return "logger_config" }
func (c *LoggerConfig) Get() error {
err := db.FindOneQ(ConfigCollection, db.Query(byId(c.SectionId())), c)
if err != nil && err.Error() == errNotFound {
*c = LoggerConfig{}
return nil
}
return errors.Wrapf(err, "error retrieving section %s", c.SectionId())
}
func (c *LoggerConfig) Set() error {
_, err := db.Upsert(ConfigCollection, byId(c.SectionId()), bson.M{
"$set": bson.M{
"buffer": c.Buffer,
"default_level": c.DefaultLevel,
"threshold_level": c.ThresholdLevel,
},
})
return errors.Wrapf(err, "error updating section %s", c.SectionId())
}
func (c *LoggerConfig) ValidateAndDefault() error {
if c.Buffer.DurationSeconds == 0 {
c.Buffer.DurationSeconds = defaultLogBufferingDuration
}
if c.DefaultLevel == "" {
c.DefaultLevel = "info"
}
if c.ThresholdLevel == "" {
c.ThresholdLevel = "debug"
}
info := c.Info()
if !info.Valid() {
return errors.Errorf("logging level configuration is not valid [%+v]", info)
}
return nil
}
type LogBuffering struct {
DurationSeconds int `bson:"duration_seconds" json:"duration_seconds" yaml:"duration_seconds"`
Count int `bson:"count" json:"count" yaml:"count"`
}