Files
loggerx/options.go
T

199 lines
3.9 KiB
Go
Raw Normal View History

2024-01-22 18:05:51 +08:00
package loggerx
import (
"io"
"os"
"time"
)
2024-04-15 23:26:55 +08:00
2024-01-22 18:05:51 +08:00
type loggerOption struct {
2024-01-23 00:12:08 +08:00
prefix string // 日志前缀
format string // text json
dir string // 文件目录
isGinLog bool
isGid bool
2024-07-09 15:36:11 +08:00
isPrintFile bool
2024-11-22 20:08:25 +08:00
writeType writeType // 是否异步罗盘
traceField string // trace字段
errorToInfo bool // 错误日志是否写入info日志
days int // 日志保存天数
drivers []io.Writer // 文件落盘驱动器
fileSplit FileSplit // 文件切割规则
sizeSplit int // 根据文件大小切割
timeZone *time.Location // 时区
2025-01-15 15:29:45 +08:00
escapeHTML bool
2025-06-10 15:41:44 +08:00
expandData map[string]string // 扩展字段
2024-01-22 18:05:51 +08:00
}
2024-11-22 20:08:25 +08:00
type writeType uint8
const (
// 0.默认(同步) 1.指定同步 2.指定异步
writeTypeDefault writeType = iota
writeTypeSync
writeTypeAsync
)
2024-01-22 18:05:51 +08:00
func defaultOptions() loggerOption {
return loggerOption{
2024-07-09 15:36:11 +08:00
isGinLog: true,
isGid: true,
isPrintFile: true,
2024-11-22 20:08:25 +08:00
writeType: writeTypeDefault, // 默认同步
2024-07-09 15:36:11 +08:00
format: "json",
dir: "./log",
traceField: "trace_id",
days: 7,
fileSplit: FileSplitTimeE,
timeZone: time.Local,
2025-01-15 15:29:45 +08:00
escapeHTML: true,
2025-06-10 15:41:44 +08:00
expandData: make(map[string]string),
2024-01-22 18:05:51 +08:00
}
}
type Option func(*loggerOption)
2024-01-23 00:12:08 +08:00
// trace字段
func SetTraceField(traceField string) Option {
return func(o *loggerOption) {
o.traceField = traceField
}
}
2025-06-10 15:41:44 +08:00
// 附加字段
func SetExpandData(key string, value string) Option {
return func(o *loggerOption) {
if o.expandData == nil {
o.expandData = make(map[string]string)
}
o.expandData[key] = value
}
}
2024-11-22 20:08:25 +08:00
// 是否异步写入
func SetWriteAsync() Option {
return func(o *loggerOption) {
o.writeType = writeTypeAsync
}
}
// 打印到控制台
func SetToConsole() Option {
return func(o *loggerOption) {
o.drivers = append(o.drivers, os.Stdout)
}
}
2024-01-23 00:12:08 +08:00
// 错误日志是否写入info日志
func SetErrorToInfo() Option {
return func(o *loggerOption) {
o.errorToInfo = true
}
}
2024-01-22 18:05:51 +08:00
// 日志的前缀
func SetPrefix(prefix string) Option {
return func(o *loggerOption) {
o.prefix = prefix
}
}
// 日志格式(默认json)
func SetFormat(format string) Option {
return func(o *loggerOption) {
o.format = format
}
}
2024-07-09 15:36:11 +08:00
// 设置是否打印到文件
func SetPrintFile(print bool) Option {
return func(o *loggerOption) {
o.isPrintFile = print
}
}
2024-01-22 18:05:51 +08:00
// 是否保存gin的日志
2024-04-15 23:40:55 +08:00
func SetGinLog(open bool) Option {
2024-01-22 18:05:51 +08:00
return func(o *loggerOption) {
2024-04-15 23:40:55 +08:00
o.isGinLog = open
2024-01-22 18:05:51 +08:00
}
}
2024-01-23 00:12:08 +08:00
// 文件路径
func SetDir(dir string) Option {
2024-01-22 18:05:51 +08:00
return func(o *loggerOption) {
2024-03-13 16:39:42 +08:00
if dir != "" {
o.dir = dir
}
2024-01-22 18:05:51 +08:00
}
}
// 保存goroutine的ID信息
2024-04-15 23:40:55 +08:00
func SetGID(open bool) Option {
2024-01-22 18:05:51 +08:00
return func(o *loggerOption) {
2024-04-15 23:40:55 +08:00
o.isGid = open
2024-01-22 18:05:51 +08:00
}
}
2024-01-23 13:37:51 +08:00
// 日志保存天数
func SetDays(days int) Option {
return func(o *loggerOption) {
o.days = days
}
}
2024-04-15 23:26:55 +08:00
// 设置时区
func SetTimeZone(loc *time.Location) Option {
2024-04-15 23:26:55 +08:00
return func(o *loggerOption) {
o.timeZone = loc
2024-04-15 23:26:55 +08:00
}
}
// 文件额外的驱动
func SetExtraDriver(ds ...io.Writer) Option {
return func(o *loggerOption) {
2024-04-21 12:23:26 +08:00
for _, d := range ds {
if d != nil {
o.drivers = append(o.drivers, d)
}
}
2024-04-15 23:26:55 +08:00
}
}
2024-01-22 18:05:51 +08:00
// 文件切割规则
2024-01-23 00:12:08 +08:00
// 1.文件大小
// 2.时间A(年/月/日/时)
// 3.时间B(年/月-日)
// 4.时间C(年-月-日-时)
// 5.时间D(年-月-日)
2024-05-08 16:33:58 +08:00
func SetFileSplit(split FileSplit) Option {
return func(o *loggerOption) {
o.fileSplit = split
}
}
type FileSplit string
const (
FileSplitNone FileSplit = "none" // 不切割
FileSplitTimeA FileSplit = "timeA" // (年/月/日/时)
FileSplitTimeB FileSplit = "timeB" // (年/月/日)
FileSplitTimeC FileSplit = "timeC" // (年/月-日)
FileSplitTimeD FileSplit = "timeD" // (年-月-日-时)
FileSplitTimeE FileSplit = "timeE" // (年-月-日)
2024-01-23 00:12:08 +08:00
2024-05-08 16:33:58 +08:00
)
// 根据文件大小切割(暂时未生效)
func SetSizeSplit(m int) Option {
return func(o *loggerOption) {
o.sizeSplit = m
}
}
2025-01-15 15:29:45 +08:00
func SetEscapeHTML(b bool) Option {
return func(o *loggerOption) {
o.escapeHTML = b
}
}