Debian Codegraph @

logger.go

raw symbols SCIP
1 package chart
2
3 import (
4 "fmt"
5 "io"
6 "os"
7 "time"
8 )
9
10 var (
11 _ Logger = (*StdoutLogger)(nil)
12 )
13
14 // NewLogger returns a new logger.
15 func NewLogger(options ...LoggerOption) Logger {
16 stl := &StdoutLogger{
17 TimeFormat: time.RFC3339Nano,
18 Stdout: os.Stdout,
19 Stderr: os.Stderr,
20 }
21 for _, option := range options {
22 option(stl)
23 }
24 return stl
25 }
26
27 // Logger is a type that implements the logging interface.
28 type Logger interface {
29 Info(...interface{})
30 Infof(string, ...interface{})
31 Debug(...interface{})
32 Debugf(string, ...interface{})
33 Err(error)
34 FatalErr(error)
35 Error(...interface{})
36 Errorf(string, ...interface{})
37 }
38
39 // Info logs an info message if the logger is set.
40 func Info(log Logger, arguments ...interface{}) {
41 if log == nil {
42 return
43 }
44 log.Info(arguments...)
45 }
46
47 // Infof logs an info message if the logger is set.
48 func Infof(log Logger, format string, arguments ...interface{}) {
49 if log == nil {
50 return
51 }
52 log.Infof(format, arguments...)
53 }
54
55 // Debug logs an debug message if the logger is set.
56 func Debug(log Logger, arguments ...interface{}) {
57 if log == nil {
58 return
59 }
60 log.Debug(arguments...)
61 }
62
63 // Debugf logs an debug message if the logger is set.
64 func Debugf(log Logger, format string, arguments ...interface{}) {
65 if log == nil {
66 return
67 }
68 log.Debugf(format, arguments...)
69 }
70
71 // LoggerOption mutates a stdout logger.
72 type LoggerOption = func(*StdoutLogger)
73
74 //OptLoggerStdout sets the Stdout writer.
75 func OptLoggerStdout(wr io.Writer) LoggerOption {
76 return func(stl *StdoutLogger) {
77 stl.Stdout = wr
78 }
79 }
80
81 // OptLoggerStderr sets the Stdout writer.
82 func OptLoggerStderr(wr io.Writer) LoggerOption {
83 return func(stl *StdoutLogger) {
84 stl.Stderr = wr
85 }
86 }
87
88 // StdoutLogger is a basic logger.
89 type StdoutLogger struct {
90 TimeFormat string
91 Stdout io.Writer
92 Stderr io.Writer
93 }
94
95 // Info writes an info message.
96 func (l *StdoutLogger) Info(arguments ...interface{}) {
97 l.Println(append([]interface{}{"[INFO]"}, arguments...)...)
98 }
99
100 // Infof writes an info message.
101 func (l *StdoutLogger) Infof(format string, arguments ...interface{}) {
102 l.Println(append([]interface{}{"[INFO]"}, fmt.Sprintf(format, arguments...))...)
103 }
104
105 // Debug writes an debug message.
106 func (l *StdoutLogger) Debug(arguments ...interface{}) {
107 l.Println(append([]interface{}{"[DEBUG]"}, arguments...)...)
108 }
109
110 // Debugf writes an debug message.
111 func (l *StdoutLogger) Debugf(format string, arguments ...interface{}) {
112 l.Println(append([]interface{}{"[DEBUG]"}, fmt.Sprintf(format, arguments...))...)
113 }
114
115 // Error writes an error message.
116 func (l *StdoutLogger) Error(arguments ...interface{}) {
117 l.Println(append([]interface{}{"[ERROR]"}, arguments...)...)
118 }
119
120 // Errorf writes an error message.
121 func (l *StdoutLogger) Errorf(format string, arguments ...interface{}) {
122 l.Println(append([]interface{}{"[ERROR]"}, fmt.Sprintf(format, arguments...))...)
123 }
124
125 // Err writes an error message.
126 func (l *StdoutLogger) Err(err error) {
127 if err != nil {
128 l.Println(append([]interface{}{"[ERROR]"}, err.Error())...)
129 }
130 }
131
132 // FatalErr writes an error message and exits.
133 func (l *StdoutLogger) FatalErr(err error) {
134 if err != nil {
135 l.Println(append([]interface{}{"[FATAL]"}, err.Error())...)
136 os.Exit(1)
137 }
138 }
139
140 // Println prints a new message.
141 func (l *StdoutLogger) Println(arguments ...interface{}) {
142 fmt.Fprintln(l.Stdout, append([]interface{}{time.Now().UTC().Format(l.TimeFormat)}, arguments...)...)
143 }
144
145 // Errorln prints a new message.
146 func (l *StdoutLogger) Errorln(arguments ...interface{}) {
147 fmt.Fprintln(l.Stderr, append([]interface{}{time.Now().UTC().Format(l.TimeFormat)}, arguments...)...)
148 }