-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
132 lines (113 loc) · 3.2 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
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
// Copyright 2020 lesismal. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package logging
import (
"fmt"
"os"
"time"
)
var (
// TimeFormat is used to format time parameters.
TimeFormat = "2006/01/02 15:04:05.000"
// Output is used to receive log output.
Output = os.Stdout
// DefaultLogger is the default logger and is used by arpc.
DefaultLogger Logger = &logger{level: LevelInfo}
)
const (
// LevelAll enables all logs.
LevelAll = iota
// LevelDebug logs are usually disabled in production.
LevelDebug
// LevelInfo is the default logging priority.
LevelInfo
// LevelWarn .
LevelWarn
// LevelError .
LevelError
// LevelNone disables all logs.
LevelNone
)
// Logger defines log interface.
type Logger interface {
SetLevel(lvl int)
Debug(format string, v ...interface{})
Info(format string, v ...interface{})
Warn(format string, v ...interface{})
Error(format string, v ...interface{})
}
// SetLogger sets default logger.
func SetLogger(l Logger) {
DefaultLogger = l
}
// SetLevel sets default logger's priority.
func SetLevel(lvl int) {
switch lvl {
case LevelAll, LevelDebug, LevelInfo, LevelWarn, LevelError, LevelNone:
DefaultLogger.SetLevel(lvl)
default:
fmt.Fprintf(Output, "invalid log level: %v", lvl)
}
}
// logger implements Logger and is used in arpc by default.
type logger struct {
level int
}
// SetLevel sets logs priority.
func (l *logger) SetLevel(lvl int) {
switch lvl {
case LevelAll, LevelDebug, LevelInfo, LevelWarn, LevelError, LevelNone:
l.level = lvl
default:
fmt.Fprintf(Output, "invalid log level: %v", lvl)
}
}
// Debug uses fmt.Printf to log a message at LevelDebug.
func (l *logger) Debug(format string, v ...interface{}) {
if LevelDebug >= l.level {
fmt.Fprintf(Output, time.Now().Format(TimeFormat)+" [DBG] "+format+"\n", v...)
}
}
// Info uses fmt.Printf to log a message at LevelInfo.
func (l *logger) Info(format string, v ...interface{}) {
if LevelInfo >= l.level {
fmt.Fprintf(Output, time.Now().Format(TimeFormat)+" [INF] "+format+"\n", v...)
}
}
// Warn uses fmt.Printf to log a message at LevelWarn.
func (l *logger) Warn(format string, v ...interface{}) {
if LevelWarn >= l.level {
fmt.Fprintf(Output, time.Now().Format(TimeFormat)+" [WRN] "+format+"\n", v...)
}
}
// Error uses fmt.Printf to log a message at LevelError.
func (l *logger) Error(format string, v ...interface{}) {
if LevelError >= l.level {
fmt.Fprintf(Output, time.Now().Format(TimeFormat)+" [ERR] "+format+"\n", v...)
}
}
// Debug uses DefaultLogger to log a message at LevelDebug.
func Debug(format string, v ...interface{}) {
if DefaultLogger != nil {
DefaultLogger.Debug(format, v...)
}
}
// Info uses DefaultLogger to log a message at LevelInfo.
func Info(format string, v ...interface{}) {
if DefaultLogger != nil {
DefaultLogger.Info(format, v...)
}
}
// Warn uses DefaultLogger to log a message at LevelWarn.
func Warn(format string, v ...interface{}) {
if DefaultLogger != nil {
DefaultLogger.Warn(format, v...)
}
}
// Error uses DefaultLogger to log a message at LevelError.
func Error(format string, v ...interface{}) {
if DefaultLogger != nil {
DefaultLogger.Error(format, v...)
}
}