forked from michael1011/lightningtip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
38 lines (25 loc) · 858 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
37
38
package main
import (
"os"
"github.com/op/go-logging"
)
var log = logging.MustGetLogger("")
var logFormat = logging.MustStringFormatter("%{time:2006-01-02 15:04:05} [%{level}] %{message}")
var backendConsole = logging.NewLogBackend(os.Stdout, "", 0)
func initLog() {
logging.SetFormatter(logFormat)
logging.SetBackend(backendConsole)
}
func initLogger(logFile string, level logging.Level) error {
file, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return err
}
backendFile := logging.NewLogBackend(file, "", 0)
backendFileLeveled := logging.AddModuleLevel(backendFile)
backendFileLeveled.SetLevel(level, "")
backendConsoleLeveled := logging.AddModuleLevel(backendConsole)
backendConsoleLeveled.SetLevel(level, "")
logging.SetBackend(backendConsoleLeveled, backendFileLeveled)
return nil
}