-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
52 lines (42 loc) · 1.21 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
package easytcp
import (
"fmt"
"io/ioutil"
"log"
"os"
)
var _ Logger = &DefaultLogger{}
// Log is the instance of Logger interface.
var Log Logger = newMuteLogger()
// Logger is the generic interface for log recording.
type Logger interface {
Errorf(format string, args ...interface{})
Tracef(format string, args ...interface{})
}
func newLogger() *DefaultLogger {
return &DefaultLogger{
rawLogger: log.New(os.Stdout, "easytcp ", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lmsgprefix),
}
}
func newMuteLogger() *DefaultLogger {
return &DefaultLogger{
rawLogger: log.New(ioutil.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...))
}
// SetLogger sets the package logger.
func SetLogger(lg Logger) {
Log = lg
}