-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.go
43 lines (35 loc) · 1.15 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
package gobot
import (
"github.com/op/go-logging"
"os"
)
var (
// Log handle. See: https://github.com/op/go-logging
Log *logging.Logger
format = logging.MustStringFormatter(
`%{color}%{time:15:04:05.000} %{shortfunc} [%{level:.4s}]%{color:reset} %{message}`,
)
uncoloredFormat = logging.MustStringFormatter(
`%{time:15:04:05.000} %{shortfunc} [%{level:.4s}] %{message}`,
)
)
const (
logfile = "gobot.log"
loggerName = "gobot"
)
func init() {
file, err := os.OpenFile(logfile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
Log = logging.MustGetLogger(loggerName)
bufferStdout := logging.NewLogBackend(os.Stdout, "", 0)
bufferStdoutFormatted := logging.NewBackendFormatter(bufferStdout, format)
bufferStdoutLeveled := logging.AddModuleLevel(bufferStdoutFormatted)
bufferStdoutLeveled.SetLevel(logging.DEBUG, "")
bufferFile := logging.NewLogBackend(file, "", 0)
bufferFileFormatted := logging.NewBackendFormatter(bufferFile, uncoloredFormat)
bufferFileLeveled := logging.AddModuleLevel(bufferFileFormatted)
bufferFileLeveled.SetLevel(logging.DEBUG, "")
logging.SetBackend(bufferStdoutLeveled, bufferFileLeveled)
}