-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.go
157 lines (135 loc) · 3.79 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
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
package main
// This logging trick is learnt from a post by Rob Pike
// https://groups.google.com/d/msg/golang-nuts/gU7oQGoCkmg/j3nNxuS2O_sJ
import (
"flag"
"fmt"
"io"
"log"
"os"
"runtime"
"strconv"
"strings"
"github.com/cyfdecyf/color"
)
// TODO 最后一条日志的重复归并,
// 即如果连续出现相同的日志,则不再输出更多相同的行,而是对重复的行进行计数修改。
// 这种功能在emacs的*Message*中看到过。
type infoLogging bool
type debugLogging bool
type errorLogging bool
type requestLogging bool
type responseLogging bool
var (
info infoLogging
debug debugLogging
errl errorLogging
dbgRq requestLogging
dbgRep responseLogging
logFile io.Writer
logFlags = log.Flags()
// make sure logger can be called before initLog
infoLog = log.New(os.Stdout, "[INFO] ", logFlags)
errorLog = log.New(os.Stdout, "[ERROR] ", logFlags)
debugLog = log.New(os.Stdout, "[DEBUG] ", logFlags)
requestLog = log.New(os.Stdout, "[>>>>>] ", logFlags)
responseLog = log.New(os.Stdout, "[<<<<<] ", logFlags)
verbose bool
colorize bool
)
func init() {
flag.BoolVar((*bool)(&info), "info", true, "info log")
flag.BoolVar((*bool)(&debug), "debug", false, "debug log, with this option, log goes to stdout with color")
flag.BoolVar((*bool)(&errl), "err", true, "error log")
/*
flag.BoolVar((*bool)(&dbgRq), "request", true, "request log")
flag.BoolVar((*bool)(&dbgRep), "reply", true, "reply log")
*/
flag.BoolVar(&verbose, "v", false, "more info in request/response logging")
flag.BoolVar(&colorize, "color", false, "colorize log output")
}
func initLog() {
logFile = os.Stdout
log.SetOutput(logFile)
if colorize {
color.SetDefaultColor(color.ANSI)
} else {
color.SetDefaultColor(color.NoColor)
}
infoLog = log.New(logFile, "[INFO] ", logFlags)
errorLog = log.New(logFile, color.Red("[ERROR] "), logFlags)
debugLog = log.New(logFile, color.Blue("[DEBUG] "), logFlags)
requestLog = log.New(logFile, color.Green("[>>>>>] "), logFlags)
responseLog = log.New(logFile, color.Yellow("[<<<<<] "), logFlags)
}
func (d infoLogging) Printf(format string, args ...interface{}) {
if d {
format = "%s " + format
args = append(getFileLine(), args...)
infoLog.Printf(format, args...)
}
}
func (d infoLogging) Println(args ...interface{}) {
if d {
args = append(getFileLine(), args...)
infoLog.Println(args...)
}
}
func (d debugLogging) Printf(format string, args ...interface{}) {
if d {
format = "%s " + format
args = append(getFileLine(), args...)
debugLog.Printf(format, args...)
}
}
func (d debugLogging) Println(args ...interface{}) {
if d {
args = append(getFileLine(), args...)
debugLog.Println(args...)
}
}
func (d errorLogging) Printf(format string, args ...interface{}) {
if d {
format = "%s " + format
args = append(getFileLine(), args...)
errorLog.Printf(format, args...)
}
}
func (d errorLogging) Println(args ...interface{}) {
if d {
args = append(getFileLine(), args...)
errorLog.Println(args...)
}
}
func (d requestLogging) Printf(format string, args ...interface{}) {
if d {
format = "%s " + format
args = append(getFileLine(), args...)
requestLog.Printf(format, args...)
}
}
func (d responseLogging) Printf(format string, args ...interface{}) {
if d {
format = "%s " + format
args = append(getFileLine(), args...)
responseLog.Printf(format, args...)
}
}
func Fatal(args ...interface{}) {
fmt.Println(args...)
os.Exit(1)
}
func Fatalf(format string, args ...interface{}) {
fmt.Printf(format, args...)
os.Exit(1)
}
func getFileLine() []interface{} {
pc, file, line, ok := runtime.Caller(2)
if !ok {
file = "unknown"
}
fn := runtime.FuncForPC(pc)
pos := strings.LastIndexByte(file, os.PathSeparator)
sfile := file[pos+1:]
return []interface{}{sfile + ":" + strconv.Itoa(line) + " " + fn.Name()}
}