-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
161 lines (143 loc) · 2.85 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
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
153
154
155
156
157
158
159
160
161
package console
import (
"bytes"
"path/filepath"
"runtime"
"time"
"github.com/fatih/color"
)
// Date format type
type DateFmt int
func (d DateFmt) fmt() func(t time.Time) string {
switch d {
case DateHide:
return nil
case DateHour:
return func(t time.Time) string { return t.Format("15:04:05") }
case DateFull:
return func(t time.Time) string { return t.Format("2006/01/02 15:04:05") }
default:
panic("Invalid DateFmt")
}
}
// All the date format configurations
const (
DateHide = DateFmt(iota)
DateHour
DateFull
)
// Filename format type
type FileFmt int
func (f FileFmt) fmt() func(n string) string {
switch f {
case FileHide:
return nil
case FileShow:
return func(n string) string {
d, f := filepath.Split(n)
return filepath.Base(d) + string(filepath.Separator) + f
}
case FileFull:
return func(n string) string { return n }
default:
panic("Invalid FileFmt")
}
}
// All the file path configurations
const (
FileHide = FileFmt(iota)
FileShow
FileFull
)
// Logging level
type Lvl int
// List of priorities
const (
LvlTrace = Lvl(iota)
LvlDebug
LvlInfo
LvlWarn
LvlError
LvlPanic
)
type desc struct {
label string
Color func(...interface{}) string
}
func (l desc) Label(c bool) string {
if c {
return l.Color(l.label)
}
return l.label
}
func newDesc(s string, c *color.Color) desc {
return desc{s, c.SprintFunc()}
}
var levels = map[Lvl]desc{
LvlTrace: newDesc("TRACE", color.New(color.FgHiBlue)),
LvlDebug: newDesc("DEBUG", color.New(color.FgHiCyan)),
LvlInfo: newDesc("INFO ", color.New(color.FgHiGreen)),
LvlWarn: newDesc("WARN ", color.New(color.FgHiYellow)),
LvlError: newDesc("ERROR", color.New(color.FgHiRed)),
LvlPanic: newDesc("PANIC", color.New(color.FgHiMagenta)),
}
// Holds the configuration of a Console
type Cfg struct {
Date DateFmt
File FileFmt
Lvl Lvl
Color bool
prefix string
fn struct {
date func(time.Time) string
file func(string) string
}
fmt string
}
func (c *Cfg) Label(l Lvl) string {
return levels[l].Label(c.Color)
}
func (c *Cfg) validate() {
c.fn.date = c.Date.fmt()
c.fn.file = c.File.fmt()
b := bytes.NewBuffer(nil)
if c.fn.date != nil {
s := "%s "
if c.Color {
s = white(s)
}
b.WriteString(s)
}
b.WriteString("%s ")
if c.fn.file != nil {
fl := "[%s:%d]"
if c.Color {
fl = gray(fl)
}
b.WriteString(fl + " ")
}
if c.prefix != "" {
b.WriteString("%s ")
}
b.WriteString("%s")
c.fmt = b.String()
}
func (c *Cfg) args(l Lvl, msg string) []interface{} {
var args = make([]interface{}, 0, 4)
if t := c.fn.date; t != nil {
args = append(args, t(time.Now()))
}
args = append(args, c.Label(l))
if f := c.fn.file; f != nil {
_, name, line, _ := runtime.Caller(3)
args = append(args, f(name), line)
}
if c.prefix != "" {
p := c.prefix
if c.Color {
p = levels[l].Color(p)
}
args = append(args, p)
}
return append(args, msg)
}