0%

【Go】实现分级日志

【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
package log

import (
"errors"
"fmt"
"log"
"os"
)

const (
LevelDebug = (1 + iota) * 10
LevelInfo
LevelWaring
LevelError
LevelCritical
)

var debugLogger *log.Logger
var debugFileLogger *log.Logger
var infoLogger *log.Logger
var infoFileLogger *log.Logger
var warningLogger *log.Logger
var warningFileLogger *log.Logger
var errorLogger *log.Logger
var errorFileLogger *log.Logger
var criticalLogger *log.Logger
var criticalFileLogger *log.Logger
var flag = log.Ldate | log.Ltime | log.Lshortfile | log.Lmsgprefix
var logLevel = LevelInfo

func SetLevel(level int) error {
if level != LevelDebug && level != LevelInfo && level != LevelWaring && level != LevelError && level != LevelCritical {
return errors.New(fmt.Sprintf("invalid level: %d", level))
} else {
logLevel = level
return nil
}
}

func AddFileOutput(filePath string) error {
logWriter, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0655)
if err != nil {
return err
} else {
debugFileLogger = log.New(logWriter, "[DEBUG] ", flag)
infoFileLogger = log.New(logWriter, "[INFO] ", flag)
warningFileLogger = log.New(logWriter, "[WARNING] ", flag)
errorFileLogger = log.New(logWriter, "[ERROR] ", flag)
criticalFileLogger = log.New(logWriter, "[CRITICAL] ", flag)
return nil
}
}

func Debug(format string, v ...any) {
logWithLevel(debugLogger, LevelDebug, format, v)
logWithLevel(debugFileLogger, LevelDebug, format, v)
}

func Info(format string, v ...any) {
logWithLevel(infoLogger, LevelDebug, format, v)
logWithLevel(infoFileLogger, LevelDebug, format, v)
}

func Waring(format string, v ...any) {
logWithLevel(warningLogger, LevelDebug, format, v)
logWithLevel(warningFileLogger, LevelDebug, format, v)
}

func Error(format string, v ...any) {
logWithLevel(errorLogger, LevelDebug, format, v)
logWithLevel(errorFileLogger, LevelDebug, format, v)
}

func Critical(format string, v ...any) {
logWithLevel(criticalLogger, LevelDebug, format, v)
logWithLevel(criticalFileLogger, LevelDebug, format, v)
}

func init() {
debugLogger = log.New(os.Stderr, "[DEBUG] ", flag)
infoLogger = log.New(os.Stderr, "[INFO] ", flag)
warningLogger = log.New(os.Stderr, "[WARNING] ", flag)
errorLogger = log.New(os.Stderr, "[ERROR] ", flag)
criticalLogger = log.New(os.Stderr, "[CRITICAL] ", flag)
}

func logWithLevel(logger *log.Logger, level int, format string, v []any) {
if logger != nil && logLevel >= level {
logger.Printf(format, v...)
}
}

简单使用如下:

1
2
3
4
5
6
7
8
9
10
package main

import (
"GoCode/log"
)

func main() {
log.AddFileOutput("./log.txt")
log.Info("Hello, %s", "log")
}
1
2022/05/30 02:04:01 log.go:89: [INFO] Hello, log