-
Notifications
You must be signed in to change notification settings - Fork 6
/
log.go
36 lines (31 loc) · 785 Bytes
/
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
package main
import (
"flag"
"fmt"
"os"
)
// Log levels
const (
LogOff = iota
LogErr
LogWrn
LogInf
LogDbg
LogTrc
)
var LogLevel *int
var LogUseColor *bool
func AddFlagLog() {
LogLevel = flag.Int("loglevel", LogInf, "Log level (off = 0, error = 1, warning = 2, info = 3, debug = 4, trace = 5)")
LogUseColor = flag.Bool("color", false, "Use color for logging")
}
func Log(level byte, format string, args ...interface{}) {
var colorPrefix, colorSuffix string
if level <= byte(*LogLevel) {
if *LogUseColor {
colorPrefix = "\033[" + [...]string{"", "91", "93", "0", "97", "94"}[level] + "m"
colorSuffix = "\033[0m"
}
_, _ = fmt.Fprintf(os.Stderr, colorPrefix+[...]string{"", "ERR", "WRN", "INF", "DBG", "TRC"}[level]+" "+format+colorSuffix+"\n", args...)
}
}