-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
104 lines (93 loc) · 2.69 KB
/
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
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
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hlog
import (
"context"
"fmt"
"io"
)
// FormatLogger is a logger interface that output logs with a format.
type FormatLogger interface {
Tracef(format string, v ...interface{})
Debugf(format string, v ...interface{})
Infof(format string, v ...interface{})
Noticef(format string, v ...interface{})
Warnf(format string, v ...interface{})
Errorf(format string, v ...interface{})
Fatalf(format string, v ...interface{})
}
// Logger is a logger interface that provides logging function with levels.
type Logger interface {
Trace(v ...interface{})
Debug(v ...interface{})
Info(v ...interface{})
Notice(v ...interface{})
Warn(v ...interface{})
Error(v ...interface{})
Fatal(v ...interface{})
}
// CtxLogger is a logger interface that accepts a context argument and output
// logs with a format.
type CtxLogger interface {
CtxTracef(ctx context.Context, format string, v ...interface{})
CtxDebugf(ctx context.Context, format string, v ...interface{})
CtxInfof(ctx context.Context, format string, v ...interface{})
CtxNoticef(ctx context.Context, format string, v ...interface{})
CtxWarnf(ctx context.Context, format string, v ...interface{})
CtxErrorf(ctx context.Context, format string, v ...interface{})
CtxFatalf(ctx context.Context, format string, v ...interface{})
}
// Control provides methods to config a logger.
type Control interface {
SetLevel(Level)
SetOutput(io.Writer)
}
// FullLogger is the combination of Logger, FormatLogger, CtxLogger and Control.
type FullLogger interface {
Logger
FormatLogger
CtxLogger
Control
}
// Level defines the priority of a log message.
// When a logger is configured with a level, any log message with a lower
// log level (smaller by integer comparison) will not be output.
type Level int
// The levels of logs.
const (
LevelTrace Level = iota
LevelDebug
LevelInfo
LevelNotice
LevelWarn
LevelError
LevelFatal
)
var strs = []string{
"[Trace] ",
"[Debug] ",
"[Info] ",
"[Notice] ",
"[Warn] ",
"[Error] ",
"[Fatal] ",
}
func (lv Level) toString() string {
if lv >= LevelTrace && lv <= LevelFatal {
return strs[lv]
}
return fmt.Sprintf("[?%d] ", lv)
}