-
Notifications
You must be signed in to change notification settings - Fork 19
/
logging.go
48 lines (38 loc) · 1.33 KB
/
logging.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
package main
import (
"fmt"
"os"
"path/filepath"
"time"
logging "github.com/op/go-logging"
)
var log = logging.MustGetLogger("auto-hard-reset-log")
//LogMachines - function for basic logging
func LogMachines() {
//get binary dir
//os.Args doesn't work the way we want with "go run". You can use next line
//for local dev, but use the original for production.
//dir, err := filepath.Abs("./")
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
t := time.Now().Format("2006-01-02-15-04-05")
fname := fmt.Sprintf("%s/auto-hard-reset-log-%s.txt", dir, t)
f, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
if err != nil {
return
}
//defer f.Close()
b1format := logging.MustStringFormatter(`%{time:2006-01-02-15:04:05.000} ▶ %{level:.5s} %{id:03x} ▶ %{message}`)
b2format := logging.MustStringFormatter(`%{color}%{time:15:04:05.000} ▶ %{level:.5s} %{id:03x} ▶ %{color:reset} %{message}`)
b1 := logging.NewLogBackend(f, "", 0)
b2 := logging.NewLogBackend(os.Stderr, "", 0)
//Formating messages
b1Formater := logging.NewBackendFormatter(b1, b1format)
b2Formater := logging.NewBackendFormatter(b2, b2format)
//Sending errors to b1logger
b1Leveled := logging.AddModuleLevel(b1Formater)
b1Leveled.SetLevel(logging.ERROR, "")
logging.SetBackend(b1Leveled, b2Formater)
}