forked from byte-power/room
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
107 lines (98 loc) · 3.38 KB
/
log.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package base
import (
"bytepower_room/base/log"
"bytepower_room/utility"
"errors"
"fmt"
"os"
"strings"
)
func parseLogger(name string, cfg utility.StrMap) (*log.Logger, error) {
var outputs []log.Output
for k, v := range cfg {
vs := utility.AnyToAnyMap(v)
if vs == nil && v != nil {
return nil, fmt.Errorf("'log.%v' should be map", k)
}
format := parseFormat(vs)
level := parseLevel(vs["level"])
var output log.Output
switch k {
case "console":
output = parseConsoleLogger(name, format, level, vs)
case "file":
output = parseFileLogger(name, format, level, vs)
case "tcp":
output, _ = parseTCPLogger(name, format, level, vs)
}
if output == nil {
return nil, fmt.Errorf("'log.%v' unknown output type", k)
}
outputs = append(outputs, output)
}
return log.NewLogger(outputs...), nil
}
func parseConsoleLogger(name string, fmt log.LocalFormat, level log.Level, cfg utility.AnyMap) log.Output {
stream := log.MakeConsoleStream(utility.AnyToString(cfg["stream"]))
return log.MakeConsoleOutput(name, fmt, level, stream)
}
func parseFileLogger(name string, fmt log.LocalFormat, level log.Level, cfg utility.AnyMap) log.Output {
location := utility.AnyToString(cfg["location"])
if strings.Contains(location, "{pid}") {
location = strings.Replace(location, "{pid}", utility.AnyToString(os.Getpid()), 1)
}
rotation := parseFileRotation(utility.AnyToAnyMap(cfg["rotation"]))
return log.MakeFileOutput(name, fmt, level, location, rotation)
}
func parseTCPLogger(name string, fmt log.LocalFormat, level log.Level, cfg utility.AnyMap) (log.Output, error) {
if name == "" {
return nil, errors.New("TCPWriter name must be sepecified in config.")
}
config := utility.DefaultTCPWriterConfig()
if it, ok := cfg["dsn"].(string); ok {
if it == "" {
return nil, errors.New("TCPWriter dsn must be specified in config.")
}
config.DSN = it
}
config.Async = utility.AnyToBool(cfg["async"])
if it := int(utility.AnyToInt64(cfg["buffer_limit"])); it > 0 {
config.BufferLimit = it
}
return log.MakeTCPOutput(name, fmt, level, config), nil
}
func parseFormat(cfg utility.AnyMap) log.LocalFormat {
msgFMT := parseMessageFormat(cfg["format"])
fmt := log.MakeLocalFormat(msgFMT)
if keys := utility.AnyToAnyMap(cfg["keys"]); keys != nil {
fmt.CallerKey = utility.AnyToString(keys["caller"])
fmt.TimeKey = utility.AnyToString(keys["time"])
fmt.MessageKey = utility.AnyToString(keys["message"])
fmt.LevelKey = utility.AnyToString(keys["level"])
fmt.NameKey = utility.AnyToString(keys["name"])
}
if timeFMT, ok := cfg["time_format"].(string); ok {
fmt.TimeFormat = log.MakeTimeFormat(timeFMT)
}
return fmt
}
func parseMessageFormat(v interface{}) log.MessageFormat {
name, _ := v.(string)
return log.MakeMessageFormat(name)
}
func parseLevel(v interface{}) log.Level {
name, _ := v.(string)
return log.MakeLevelWithName(name)
}
func parseFileRotation(cfg utility.AnyMap) log.FileRotation {
return log.FileRotation{
MaxSize: int(utility.AnyToInt64(cfg["max_size"])),
Compress: utility.AnyToBool(cfg["compress"]),
MaxAge: int(utility.AnyToInt64(cfg["max_age"])),
MaxBackups: int(utility.AnyToInt64(cfg["max_backups"])),
LocalTime: utility.AnyToBool(cfg["localtime"]),
RotateOnTime: utility.AnyToBool(cfg["rotate_on_time"]),
RotatePeriod: utility.AnyToString(cfg["rotate_period"]),
RotateAfter: int(utility.AnyToInt64(cfg["rotate_after"])),
}
}