-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.go
120 lines (97 loc) · 2.43 KB
/
logger.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
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
package owl
import (
"errors"
"fmt"
"log"
"os"
"time"
raven "github.com/getsentry/raven-go"
"github.com/mgutz/ansi"
)
var (
infoLogger *log.Logger
warningLogger *log.Logger
errorLogger *log.Logger
logFile *os.File
)
func initLogger() error {
if config.Logger == nil {
useSentry = false
return nil
}
ansi.DisableColors(!config.Logger.UseColors)
if config.Logger.DisplayLogs {
formatFlags := log.Ldate | log.Lmicroseconds
infoLogger = log.New(os.Stdout, ansi.Color("[INF] ", "blue"), formatFlags)
warningLogger = log.New(os.Stdout, ansi.Color("[WAR] ", "yellow"), formatFlags)
errorLogger = log.New(os.Stderr, ansi.Color("[ERR] ", "red"), formatFlags)
}
if config.Logger.LogFilePath != "" {
var err error
logFile, err = os.OpenFile(config.Logger.LogFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0660)
if err != nil {
logFile = nil
return Error("owl: cannot open or create log file: %s", err)
}
}
if useSentry {
if config.Logger.SentryDsn == "" {
useSentry = false
return Error("owl: Cannot use Sentry as the SentryDsn configuration field is empty")
}
raven.SetDSN(config.Logger.SentryDsn)
}
return nil
}
func stopLogger() {
if logFile != nil {
logFile.Close()
logFile = nil
}
if infoLogger != nil {
infoLogger = nil
warningLogger = nil
errorLogger = nil
}
}
func Info(format string, args ...interface{}) {
logEverywhere(infoLogger, "inf", format, args...)
}
func Warning(format string, args ...interface{}) {
logEverywhere(warningLogger, "war", format, args...)
}
func Error(format string, args ...interface{}) error {
message := logEverywhere(errorLogger, "err", format, args...)
err := errors.New(message)
if useSentry {
raven.CaptureError(err, nil)
}
return err
}
func logEverywhere(l *log.Logger, lvl, format string, args ...interface{}) (msg string) {
msg = fmt.Sprintf(format, args...)
writeToLogFile(lvl, msg)
writeToLogger(l, msg)
return
}
func writeToLogger(l *log.Logger, msg string) {
if l == nil {
return
}
l.Println(msg)
}
func writeToLogFile(lvl, msg string) {
if logFile == nil {
return
}
jsonMsg := fmt.Sprintf("{\"ts\":\"%s\",\"lvl\":\"%s\",\"msg\":\"%s\",\"app\":\"%s\",\"logger\":\"%s\"}\n",
time.Now().Format("2006-01-02T15:04:05.000"),
lvl,
msg,
config.AppName,
config.Logger.Logger,
)
if _, err := logFile.WriteString(jsonMsg); err != nil {
writeToLogger(errorLogger, fmt.Sprintf("owl: cannot write log file to disk: %s", err))
}
}