-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
33 lines (25 loc) · 973 Bytes
/
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
package main
import (
"fmt"
"os"
"path/filepath"
logging "github.com/op/go-logging"
)
const (
LogDir = "/var/log/hera"
)
func InitLogger(name string) {
log := logging.MustGetLogger(name)
logPath := filepath.Join(LogDir, fmt.Sprintf("%s.log", name))
stderrBackend := logging.NewLogBackend(os.Stderr, "", 0)
strderrBackendFormat := logging.MustStringFormatter(`[%{level}] %{message}`)
strderrBackendFormatter := logging.NewBackendFormatter(stderrBackend, strderrBackendFormat)
logFile, err := os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Errorf("Unable to open file for logging: %s", err)
}
logFileBackend := logging.NewLogBackend(logFile, "", 0)
logFileBackendFormat := logging.MustStringFormatter(`%{time:15:04:00.000} [%{level}] %{message}`)
logFileBackendFormatter := logging.NewBackendFormatter(logFileBackend, logFileBackendFormat)
logging.SetBackend(strderrBackendFormatter, logFileBackendFormatter)
}