-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
138 lines (119 loc) · 3 KB
/
logger.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
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
/*
* Copyright 2022 Yann Dumont
*
* 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 log_level
import (
"fmt"
"github.com/y-du/go-log-level/level"
"log"
)
type Logger struct {
level level.Level
pLevel level.Level
ePrefix string
wPrefix string
iPrefix string
dPrefix string
*log.Logger
}
func New(logLogger *log.Logger, loggerLevel level.Level) (l *Logger, err error) {
if ok := checkLevel(loggerLevel); !ok {
err = fmt.Errorf("unknown level '%d': defaulting to '%s'", loggerLevel, level.Default)
loggerLevel = level.Default
}
l = &Logger{
level: loggerLevel,
pLevel: -1,
Logger: logLogger,
}
return
}
func (l *Logger) SetLevelPrefix(error, warning, info, debug string) {
l.ePrefix = error
l.wPrefix = warning
l.iPrefix = info
l.dPrefix = debug
}
func (l *Logger) Printf(format string, v ...interface{}) {
if l.pLevel <= l.level {
l.Output(3, fmt.Sprintf(format, v...))
}
}
func (l *Logger) Print(v ...interface{}) {
if l.pLevel <= l.level {
l.Output(3, fmt.Sprint(v...))
}
}
func (l *Logger) Println(v ...interface{}) {
if l.pLevel <= l.level {
l.Output(3, fmt.Sprint(v...))
}
}
func (l *Logger) Error(v ...interface{}) {
if level.Error <= l.level {
l.Output(3, l.ePrefix+fmt.Sprint(v...))
}
}
func (l *Logger) Errorf(format string, v ...interface{}) {
if level.Error <= l.level {
l.Output(3, l.ePrefix+fmt.Sprintf(format, v...))
}
}
func (l *Logger) Warning(v ...interface{}) {
if level.Warning <= l.level {
l.Output(3, l.wPrefix+fmt.Sprint(v...))
}
}
func (l *Logger) Warningf(format string, v ...interface{}) {
if level.Warning <= l.level {
l.Output(3, l.wPrefix+fmt.Sprintf(format, v...))
}
}
func (l *Logger) Info(v ...interface{}) {
if level.Info <= l.level {
l.Output(3, l.iPrefix+fmt.Sprint(v...))
}
}
func (l *Logger) Infof(format string, v ...interface{}) {
if level.Info <= l.level {
l.Output(3, l.iPrefix+fmt.Sprintf(format, v...))
}
}
func (l *Logger) Debug(v ...interface{}) {
if level.Debug <= l.level {
l.Output(3, l.dPrefix+fmt.Sprint(v...))
}
}
func (l *Logger) Debugf(format string, v ...interface{}) {
if level.Debug <= l.level {
l.Output(3, l.dPrefix+fmt.Sprintf(format, v...))
}
}
func (l *Logger) GetLevel() level.Level {
return l.level
}
func (l *Logger) SetPrintLevel(level level.Level) (err error) {
if ok := checkLevel(level); !ok {
return fmt.Errorf("unknown level '%d'", level)
}
l.pLevel = level
return
}
func checkLevel(l level.Level) bool {
if l >= level.Off && l <= level.Debug {
return true
}
return false
}