-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlog.go
More file actions
116 lines (100 loc) · 2.88 KB
/
Copy pathlog.go
File metadata and controls
116 lines (100 loc) · 2.88 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
package trygo
import (
"errors"
"fmt"
"runtime"
"strings"
"time"
)
type LoggerInterface interface {
Debug(arg0 interface{}, args ...interface{})
Info(arg0 interface{}, args ...interface{})
Warn(arg0 interface{}, args ...interface{}) error
Error(arg0 interface{}, args ...interface{}) error
Critical(arg0 interface{}, args ...interface{}) error
}
var Logger LoggerInterface
func init() {
Logger = &defaultLogger{}
}
type defaultLogger struct{}
func (l *defaultLogger) Write(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
if p[len(p)-1] == '\n' {
p = p[0 : len(p)-1]
}
fmt.Printf("%s [LOG] %s %s\n", formatNow(), determine(5), string(p))
return len(p), nil
}
func (l *defaultLogger) Printf(format string, args ...interface{}) {
fmt.Printf("%s [LOG] %s %s\n", formatNow(), determine(3), fmt.Sprintf(format, args...))
}
func (l *defaultLogger) Debug(arg0 interface{}, args ...interface{}) {
switch f := arg0.(type) {
case string:
l.log("DBG", f, args...)
default:
l.log("DBG", fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
}
}
func (l *defaultLogger) Info(arg0 interface{}, args ...interface{}) {
switch f := arg0.(type) {
case string:
l.log("INF", f, args...)
default:
l.log("INF", fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
}
}
func (l *defaultLogger) Warn(arg0 interface{}, args ...interface{}) error {
switch f := arg0.(type) {
case string:
return errors.New(l.log("WRN", f, args...))
default:
return errors.New(l.log("WRN", fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...))
}
}
func (l *defaultLogger) Error(arg0 interface{}, args ...interface{}) error {
switch f := arg0.(type) {
case string:
return errors.New(l.log("ERR", f, args...))
default:
return errors.New(l.log("ERR", fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...))
}
}
func (l *defaultLogger) Critical(arg0 interface{}, args ...interface{}) error {
switch f := arg0.(type) {
case string:
return errors.New(l.log("CRT", f, args...))
default:
return errors.New(l.log("CRT", fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...))
}
}
func (l *defaultLogger) log(lvl string, format string, args ...interface{}) (msg string) {
msg = fmt.Sprintf(format, args...)
fmt.Printf("%s [%s] %s %s\n", formatNow(), lvl, determine(3), msg)
return
}
func formatNow() string {
return time.Now().Format("2006-01-02 15:04:05.999")
}
func determine(skip int) string {
pc, file, lineno, ok := runtime.Caller(skip)
src := ""
if ok {
name := runtime.FuncForPC(pc).Name()
nameitems := strings.Split(name, ".")
if len(nameitems) > 2 {
nameitems = nameitems[len(nameitems)-2:]
}
name = strings.Join(nameitems, ".")
pathitems := strings.Split(file, "/")
if len(pathitems) > 2 {
pathitems = pathitems[len(pathitems)-2:]
}
file = strings.Join(pathitems, "/")
src = fmt.Sprintf("%s:%d(%s)", file, lineno, name)
}
return src
}