-
Notifications
You must be signed in to change notification settings - Fork 88
/
logger.go
50 lines (40 loc) · 1.11 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
package easytcp
import (
"fmt"
"io"
"log"
)
var _ Logger = &DefaultLogger{}
// _log is the instance of Logger interface.
var _log Logger = newDiscardLogger()
// Logger is the generic interface for log recording.
type Logger interface {
Errorf(format string, args ...interface{})
Tracef(format string, args ...interface{})
}
func newDiscardLogger() *DefaultLogger {
return &DefaultLogger{
rawLogger: log.New(io.Discard, "easytcp", log.LstdFlags),
}
}
// DefaultLogger is the default logger instance for this package.
// DefaultLogger uses the built-in log.Logger.
type DefaultLogger struct {
rawLogger *log.Logger
}
// Errorf implements Logger Errorf method.
func (d *DefaultLogger) Errorf(format string, args ...interface{}) {
d.rawLogger.Printf("[ERROR] %s", fmt.Sprintf(format, args...))
}
// Tracef implements Logger Tracef method.
func (d *DefaultLogger) Tracef(format string, args ...interface{}) {
d.rawLogger.Printf("[TRACE] %s", fmt.Sprintf(format, args...))
}
// Log returns the package logger.
func Log() Logger {
return _log
}
// SetLogger sets the package logger.
func SetLogger(lg Logger) {
_log = lg
}