-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
68 lines (58 loc) · 1.28 KB
/
log.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
package ovo
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
)
// LogLevel is type level of log.
type LogLevel int8
// Available options for LogLevel.
const (
NoLog LogLevel = iota
LogError
LogInfo
LogDebug
)
// Logger is logging interface.
type Logger interface {
Debug(format string, args ...interface{})
Info(format string, args ...interface{})
Error(format string, args ...interface{})
}
type logger struct {
level LogLevel
}
func defaultLogger(level LogLevel) *logger {
return &logger{
level: level,
}
}
// Debug to print debug log.
func (l *logger) Debug(format string, args ...interface{}) {
if l.level >= LogDebug {
fmt.Fprintf(os.Stdout, "[D] "+format+"\n", args...)
}
}
// Info to print info log.
func (l *logger) Info(format string, args ...interface{}) {
if l.level >= LogInfo {
fmt.Fprintf(os.Stdout, "[I] "+format+"\n", args...)
}
}
// Error to print error log.
func (l *logger) Error(format string, args ...interface{}) {
if l.level >= LogError {
_, f, l, _ := runtime.Caller(1)
caller := filename(f) + ":" + strconv.Itoa(l)
fmt.Fprintf(os.Stderr, "[E] "+caller+": "+format+"\n", args...)
}
}
func filename(fpath string) string {
if i := strings.LastIndexByte(fpath, filepath.Separator); i >= 0 {
return fpath[i+1:]
}
return fpath
}