-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.go
50 lines (40 loc) · 932 Bytes
/
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 main
import (
"fmt"
"log"
"os"
)
type LogLevel int
const (
Error LogLevel = 0
Info LogLevel = 1
Verbose LogLevel = 2
)
type Logger struct {
Printf func(format string, v ...interface{})
LogLevel LogLevel
}
func (l *Logger) Fatalf(format string, v ...interface{}) {
l.Printf(format, v...)
os.Exit(1)
}
func (l *Logger) Logf(logLevel LogLevel, format string, v ...interface{}) {
if l.LogLevel >= logLevel {
l.Printf(format, v...)
}
}
func (l *Logger) Errorf(format string, v ...interface{}) {
l.Logf(Error, format, v...)
}
func (l *Logger) Infof(format string, v ...interface{}) {
l.Logf(Info, format, v...)
}
func (l *Logger) Verbosef(format string, v ...interface{}) {
l.Logf(Verbose, format, v...)
}
func CreateLogger(prefix string, logLevel LogLevel) *Logger {
return &Logger{
Printf: log.New(os.Stderr, fmt.Sprintf("[%s] ", prefix), log.LstdFlags).Printf,
LogLevel: logLevel,
}
}