-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.go
More file actions
152 lines (136 loc) · 3.06 KB
/
config.go
File metadata and controls
152 lines (136 loc) · 3.06 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2018-2022 KIDTSUNAMI
// Author: alex@kidtsunami.com
package log
import (
"fmt"
stdlog "log"
"os"
"strings"
"time"
"github.com/fatih/color"
)
var defaultFlags int = stdlog.Ldate | stdlog.Ltime | stdlog.Lmicroseconds | stdlog.LUTC
var levelStrs = [...]string{"TRCE ", "DEBG ", "INFO ", "WARN ", "ERRO ", "CRIT ", "OFF "}
var levelColors = [...]*color.Color{
color.New(ColorTrace),
color.New(ColorDebug),
color.New(ColorInfo),
color.New(ColorWarn),
color.New(ColorError),
color.New(ColorFatal),
}
func ParseLevel(s string) Level {
switch strings.ToLower(s) {
case "trace":
return LevelTrace
case "debug":
return LevelDebug
case "info":
return LevelInfo
case "warn":
return LevelWarn
case "error":
return LevelError
case "fatal":
return LevelFatal
case "off":
return LevelOff
default:
return LevelInvalid
}
}
func (l Level) String() string {
switch l {
case LevelTrace:
return "trace"
case LevelDebug:
return "debug"
case LevelInfo:
return "info"
case LevelWarn:
return "warn"
case LevelError:
return "error"
case LevelFatal:
return "fatal"
case LevelOff:
return "off"
default:
return ""
}
}
func (l Level) Prefix() string {
if l >= LevelOff {
return "off"
}
return levelStrs[l]
}
func (l Level) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}
func (l *Level) UnmarshalText(data []byte) error {
v := ParseLevel(string(data))
if v == LevelInvalid {
return fmt.Errorf("invalid log level '%s'", string(data))
}
*l = v
return nil
}
type Config struct {
Level Level `json:"level"`
Flags int `json:"flags"`
Backend string `json:"backend"`
Addr string `json:"addr"`
Facility string `json:"facility"`
Ident string `json:"ident"`
Filename string `json:"filename"`
FileMode os.FileMode `json:"filemode"`
ProgressInterval time.Duration `json:"progress"`
NoColor bool `json:"nocolor"`
}
func NewConfig() *Config {
c := &Config{
Level: LevelInfo,
Flags: defaultFlags,
Backend: "stdout", // stdout, stderr, syslog, file
Addr: "",
Facility: "local0",
Ident: "logfile",
Filename: "logfile.log",
FileMode: 0600,
ProgressInterval: defaultProgressInterval,
}
c.ParseEnv()
return c
}
func ParseFlags(flags string) int {
var cflags int
if flags == "" {
return defaultFlags
}
for _, f := range strings.Split(flags, ",") {
switch f {
case "longfile":
cflags |= stdlog.Llongfile
case "shortfile":
cflags |= stdlog.Lshortfile
case "date":
cflags |= stdlog.Ldate
case "time":
cflags |= stdlog.Ltime
case "micro":
cflags |= stdlog.Lmicroseconds
case "utc":
cflags |= stdlog.LUTC
}
}
return cflags
}
func (cfg *Config) ParseEnv() {
cfg.Flags = ParseFlags(os.Getenv("LOGFLAGS"))
cfg.Level = ParseLevel(os.Getenv("LOGLEVEL"))
if cfg.Level == LevelInvalid {
cfg.Level = LevelInfo
}
}
func (cfg *Config) Check() error { return nil }