This repository was archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathlog.go
105 lines (87 loc) · 2.05 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package log
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/sirupsen/logrus"
"github.com/bytom/bytom/config"
)
const (
rotationTime int64 = 86400
maxAge int64 = 604800
)
var defaultFormatter = &logrus.TextFormatter{DisableColors: true}
func InitLogFile(config *config.Config) error {
logPath := config.LogDir()
if err := clearLockFiles(logPath); err != nil {
return err
}
hook := newBtmHook(logPath)
logrus.AddHook(hook)
logrus.SetOutput(ioutil.Discard) //控制台不输出
fmt.Printf("all logs are output in the %s directory\n", logPath)
return nil
}
type BtmHook struct {
logPath string
lock *sync.Mutex
}
func newBtmHook(logPath string) *BtmHook {
hook := &BtmHook{lock: new(sync.Mutex)}
hook.logPath = logPath
return hook
}
// Write a log line to an io.Writer.
func (hook *BtmHook) ioWrite(entry *logrus.Entry) error {
module := "general"
if data, ok := entry.Data["module"]; ok {
module = data.(string)
}
logPath := filepath.Join(hook.logPath, module)
writer, err := rotatelogs.New(
logPath+".%Y%m%d",
rotatelogs.WithMaxAge(time.Duration(maxAge)*time.Second),
rotatelogs.WithRotationTime(time.Duration(rotationTime)*time.Second),
)
if err != nil {
return err
}
msg, err := defaultFormatter.Format(entry)
if err != nil {
return err
}
if _, err = writer.Write(msg); err != nil {
return err
}
return writer.Close()
}
func clearLockFiles(logPath string) error {
files, err := ioutil.ReadDir(logPath)
if os.IsNotExist(err) {
return nil
} else if err != nil {
return err
}
for _, file := range files {
if ok := strings.HasSuffix(file.Name(), "_lock"); ok {
if err := os.Remove(filepath.Join(logPath, file.Name())); err != nil {
return err
}
}
}
return nil
}
func (hook *BtmHook) Fire(entry *logrus.Entry) error {
hook.lock.Lock()
defer hook.lock.Unlock()
return hook.ioWrite(entry)
}
// Levels returns configured log levels.
func (hook *BtmHook) Levels() []logrus.Level {
return logrus.AllLevels
}