This repository was archived by the owner on Sep 17, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlog.go
More file actions
124 lines (108 loc) · 2.03 KB
/
Copy pathlog.go
File metadata and controls
124 lines (108 loc) · 2.03 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package logger
import (
"fmt"
"log"
"os"
"strings"
"github.com/ModuleAB/ModuleAB/agent/consts"
)
const (
LogLevelDebug = iota
LogLevelInformation
LogLevelWarning
LogLevelError
LogLevelFatal
)
// Logger module
type Logger struct {
log.Logger
Level int
}
func (l *Logger) out(level string, v interface{}) {
l.SetPrefix(level)
l.Println(interfaceToLog(v))
}
// Debug is loglevel Debug
func (l *Logger) Debug(v ...interface{}) {
if l.Level <= LogLevelDebug {
l.out("Debug\t", v)
}
}
func (l *Logger) Information(v ...interface{}) {
if l.Level <= LogLevelInformation {
l.out("Info\t", v)
}
}
func (l *Logger) Info(v ...interface{}) {
l.Information(v)
}
func (l *Logger) Warning(v ...interface{}) {
if l.Level <= LogLevelWarning {
l.out("Warn\t", v)
}
}
func (l *Logger) Warn(v ...interface{}) {
l.Warning(v)
}
func (l *Logger) Error(v ...interface{}) {
if l.Level <= LogLevelError {
l.out("Error\t", v)
}
}
func (l *Logger) Fatal(v ...interface{}) {
if l.Level <= LogLevelFatal {
l.out("Fatal\t", v)
}
}
// AppLog is default log instance
var AppLog Logger
// Init is to configure log module
func Init() {
w, err := os.OpenFile(
consts.DefaultLogFile,
os.O_APPEND|os.O_CREATE|os.O_RDWR,
0666,
)
if err != nil {
fmt.Fprintln(
os.Stderr, consts.ErrorFormat,
"Cannot write log file.", err,
)
os.Exit(1)
}
AppLog.SetFlags(log.LstdFlags)
AppLog.SetOutput(w)
AppLog.Level = LogLevelInformation
}
// StringLevelToInt is to convert string log level to internal code.
func StringLevelToInt(l string) int {
switch l {
case "deb":
fallthrough
case "debug":
return LogLevelDebug
case "warn":
fallthrough
case "warning":
return LogLevelWarning
case "error":
fallthrough
case "err":
return LogLevelError
case "fatal":
return LogLevelFatal
case "info":
fallthrough
case "information":
fallthrough
default:
return LogLevelInformation
}
}
func interfaceToLog(v ...interface{}) string {
var s string
for _, n := range v {
s = fmt.Sprintf("%s%v ", s, n)
}
return strings.Trim(s, "[] ")
}