-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog2.go
More file actions
250 lines (200 loc) · 5.42 KB
/
log2.go
File metadata and controls
250 lines (200 loc) · 5.42 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package goglib
import (
"errors"
"fmt"
"io"
"log"
"os"
"sync"
"time"
)
type OLog2 struct {
name string
rootDir string
stamp string
level int
fileWrite bool
fp *os.File
fileErr error
path string
}
var lgMutex = &sync.Mutex{}
// InitLogEnv initialises a logger. The optional filewrite parameter (default true)
// controls whether log output is also written to a daily-rotated file.
// Existing callers without the argument continue to work unchanged.
func InitLogEnv(rootPath string, name string, level int, filewrite ...bool) *OLog2 {
lg := OLog2{}
lg.rootDir = rootPath
lg.name = name
lg.level = level
lg.fileWrite = len(filewrite) == 0 || filewrite[0]
if lg.fileWrite {
lg.path = lg.getFilePath()
lg.fp, lg.fileErr = lg.fileopen(lg.path)
}
return &lg
}
func (lg *OLog2) fileopen(path string) (*os.File, error) {
if !lg.fileWrite {
return nil, nil
}
if lg.fp != nil {
lg.Fileclose()
}
lgMutex.Lock()
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
lg.fp = nil
log.Println("Failed to open info log file : ", err)
lg.fileErr = err
} else {
log.Println("file open : ", path)
}
lgMutex.Unlock()
return file, err
}
func (lg *OLog2) Fileclose() {
lgMutex.Lock()
//fmt.Printf("file close..(1) [%s]\n", lg.path)
lg.fp.Close()
//lg.fp = nil
//fmt.Printf("file close..(2) [%s]\n", lg.path)
lgMutex.Unlock()
}
func (lg *OLog2) SetLevel(level int) {
lg.level = level
}
func (lg *OLog2) GetLevel() int {
return (lg.level)
}
func (lg OLog2) getFilePath() string {
// log 패키지에서 시간 찍음
year, month, day := time.Now().Date()
filePath := fmt.Sprintf("%02d%02d%02d", year, int(month), day)
dirPath := lg.rootDir + "/" + filePath
// log directory 존재 확인
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
// root dir 생성
os.Mkdir(dirPath, os.ModePerm)
}
path := lg.rootDir + "/" + filePath + "/" + lg.name + "-" + filePath + ".log"
//return filePath
return path
}
func (lg OLog2) checkFilePath(path string) bool {
var isok bool = true
_, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
log.Printf("File does not exist[%s]", path)
isok = false
}
return isok
}
func (lg OLog2) checkLevel(level int) bool {
if lg.level > level {
return false
}
return true
}
func (lg *OLog2) logging2(lv int, stamp string, format string, v ...interface{}) {
if !lg.checkLevel(lv) {
return
}
stamp2 := fmt.Sprintf("%s[%s]", stamp, lg.name)
format += "\n"
if !lg.fileWrite {
logger := log.New(os.Stderr, stamp2, log.Ldate|log.Ltime|log.Lmicroseconds)
logger.Printf(format, v...)
return
}
lg.path = lg.getFilePath()
lgMutex.Lock()
if !lg.checkFilePath(lg.path) {
var err error
lg.fp, err = os.OpenFile(lg.path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
lg.fileErr = err
log.Printf("file fd error..%v \n", err)
lgMutex.Unlock()
return
}
lg.fileErr = nil
}
if lg.fileErr != nil {
log.Printf("file fd error..%v \n", lg.fileErr)
lgMutex.Unlock()
return
}
logger := log.New(io.MultiWriter(lg.fp, os.Stderr), stamp2, log.Ldate|log.Ltime|log.Lmicroseconds)
lgMutex.Unlock()
logger.Printf(format, v...)
}
func (lg OLog2) logging(lv int, stamp string, format string, v ...interface{}) {
// check level
if !lg.checkLevel(lv) {
return
}
filePath := lg.getFilePath()
path := lg.rootDir + "/" + filePath + "/" + lg.name + "-" + filePath + ".log"
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open info log file : ", err)
return
}
defer func() {
file.Close()
}()
var logger *log.Logger
// logger = log.New(io.MultiWriter(file,os.Stderr),
// stamp,
// log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
logger = log.New(io.MultiWriter(file, os.Stderr),
stamp,
log.Ldate|log.Ltime|log.Lmicroseconds)
format += "\n"
logger.Printf(format, v...)
}
func (lg *OLog2) Always(format string, v ...interface{}) {
stamp := "ALWAYS : "
lg.logging2(99, stamp, format, v...)
}
func (lg *OLog2) Info(format string, v ...interface{}) {
stamp := "INFO : "
lg.logging2(INFO, stamp, format, v...)
}
func (lg *OLog2) Print(level int, format string, v ...interface{}) {
stamp := "PRINT : "
lg.logging2(level, stamp, format, v...)
}
func (lg *OLog2) Debug(format string, v ...interface{}) {
stamp := "DEBUG : "
lg.logging2(DEBUG, stamp, format, v...)
}
func (lg *OLog2) DebugDump(level int, format string, v ...interface{}) {
stamp := "DUMP : "
lg.logging2(level, stamp, format, v...)
}
func (lg *OLog2) Warn(format string, v ...interface{}) {
stamp := "WARN : "
lg.logging2(WARN, stamp, format, v...)
}
func (lg *OLog2) Error(format string, v ...interface{}) {
stamp := "ERROR : "
lg.logging2(ERROR, stamp, format, v...)
}
func (lg *OLog2) Dump(level int, stamp string, bytes []byte, length int) {
header_msg := fmt.Sprintf(" %s [%d]\n", stamp, length)
var message string = ""
for idx := 0; idx < length; idx++ {
if idx%20 == 0 {
if idx != 0 {
message = message + "\n"
}
message += " "
}
message += fmt.Sprintf(" %02X", bytes[idx])
}
message += "\n"
logmsg := header_msg + message
lg.DebugDump(level, logmsg)
}