This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
162 lines (135 loc) · 4.66 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright © 2020 Hedzr Yeh.
// Package log provide the standard interface of logging for what any go
// libraries want strip off the direct dependency from a known logging
// library.
package log
import (
"io"
"log"
)
type (
// SystemdLogger writes to the system log.
SystemdLogger interface {
Error(v ...interface{}) error
Warning(v ...interface{}) error
Info(v ...interface{}) error
Errorf(format string, a ...interface{}) error
Warningf(format string, a ...interface{}) error
Infof(format string, a ...interface{}) error
}
// SL provides a structural logging interface
SL interface {
With(key string, val interface{}) Logger
WithFields(fields map[string]interface{}) Logger
}
// L provides a basic logger interface
L interface {
// Trace prints all args to stdin if logging level is greater than TraceLevel
Trace(args ...interface{})
// Debug prints all args to stdin if logging level is greater than DebugLevel
Debug(args ...interface{})
// Info prints all args to stdin if logging level is greater than InfoLevel
Info(args ...interface{})
// Warn prints all args to stderr
Warn(args ...interface{})
// Error prints all args to stderr
Error(args ...interface{})
// Fatal is equivalent to Printf() followed by a call to os.Exit(1).
Fatal(args ...interface{})
// Panic is equivalent to Printf() followed by a call to panic().
Panic(args ...interface{})
// Print calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Print.
Print(args ...interface{})
// Println calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Println.
Println(args ...interface{})
}
// LF provides a L logger interface and format prototypes (such as Debugf...)
LF interface {
SL
// Tracef prints the text to stdin if logging level is greater than TraceLevel
Tracef(msg string, args ...interface{})
// Debugf prints the text to stdin if logging level is greater than DebugLevel
Debugf(msg string, args ...interface{})
// Infof prints the text to stdin if logging level is greater than InfoLevel
Infof(msg string, args ...interface{})
// Warnf prints the text to stderr
Warnf(msg string, args ...interface{})
// Errorf prints the text to stderr
Errorf(msg string, args ...interface{})
// Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
Fatalf(msg string, args ...interface{})
// Panicf is equivalent to Printf() followed by a call to panic().
Panicf(msg string, args ...interface{})
// Printf calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Printf.
Printf(msg string, args ...interface{})
}
// Logger is a minimal logger with no more dependencies
Logger interface {
LF
// SetLevel sets the logging level
SetLevel(lvl Level)
// GetLevel returns the current logging level
GetLevel() Level
// SetOutput setup the logging output device
SetOutput(out io.Writer)
// GetOutput returns the current logging output device
GetOutput() (out io.Writer)
// Setup will be invoked once an instance created
Setup()
// AddSkip adds an extra count to skip stack frames
AddSkip(skip int) Logger
// AsFieldLogger() FieldLogger
}
// LoggerExt is a minimal logger with no more dependencies
LoggerExt interface {
L
Logger
}
// BuilderFunc provides a function prototype for creating a hedzr/log & hedzr/logex -compliant creator.
BuilderFunc func(config *LoggerConfig) (logger Logger)
)
// AsL converts a logger to L type (with Info(...), ... prototypes)
func AsL(logger LF) L {
if l, ok := logger.(L); ok {
// if l1, ok := l.(Logger); ok {
// return l1.AddSkip(1).(L)
// }
return l
}
return nil
}
// AsLogger converts a logger to LF or Logger type (with Infof(...), ... prototypes)
func AsLogger(logger L) Logger {
if l, ok := logger.(Logger); ok {
return l // .AddSkip(1)
}
return nil
}
// SetLevel sets the logging level
func SetLevel(l Level) { logger.SetLevel(l) }
// Setup _
func Setup() {
logger.Setup()
}
// GetLevel returns the current logging level
func GetLevel() Level { return logger.GetLevel() }
// SetOutput setup the logging output device
func SetOutput(w io.Writer) { logger.SetOutput(w) }
// GetOutput return the logging output device
func GetOutput() (w io.Writer) { return logger.GetOutput() }
// SetLogger transfer an instance into log package-level value
func SetLogger(l Logger) {
l.SetLevel(logger.GetLevel())
logger = l
log.SetOutput(l.GetOutput())
}
// GetLogger returns the package-level logger globally
func GetLogger() Logger { return logger }
// Skip ignore some extra caller frames
func Skip(skip int) Logger {
return logger.AddSkip(skip)
// return logger
}