-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
51 lines (44 loc) · 988 Bytes
/
log.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
package main
import (
"fmt"
"log"
"os"
"time"
)
type MyLog struct {
use bool
info *log.Logger
warn *log.Logger
panic *log.Logger
}
func NewMyLog(use bool) *MyLog {
info := log.New(os.Stdout, fmt.Sprintf("[Info ][%s]", time.Now().Format("2006-01-02 15:04:05")), log.Lmsgprefix)
pic := log.New(os.Stdout, fmt.Sprintf("[Panic][%s]", time.Now().Format("2006-01-02 15:04:05")), log.Lmsgprefix)
warn := log.New(os.Stdout, fmt.Sprintf("[Warn ][%s]", time.Now().Format("2006-01-02 15:04:05")), log.Lmsgprefix)
return &MyLog{
use: use,
info: info,
panic: pic,
warn: warn,
}
}
func (l MyLog) Info(msg ...string) {
if l.use {
m := fmt.Sprint(msg)
l.info.Printf("\u001B[32m%s\u001B[0m", m)
}
}
func (l MyLog) Warn(msg ...string) {
if l.use {
m := fmt.Sprint(msg)
l.warn.Printf("\u001B[31m%s\u001B[0m", m)
}
}
func (l MyLog) Panic(msg ...string) {
m := fmt.Sprint(msg)
if l.use {
l.panic.Printf("\u001B[33m%s\u001B[0m", m)
} else {
panic(m)
}
}