-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
b
committed
Feb 21, 2024
1 parent
a9d2c8c
commit a449ab4
Showing
13 changed files
with
188 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,39 @@ | ||
package logx | ||
|
||
import ( | ||
"io" | ||
"time" | ||
|
||
"github.com/natefinch/lumberjack" | ||
"github.com/5-say/go-tool/logx/tool" | ||
"github.com/rs/zerolog/diode" | ||
) | ||
|
||
// NewWriter | ||
// | ||
// fileName string 日志文件名 | ||
// config logx.NewWriterConfig 文件日志配置 | ||
// | ||
// ex: | ||
// | ||
// logx.NewWriter("demo.log", logx.) | ||
func NewWriter(fileName string, config NewWriterConfig) diode.Writer { | ||
var c = config | ||
|
||
// 日志拆分 | ||
rollingWriter := &lumberjack.Logger{ | ||
Filename: fileName, | ||
MaxSize: c.MaxMegabytes, | ||
MaxAge: c.MaxDays, | ||
MaxBackups: c.MaxBackups, | ||
Compress: c.Compress, | ||
} | ||
// WriterConfig .. | ||
type WriterConfig struct { | ||
Rolling WriterRollingConfig | ||
Async *WriterAsyncConfig | ||
} | ||
|
||
// 线程安全、无锁、无阻塞 | ||
return diode.NewWriter(rollingWriter, c.DiodeBufferSize, c.DiodePollInterval, c.DiodeAlerter) | ||
// 滚动写入配置 | ||
type WriterRollingConfig struct { | ||
MaxMegabytes int // 单文件最大存储容量(兆字节) | ||
MaxDays int // 最大存储天数 | ||
MaxBackups int // 最大备份文件数量 | ||
Compress bool // 备份文件是否压缩 | ||
} | ||
|
||
// NewWriterConfig .. | ||
type NewWriterConfig struct { | ||
MaxMegabytes int // 单文件最大容量(兆字节) | ||
MaxDays int // 最大存储天数 | ||
MaxBackups int // 最大备份文件数量 | ||
Compress bool // 备份文件是否压缩 | ||
DiodeBufferSize int // diode 缓冲区大小 | ||
DiodePollInterval time.Duration // diode 轮询时间间隔 | ||
DiodeAlerter diode.Alerter // diode 告警函数 | ||
// 异步写入配置 | ||
type WriterAsyncConfig struct { | ||
BufferSize int // 生产者缓冲区大小 | ||
PollInterval time.Duration // 消费者轮询间隔 | ||
Alerter diode.Alerter // 丢弃告警函数 | ||
} | ||
|
||
// DefaultNewWriterConfig .. | ||
func DefaultNewWriterConfig(maxMegabytes, maxDays, maxBackups int) NewWriterConfig { | ||
return NewWriterConfig{ | ||
MaxMegabytes: maxMegabytes, | ||
MaxDays: maxDays, | ||
MaxBackups: maxBackups, | ||
Compress: false, | ||
DiodeBufferSize: 1000, | ||
DiodePollInterval: 0, | ||
DiodeAlerter: nil, | ||
// 实例化日志写入器,支持 滚动 与 异步 | ||
func NewWriter(fileName string, c WriterConfig) io.Writer { | ||
var w = tool.FileRollingWriter(fileName, c.Rolling.MaxBackups, c.Rolling.MaxDays, c.Rolling.MaxMegabytes, c.Rolling.Compress) | ||
if c.Async != nil { | ||
return tool.WrapAsyncWriter(w, c.Async.BufferSize, c.Async.PollInterval, c.Async.Alerter) | ||
} | ||
return w | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package logx | ||
|
||
import "io" | ||
|
||
// LoggingLocationName | ||
// | ||
// ex: | ||
// | ||
// logx.LoggingLocationName = "Asia/Shanghai" | ||
var LoggingLocationName = "UTC" | ||
|
||
// DefaultWriter .. | ||
// | ||
// ex: | ||
// | ||
// logx.DefaultWriter("logs/demo.log", true) | ||
func DefaultWriter(fileName string, useAsync bool) io.Writer { | ||
config := WriterConfig{ | ||
Rolling: WriterRollingConfig{ | ||
MaxMegabytes: 1, | ||
MaxDays: 10, | ||
MaxBackups: 10, | ||
Compress: false, | ||
}, | ||
} | ||
if useAsync { | ||
config.Async = &WriterAsyncConfig{ | ||
BufferSize: 1000, | ||
PollInterval: 0, | ||
Alerter: nil, | ||
} | ||
} | ||
return NewWriter(fileName, config) | ||
} | ||
|
||
// 初始化日志 | ||
// | ||
// ex: | ||
// | ||
// logx.InitDefault("logs/app", "Asia/Shanghai", isTestMode, "info", "error", "debug", "gin", "gorm") | ||
func InitDefault(filePath, loggingLocationName string, isTestMode bool, tags ...string) { | ||
LoggingLocationName = loggingLocationName | ||
w := func(tag string) io.Writer { | ||
return DefaultWriter(filePath+"."+tag+".log", !isTestMode) | ||
} | ||
for _, tag := range tags { | ||
switch tag { | ||
case "info": | ||
InitInfoWriter(w(tag)) | ||
case "error": | ||
InitErrorWriter(w(tag)) | ||
case "debug": | ||
InitDebugWriter(w(tag)) | ||
case "gin": | ||
InitGinWriter(w(tag)) | ||
case "gorm": | ||
InitGormWriter(w(tag)) | ||
} | ||
} | ||
} |
Oops, something went wrong.