forked from digisan/logkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
185 lines (156 loc) · 4.23 KB
/
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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package logkit
import (
"bufio"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime/debug"
"strings"
"time"
. "github.com/digisan/go-generics"
fd "github.com/digisan/gotk/file-dir"
. "github.com/digisan/gotk/print"
"github.com/digisan/gotk/track"
)
// "defer Track(time.Now)"
func Track(now time.Time) {
tc := track.CallerDescription(1)
fn := strings.Split(tc, "\n")[1]
d := time.Since(now)
if log2C {
item := fSf("\t%s\t%s\t%v\n", M("TRACK"), fn, d)
fPt(nowStr() + item)
}
if log2F {
item := fSf("\t%s\t%s\t%v\n", "TRACK", fn, d)
log.Printf("%s", item)
}
}
// logger : write info into Console OR File
func logger(tl int, cat logCategory, format string, v ...any) {
tc := "\n" + track.CallerDescription(tl) + "\n"
const tcPrefix = "\n\t\t\t\t--> "
const tcPrefix4f = "\n\t\t\t\t\t\t\t\t--> "
const tcPrefix4c = "\n\t\t\t\t\t\t--> "
tc = strings.Replace(tc, "\n", tcPrefix, 2)
ev := Filter(v, func(i int, e any) bool { _, ok := e.(error); return ok && e != nil })
hasErr := len(ev) > 0
clrDesc := mLvlClr[cat](mLvlDesc[cat])
v4c := append([]any{clrDesc}, v...)
clrDesc = mLvlClr[FILE](mLvlDesc[cat])
v4f := append([]any{clrDesc}, v...)
nLF := strings.Count(format, LF)
const lf4f = "\"\n\t\t\t\t\t\t\t\t\""
switch cat {
case INFO:
if log2C {
item := fSf("\t%s\t"+format+"\n", v4c...)
fPt(nowStr() + item)
}
if log2F {
item := fSf("\t%s\t\""+format+"\"\n", v4f...)
item = strings.Replace(item, LF, lf4f, nLF)
log.Printf("%s", item)
}
case DEBUG:
if log2C {
item := fSf("\t%s\t"+format+"%s", append(v4c, B(tc))...)
fPt(nowStr() + item)
}
if log2F {
item := fSf("\t%s\t\""+format+"\"%s", append(v4f, tc)...)
item = strings.Replace(item, LF, lf4f, nLF)
item = strings.Replace(item, tcPrefix, tcPrefix4f, 2)
log.Printf("%s", item)
}
case WARN:
if hasErr {
if !warnDetail {
if log2C {
item := fSf("\t%s\t"+format+"\n", v4c...)
fPt(nowStr() + item)
}
if log2F {
item := fSf("\t%s\t\""+format+"\"\n", v4f...)
item = strings.Replace(item, LF, lf4f, nLF)
log.Printf("%s", item)
}
} else {
if log2C {
item := fSf("\t%s\t"+format+"%s", append(v4c, Y(tc))...)
fPt(nowStr() + item)
}
if log2F {
item := fSf("\t%s\t\""+format+"\"%s", append(v4f, tc)...)
item = strings.Replace(item, LF, lf4f, nLF)
item = strings.Replace(item, tcPrefix, tcPrefix4f, 2)
log.Printf("%s", item)
}
}
}
case FAIL:
if hasErr {
var item string
switch {
case log2C && log2F:
// console
item = fSf("\t%s\t"+format+"%s", append(v4c, R(tc))...)
fPt(nowStr() + item)
// file
item = fSf("\t%s\t\""+format+"\"%s", append(v4f, tc)...)
item = strings.Replace(item, LF, lf4f, nLF)
item = strings.Replace(item, tcPrefix, tcPrefix4f, 2)
log.Printf("%s", item)
case log2C:
item = fSf("\t%s\t"+format+"%s", append(v4c, R(tc))...)
item = strings.Replace(item, LF, longLF, nLF)
item = strings.Replace(item, tcPrefix, tcPrefix4c, 2)
log.Printf("%s", item)
case log2F:
item = fSf("\t%s\t\""+format+"\"%s", append(v4f, tc)...)
item = strings.Replace(item, LF, lf4f, nLF)
item = strings.Replace(item, tcPrefix, tcPrefix4f, 2)
log.Printf("%s", item)
}
/// *** record fatal stack message to file ***
///
fatalDir := "./fatal"
fd.MustCreateDir(fatalDir)
fName := strings.TrimSpace(strings.ReplaceAll(nowStr(), "/", "-"))
fPath := filepath.Join(fatalDir, fName+".log")
f, err := os.OpenFile(fPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
if err != nil {
panic(err)
}
multiWriter := io.MultiWriter(os.Stderr, f)
rd, wr, err := os.Pipe()
if err != nil {
panic(err)
}
os.Stderr = wr
go func() {
scanner := bufio.NewScanner(rd)
for scanner.Scan() {
multiWriter.Write([]byte(scanner.Text() + "\n"))
}
}()
fmt.Fprintln(os.Stderr, item+"\n")
debug.PrintStack()
time.Sleep(time.Duration(1 * time.Second))
/// ***
panic("FAILED!")
}
}
}
// ------------------------------------------------------- //
func Log(format string, v ...any) {
logger(0, INFO, format, v...)
}
// LogWhen : write info into Console OR File
func LogWhen(condition bool, format string, v ...any) {
if condition {
Log(format, v...)
}
}