-
Notifications
You must be signed in to change notification settings - Fork 19
/
config.go
56 lines (52 loc) · 1.56 KB
/
config.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
package log
import (
"encoding/base64"
"fmt"
"net/url"
"strconv"
)
// Config for logging
type Config struct {
Level string `yaml:"level" json:"level" default:"info" validate:"regexp=^(fatal|panic|error|warn|info|debug)$"`
Encoding string `yaml:"encoding" json:"encoding" default:"json" validate:"regexp=^(json|console)$"`
Filename string `yaml:"filename" json:"filename"`
Compress bool `yaml:"compress" json:"compress"`
MaxAge int `yaml:"maxAge" json:"maxAge" default:"15" validate:"min=1"` // days
MaxSize int `yaml:"maxSize" json:"maxSize" default:"50" validate:"min=1"` // MB
MaxBackups int `yaml:"maxBackups" json:"maxBackups" default:"15" validate:"min=1"`
}
func (c *Config) String() string {
return fmt.Sprintf("level=%s&encoding=%s&filename=%s&compress=%t&maxAge=%d&maxSize=%d&maxBackups=%d",
c.Level,
c.Encoding,
base64.URLEncoding.EncodeToString([]byte(c.Filename)),
c.Compress,
c.MaxAge,
c.MaxSize,
c.MaxBackups)
}
// FromURL creates config from url
func FromURL(u *url.URL) (c Config, err error) {
args := u.Query()
c.Level = args.Get("level")
c.Encoding = args.Get("encoding")
filename, err := base64.URLEncoding.DecodeString(args.Get("filename"))
if err != nil {
return
}
c.Filename = string(filename)
c.Compress, err = strconv.ParseBool(args.Get("compress"))
if err != nil {
return
}
c.MaxAge, err = strconv.Atoi(args.Get("maxAge"))
if err != nil {
return
}
c.MaxSize, err = strconv.Atoi(args.Get("maxSize"))
if err != nil {
return
}
c.MaxBackups, err = strconv.Atoi(args.Get("maxBackups"))
return
}