forked from hoisie/twitterstream
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlog.go
74 lines (64 loc) · 1.45 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
69
70
71
72
73
74
package httpstream
import (
"fmt"
"log"
)
const (
FATAL = 0
ERROR = 1
WARN = 2
INFO = 3
DEBUG = 4
)
var (
LogLevel int = ERROR
logger *log.Logger
LogLevelWords map[string]int = map[string]int{"fatal": 0, "error": 1, "warn": 2, "info": 3, "debug": 4, "none": -1}
)
// set the logger to use, normal usage:
//
// var logLevel *string = flag.String("logging", "debug", "Which log level: [debug,info,warn,error,fatal]")
//
// func init() {
// SetLogger(log.New(os.Stdout, "", log.Ltime|log.Lshortfile), "debug")
// }
func SetLogger(l *log.Logger, logLevel string) {
logger = l
LogLevelSet(logLevel)
}
// sets the log level from a string
func LogLevelSet(level string) {
if lvl, ok := LogLevelWords[level]; ok {
LogLevel = lvl
}
}
// Log at debug level
func Debug(v ...interface{}) {
if logger != nil && LogLevel >= 4 {
logger.Output(2, fmt.Sprintln(v...))
}
}
func Debugf(format string, v ...interface{}) {
if LogLevel >= 4 {
DoLog(3, fmt.Sprintf(format, v...), logger)
}
}
// Log to logger if setup
// Log(ERROR, "message")
func Log(logLvl int, v ...interface{}) {
if LogLevel >= logLvl {
DoLog(3, fmt.Sprintln(v...), logger)
}
}
// Log to logger if setup
// Logf(ERROR, "message %d", 20)
func Logf(logLvl int, format string, v ...interface{}) {
if LogLevel >= logLvl {
DoLog(3, fmt.Sprintf(format, v...), logger)
}
}
func DoLog(depth int, msg string, lgr *log.Logger) {
if lgr != nil {
lgr.Output(depth, msg)
}
}