-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.go
71 lines (65 loc) · 2.1 KB
/
flag.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
// File: "flag.go"
package xlog
import "flag"
// Command line logger options
type Opt struct {
LogLvl string // log level (trace/debug/info/warn/error/fatal)
SLog bool // use structured text loger (slog)
JLog bool // use structured JSON loger (slog)
TLog bool // use tinted (colorized) logger (tint)
LogSrc bool // force log source file name and line number
LogPkg bool // force log source directory/file name and line number
LogTime bool // force add time to log
LogTFmt string // log time format
}
// Setup command line logger options
// Usage:
//
// -log <level> - Log level (trace/debug/info/notice/warm/error/fatal)
// -slog - Use structured text logger (slog)
// -jlog - Use structured JSON logger (slog)
// -tlog - Use tinted (colorized) logger (tint)
// -lsrc - Force log source file name and line number
// -ltime - Force add time to log
func NewOpt() *Opt {
opt := &Opt{}
flag.StringVar(&opt.LogLvl, "log", "", "Override log level (flood/trace/debug/info/warm/error/fatal)")
flag.BoolVar(&opt.SLog, "slog", false, "Use structured text logger (slog)")
flag.BoolVar(&opt.JLog, "jlog", false, "Use structured JSON logger (slog)")
flag.BoolVar(&opt.TLog, "tlog", false, "Use tinted (colorized) logger (tint)")
flag.BoolVar(&opt.LogSrc, "lsrc", false, "Force log source file name and line number")
flag.BoolVar(&opt.LogPkg, "lpkg", false, "Force log source directory/file name and line number")
flag.BoolVar(&opt.LogTime, "ltime", false, "Force add time to log")
flag.StringVar(&opt.LogTFmt, "ltimefmt", "", "Override log time format (e.g. 15:04:05.999)")
return opt
}
// Add parsed command line options to logger config
func AddOpt(opt *Opt, conf *Conf) {
if opt.LogLvl != "" {
conf.Level = opt.LogLvl
}
if opt.SLog {
conf.Slog = true
}
if opt.JLog {
conf.JSON = true
}
if opt.TLog {
conf.Tint = true
}
if opt.LogSrc {
conf.Src = true
}
if opt.LogPkg {
conf.Src = true
conf.SrcLong = true
}
if opt.LogTime {
conf.Time = true
}
if opt.LogTFmt != "" {
conf.Time = true
conf.TimeTint = opt.LogTFmt
}
}
// EOF: "flag.go"