-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
108 lines (101 loc) · 3.11 KB
/
stream.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
// Copyright (c) 2020 HigKer
// Open Source: MIT License
// Author: SDing <deen.job@qq.com>
// Date: 2020/4/16 - 10:59 下午
package logker
// STREAM v1.0.6
// Format console and file log information.
// IO read write operation.
import (
"fmt"
"github.com/fatih/color"
)
const (
//[INFO] 2006-01-02 13:05.0006 MP - Position: test.go|main.test:21 - Message: news
format = "[%s] - Date: %s %s - Message: %s"
fileFormat = format + "\n"
)
// Logging record
type logRecord interface {
outPutMessage(v string) // Record logging msg.
outPutErrMessage(v string)
}
func (c *console) outPutMessage(model level, v string) {
switch model.toStr() {
case DEBUG.toStr():
// blue color of log message.
// format log message output console.
color.Blue(format, DEBUG.toStr(), c.tz.NowTimeStr(), buildCallerStr(SKIP), v)
case INFO.toStr():
color.Green(format, INFO.toStr(), c.tz.NowTimeStr(), buildCallerStr(SKIP), v)
case WARNING.toStr():
color.Yellow(format, WARNING.toStr(), c.tz.NowTimeStr(), buildCallerStr(SKIP), v)
case ERROR.toStr():
color.Red(format, ERROR.toStr(), c.tz.NowTimeStr(), buildCallerStr(SKIP), v)
default:
// Log Level Type Error
// Program automatically set to debug
color.Red("-----------------------------------------------------------------")
color.Red("!!!Log Level Type Error,Program automatically set to debug!!!")
color.Red("-----------------------------------------------------------------")
c.logLevel = DEBUG
// recursion
c.outPutMessage(model, v)
}
}
func (f *fileLog) outPutMessage(model level, v string) {
switch model.toStr() {
case DEBUG.toStr():
f.outPut(DEBUG, v)
case INFO.toStr():
f.outPut(INFO, v)
case WARNING.toStr():
f.outPut(WARNING, v)
case ERROR.toStr():
//f.outPutErr(ERROR, v)
f.outPut(ERROR, v)
default:
// Log Level Type Error
// Program automatically set to debug
f.logLevel = DEBUG
f.Error("Log Level Type Error! Program automatically set to debug!!!")
// recursion
f.outPutMessage(model, v)
}
}
func (f *fileLog) outPut(lev level, v string) {
_, err := f.file.WriteString(fmt.Sprintf(fileFormat, lev.toStr(), f.tz.NowTimeStr(), buildCallerStr(SKIP+1), v))
_ = f.file.Sync()
if err != nil {
_ = f.file.Close()
panic("output message to log file fail. filePath:" + f.directory + "/" + f.fileName + ".log")
}
//fmt.Println(n)
}
func (f *fileLog) outPutErrMessage(model level, v string) {
switch model.toStr() {
case DEBUG.toStr():
f.outPutErr(DEBUG, v)
case INFO.toStr():
f.outPutErr(INFO, v)
case WARNING.toStr():
f.outPutErr(WARNING, v)
case ERROR.toStr():
f.outPutErr(ERROR, v)
default:
// Log Level Type Error
// Program automatically set to debug
f.logLevel = DEBUG
f.Error("Log Level Type Error! Program automatically set to debug!!!")
// recursion
f.outPutErrMessage(model, v)
}
}
func (f *fileLog) outPutErr(model level, v string) {
_, err := f.errFile.WriteString(fmt.Sprintf(fileFormat, model.toStr(), f.tz.NowTimeStr(), buildCallerStr(SKIP+1), v))
_ = f.errFile.Sync()
if err != nil {
_ = f.errFile.Close()
panic("output message to log file fail. filePath:" + f.directory + "/" + f.fileName + ".log")
}
}