-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
93 lines (80 loc) · 1.83 KB
/
log.go
File metadata and controls
93 lines (80 loc) · 1.83 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
package log
import (
"fmt"
golog "log"
"os"
"path/filepath"
"runtime"
"time"
)
var logger *golog.Logger
var verbosity Level
var verbositySet bool
// Level is used to set verbosity of your app
type Level int
const (
//LevelNone - log nothing
LevelNone Level = iota
//LevelError - logs only errors
LevelError
//LevelWarning - logs warning and errors
LevelWarning
//LevelTest - logs only debug messages
LevelTest
//LevelInfo - logs info, warining and errors
LevelInfo
)
// getVerbosity defaults verbosity to LogLevelWarning if a verbosity is not set
func getVerbosity() Level {
if !verbositySet {
return LevelWarning
}
return verbosity
}
// SetLogLevel sets log level
func SetLogLevel(l Level) {
verbosity = l
verbositySet = true
}
// SetLogger sets Logger
func SetLogger(l *golog.Logger) {
logger = l
}
func printLog(message string) {
if logger != nil {
logger.Println(message)
} else {
println("[" + time.Now().Format("2006-01-02-15:04:05.000000") + "] " + message)
}
}
// Info prints out informational messages
func Info(message string) {
if getVerbosity() >= LevelInfo {
printLog(message)
}
}
// Error prints out error messages
func Error(message string) {
if getVerbosity() >= LevelError {
_, fn, line, _ := runtime.Caller(1)
printLog(fmt.Sprintf("ERROR: %s (%s:%d)", message, filepath.Base(fn), line))
}
}
// Warn prints out warning messages
func Warn(message string) {
if getVerbosity() >= LevelWarning {
_, fn, line, _ := runtime.Caller(1)
printLog(fmt.Sprintf("WARNING: %s (%s:%d)", message, filepath.Base(fn), line))
}
}
// Test prints out debugging or test messages
func Test(message string) {
if getVerbosity() == LevelTest {
_, fn, line, _ := runtime.Caller(1)
printLog(fmt.Sprintf("DEBUG: %s (%s:%d)", message, filepath.Base(fn), line))
}
}
func Fatal(message string) {
Error(message)
os.Exit(1)
}