-
Notifications
You must be signed in to change notification settings - Fork 0
/
pldlogger.go
executable file
·146 lines (132 loc) · 3.82 KB
/
pldlogger.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package sdk
import (
"fmt"
"github.com/kataras/golog"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/paradeum-team/chainstorage-sdk/utils"
"io"
"os"
"path"
"sync"
"time"
)
type PldLogger struct {
logger *golog.Logger
config *LoggerConf
currentDate string //当前时间
}
var onceLogger sync.Once
var pldLoggerInstance *PldLogger
func newLogger(cfg *LoggerConf) *PldLogger {
if pldLoggerInstance != nil {
return pldLoggerInstance
}
onceLogger.Do(func() {
currentDate := utils.GetCurrentDate8() //当前的8位长度的日期
pldLoggerInstance = &PldLogger{
logger: golog.Default,
config: cfg,
currentDate: currentDate,
}
pldLoggerInstance.logger.SetTimeFormat("2006-01-02 15:04:05")
if cfg.IsOutPutFile == false {
//return pldLoggerInstance
return
}
logInfoPath := pldLoggerInstance.createGinSysLogPath("chainstorage-sdk")
/*file, err := os.OpenFile(logInfoPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
if err != nil {
log.Printf("ERROR: %s\n", fmt.Sprintf("%s append|create failed:%v", logInfoPath, err))
return nil
}*/
//设置output
logWriter := pldLoggerInstance.logSplite(logInfoPath)
pldLoggerInstance.logger.SetOutput(logWriter)
//pldLoggerInstance.logger.AddOutput(logWriter)
})
return pldLoggerInstance
}
//func NewInstance() *PldLogger {
// if pldLoggerInstance != nil {
// return pldLoggerInstance
// }
// currentDate := utils.GetCurrentDate8() //当前的8位长度的日期
// pldLoggerInstance = &PldLogger{
// logger: golog.Default,
// currentDate: currentDate,
// }
// pldLoggerInstance.logger.SetTimeFormat("2006-01-02 15:04:05")
//
// if conf.Logger.IsOutPutFile == false {
// return pldLoggerInstance
// }
//
// logInfoPath := createGinSysLogPath("pn")
// /*file, err := os.OpenFile(logInfoPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
// if err != nil {
// log.Printf("ERROR: %s\n", fmt.Sprintf("%s append|create failed:%v", logInfoPath, err))
// return nil
// }*/
// //设置output
// logWriter := logSplite(logInfoPath)
// pldLoggerInstance.logger.SetOutput(logWriter)
// //pldLoggerInstance.logger.AddOutput(logWriter)
//
// return pldLoggerInstance
//}
func GetLogger(cfg *LoggerConf) *PldLogger {
if pldLoggerInstance == nil {
pldLoggerInstance = newLogger(cfg)
//return pldLoggerInstance.logger
} /*else {
if lf.currentDate == bfsutils.GetCurrentDate8() {
//同一天,说明日志不用切换文件,否则就新打开一个文件
} else {
NewInstance()
}
}*/
return pldLoggerInstance
}
/*
*
创建系统日志的名字
*/
func (pl *PldLogger) createGinSysLogPath(filePrix string) string {
baseLogPath := pl.config.LogPath
writePath := utils.CreateDateDir(baseLogPath) //根据时间检测是否存在目录,不存在创建
//fileName := path.Join(writePath, filePrix + "_" + bfsutils.GetCurrentDate8() + ".log")
fileName := path.Join(writePath, filePrix)
return fileName
}
/*
*
使用io.WriteString()函数进行数据的写入,不存在则创建
*/
func (pl *PldLogger) writeWithIo(filePath, content string) {
fileObj, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
if err != nil {
fmt.Println("Failed to open the file", err.Error())
os.Exit(2)
}
defer fileObj.Close()
io.WriteString(fileObj, content)
}
/*
*
日志分割
*/
func (pl *PldLogger) logSplite(logInfoPath string) io.Writer {
logWriter, _ := rotatelogs.New(
// 分割后的文件名称
logInfoPath+"_%Y%m%d.log",
// 生成软链,指向最新日志文件
//rotatelogs.WithLinkName(logInfoPath),
// 设置最大保存时间(7天)
rotatelogs.WithMaxAge(time.Duration(pl.config.MaxAgeDay*24)*time.Hour),
// 设置日志切割时间间隔(1天)
rotatelogs.WithRotationTime(time.Duration(pl.config.RotationTime*24)*time.Hour),
//大小为这么多过期
//rotatelogs.WithRotationCount(30),
)
return logWriter
}