forked from wcharczuk/go-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
148 lines (126 loc) · 3.53 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
package chart
import (
"fmt"
"io"
"os"
"time"
)
var (
_ Logger = (*StdoutLogger)(nil)
)
// NewLogger returns a new logger.
func NewLogger(options ...LoggerOption) Logger {
stl := &StdoutLogger{
TimeFormat: time.RFC3339Nano,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
for _, option := range options {
option(stl)
}
return stl
}
// Logger is a type that implements the logging interface.
type Logger interface {
Info(...interface{})
Infof(string, ...interface{})
Debug(...interface{})
Debugf(string, ...interface{})
Err(error)
FatalErr(error)
Error(...interface{})
Errorf(string, ...interface{})
}
// Info logs an info message if the logger is set.
func Info(log Logger, arguments ...interface{}) {
if log == nil {
return
}
log.Info(arguments...)
}
// Infof logs an info message if the logger is set.
func Infof(log Logger, format string, arguments ...interface{}) {
if log == nil {
return
}
log.Infof(format, arguments...)
}
// Debug logs an debug message if the logger is set.
func Debug(log Logger, arguments ...interface{}) {
if log == nil {
return
}
log.Debug(arguments...)
}
// Debugf logs an debug message if the logger is set.
func Debugf(log Logger, format string, arguments ...interface{}) {
if log == nil {
return
}
log.Debugf(format, arguments...)
}
// LoggerOption mutates a stdout logger.
type LoggerOption = func(*StdoutLogger)
//OptLoggerStdout sets the Stdout writer.
func OptLoggerStdout(wr io.Writer) LoggerOption {
return func(stl *StdoutLogger) {
stl.Stdout = wr
}
}
// OptLoggerStderr sets the Stdout writer.
func OptLoggerStderr(wr io.Writer) LoggerOption {
return func(stl *StdoutLogger) {
stl.Stderr = wr
}
}
// StdoutLogger is a basic logger.
type StdoutLogger struct {
TimeFormat string
Stdout io.Writer
Stderr io.Writer
}
// Info writes an info message.
func (l *StdoutLogger) Info(arguments ...interface{}) {
l.Println(append([]interface{}{"[INFO]"}, arguments...)...)
}
// Infof writes an info message.
func (l *StdoutLogger) Infof(format string, arguments ...interface{}) {
l.Println(append([]interface{}{"[INFO]"}, fmt.Sprintf(format, arguments...))...)
}
// Debug writes an debug message.
func (l *StdoutLogger) Debug(arguments ...interface{}) {
l.Println(append([]interface{}{"[DEBUG]"}, arguments...)...)
}
// Debugf writes an debug message.
func (l *StdoutLogger) Debugf(format string, arguments ...interface{}) {
l.Println(append([]interface{}{"[DEBUG]"}, fmt.Sprintf(format, arguments...))...)
}
// Error writes an error message.
func (l *StdoutLogger) Error(arguments ...interface{}) {
l.Println(append([]interface{}{"[ERROR]"}, arguments...)...)
}
// Errorf writes an error message.
func (l *StdoutLogger) Errorf(format string, arguments ...interface{}) {
l.Println(append([]interface{}{"[ERROR]"}, fmt.Sprintf(format, arguments...))...)
}
// Err writes an error message.
func (l *StdoutLogger) Err(err error) {
if err != nil {
l.Println(append([]interface{}{"[ERROR]"}, err.Error())...)
}
}
// FatalErr writes an error message and exits.
func (l *StdoutLogger) FatalErr(err error) {
if err != nil {
l.Println(append([]interface{}{"[FATAL]"}, err.Error())...)
os.Exit(1)
}
}
// Println prints a new message.
func (l *StdoutLogger) Println(arguments ...interface{}) {
fmt.Fprintln(l.Stdout, append([]interface{}{time.Now().UTC().Format(l.TimeFormat)}, arguments...)...)
}
// Errorln prints a new message.
func (l *StdoutLogger) Errorln(arguments ...interface{}) {
fmt.Fprintln(l.Stderr, append([]interface{}{time.Now().UTC().Format(l.TimeFormat)}, arguments...)...)
}