-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_logger.go
104 lines (94 loc) · 1.89 KB
/
my_logger.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
package log
import (
"fmt"
"io"
"os"
"runtime"
"strings"
"time"
)
var (
DefaultTimeFormat = time.RFC3339
)
type Fields map[string]interface{}
type myLogger struct {
Writer io.Writer
ErrWriter io.Writer
prefix string
begin time.Time
level uint8
data Fields
}
func logPrefix(level uint8) string {
return fmt.Sprintf("time=%q level=%s function=%s", time.Now().Format(DefaultTimeFormat), unmarshalLevel(level), funcName())
}
func (fields Fields) String() string {
if len(fields) == 0 {
return ""
}
var buffer strings.Builder
for k, v := range fields {
switch v := v.(type) {
case string:
buffer.WriteString(fmt.Sprintf(" %s=%q", k, v))
default:
buffer.WriteString(fmt.Sprintf(" %s=%v", k, v))
}
}
return buffer.String()
}
func (l myLogger) log(flag uint8, s ...interface{}) {
var w io.Writer
{
if flag == errorFlag {
w = l.ErrWriter
} else {
w = l.Writer
}
}
f := fmt.Sprintf("%s%s msg=%q%s", logPrefix(flag), l.prefix, fmt.Sprint(s...), l.data)
fmt.Fprintln(w, f)
}
func (l myLogger) logf(flag uint8, format string, s ...interface{}) {
var w io.Writer
{
if flag == errorFlag {
w = l.ErrWriter
} else {
w = l.Writer
}
}
format = fmt.Sprintf("%%s%%s msg=\"%s\"%s\n", format, l.data)
s = append([]interface{}{logPrefix(flag), l.prefix}, s...)
fmt.Fprintf(w, format, s...)
}
func funcName() string {
pc := make([]uintptr, 10)
runtime.Callers(2, pc)
for _, p := range pc {
fn := runtime.FuncForPC(p)
if !strings.HasPrefix(fn.Name(), "github.com/rjbasitali/go-log") {
fname := fn.Name()
i := strings.LastIndexByte(fname, byte('/'))
if i == -1 {
return fname
}
return fname[i+1:]
}
}
return ""
}
func NewLogger(w, errw io.Writer) Logger {
if w == nil {
w = os.Stdout
}
if errw == nil {
errw = os.Stderr
}
return myLogger{
Writer: w,
ErrWriter: errw,
begin: time.Now(),
level: traceFlag,
}
}