summaryrefslogtreecommitdiff
path: root/logging/logger.go
blob: 0ecc35c35ae975741a01d37c756f3d8082d8b1f7 (plain)
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
package logging

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
)

var (
	dbg  *log.Logger
	info *log.Logger
	warn *log.Logger
	err  *log.Logger
)

func Init() {
	flags := log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile | log.LUTC
	dbg = log.New(os.Stdout, "DEBUG ", flags)
	info = log.New(os.Stdout, "INFO  ", flags)
	warn = log.New(os.Stdout, "WARN  ", flags)
	err = log.New(os.Stdout, "ERROR ", flags)
}

func ErrorLogger() *log.Logger {
	if err == nil {
		return log.New(ioutil.Discard, "", log.LstdFlags)
	}
	return err
}

func Debugf(message string, args ...interface{}) {
	if dbg == nil {
		return
	}
	if len(args) > 0 {
		message = fmt.Sprintf(message, args...)
	}
	dbg.Output(2, message)
}

func Infof(message string, args ...interface{}) {
	if info == nil {
		return
	}
	if len(args) > 0 {
		message = fmt.Sprintf(message, args...)
	}
	info.Output(2, message)
}

func Warnf(message string, args ...interface{}) {
	if warn == nil {
		return
	}
	if len(args) > 0 {
		message = fmt.Sprintf(message, args...)
	}
	warn.Output(2, message)
}

func Errorf(message string, args ...interface{}) {
	if err == nil {
		return
	}
	if len(args) > 0 {
		message = fmt.Sprintf(message, args...)
	}
	err.Output(2, message)
}