-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlog.go
59 lines (51 loc) · 1.16 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2020 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package main
import (
"io"
"os"
"path/filepath"
"strings"
"github.com/kataras/golog"
)
const (
logModeFile = `file`
logModeStdout = `stdout`
logLevelDisable = `disable`
logLevelError = `error`
logLevelWarning = `warn`
logLevelInfo = `info`
)
var (
logFile *os.File
)
// SetLogging sets the options of the logging
func SetLogging(basename string) {
if len(cfg.Log.Level) == 0 {
cfg.Log.Level = logLevelInfo
}
if strings.Index(cfg.Log.Mode, logModeStdout) < 0 {
golog.SetOutput(io.Discard)
}
err := os.MkdirAll(cfg.Log.Dir, 0777)
if err != nil {
golog.Fatal(err)
}
if cfg.Log.Level != logLevelDisable && strings.Index(cfg.Log.Mode, logModeFile) >= 0 {
logFile, err = os.OpenFile(filepath.Join(cfg.Log.Dir, basename+`.log`),
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
golog.Fatal(err)
}
golog.AddOutput(logFile)
}
golog.SetLevel(cfg.Log.Level)
golog.Info(`Start`)
}
// CloseLog close file handle
func CloseLog() {
if logFile != nil {
logFile.Close()
}
}