Skip to content

Commit

Permalink
修正 go test 环境下文件日志丢失的 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
b committed Feb 21, 2024
1 parent a9d2c8c commit a449ab4
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 102 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
require (
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNc
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
Expand Down
13 changes: 7 additions & 6 deletions logx/Debug.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package logx

import (
"io"
"os"

"github.com/5-say/go-tool/logx/tool"
"github.com/rs/zerolog"
)

Expand All @@ -11,14 +13,13 @@ var (
debugLogger *zerolog.Logger
)

// InitWriter_Debug
// InitDebugWriter
//
// ex:
//
// logx.InitWriter_Debug(filePath, logx.DefaultNewWriterConfig(1, 10, 10))
func InitWriter_Debug(filePath string, debugWriterConfig NewWriterConfig) {
w := NewWriter(filePath+".debug.log", debugWriterConfig)
l := zerolog.New(zerolog.ConsoleWriter{Out: w, NoColor: true, FormatTimestamp: FormatTimestamp}).With().Timestamp().Caller().Logger()
// logx.InitDebugWriter(logx.DefaultWriter("debug.log", true))
func InitDebugWriter(writer io.Writer) {
l := zerolog.New(zerolog.ConsoleWriter{Out: writer, NoColor: true, FormatTimestamp: tool.ZerologFormatTimestamp(LoggingLocationName)}).With().Timestamp().Caller().Logger()
// 存储单例
debugLogger = &l
}
Expand All @@ -33,6 +34,6 @@ func Debug() *zerolog.Event {
if debugLogger != nil {
return debugLogger.Debug()
}
l := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true, FormatTimestamp: FormatTimestamp}).With().Timestamp().Caller().Logger()
l := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true, FormatTimestamp: tool.ZerologFormatTimestamp(LoggingLocationName)}).With().Timestamp().Caller().Logger()
return l.Debug()
}
13 changes: 7 additions & 6 deletions logx/Error.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package logx

import (
"io"
"os"

"github.com/5-say/go-tool/logx/tool"
"github.com/rs/zerolog"
)

Expand All @@ -11,14 +13,13 @@ var (
errorLogger *zerolog.Logger
)

// InitWriter_Error
// InitErrorWriter
//
// ex:
//
// logx.InitWriter_Error(filePath, logx.DefaultNewWriterConfig(1, 10, 10))
func InitWriter_Error(filePath string, errorWriterConfig NewWriterConfig) {
w := NewWriter(filePath+".error.log", errorWriterConfig)
l := zerolog.New(zerolog.ConsoleWriter{Out: w, NoColor: true, FormatTimestamp: FormatTimestamp}).With().Timestamp().Caller().Logger()
// logx.InitErrorWriter(logx.DefaultWriter("error.log", true))
func InitErrorWriter(writer io.Writer) {
l := zerolog.New(zerolog.ConsoleWriter{Out: writer, NoColor: true, FormatTimestamp: tool.ZerologFormatTimestamp(LoggingLocationName)}).With().Timestamp().Caller().Logger()
// 存储单例
errorLogger = &l
}
Expand All @@ -33,6 +34,6 @@ func Error() *zerolog.Event {
if errorLogger != nil {
return errorLogger.Error()
}
l := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, FormatTimestamp: FormatTimestamp}).With().Timestamp().Caller().Logger()
l := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, FormatTimestamp: tool.ZerologFormatTimestamp(LoggingLocationName)}).With().Timestamp().Caller().Logger()
return l.Error()
}
18 changes: 8 additions & 10 deletions logx/Gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"os"

"github.com/5-say/go-tool/logx/tool"
"github.com/rs/zerolog"
)

Expand All @@ -12,14 +13,13 @@ var (
ginWriter io.Writer
)

// InitWriter_Gin
// InitGinWriter
//
// ex:
//
// logx.InitWriter_Gin(filePath, logx.DefaultNewWriterConfig(1, 10, 10))
func InitWriter_Gin(filePath string, ginWriterConfig NewWriterConfig) {
w := NewWriter(filePath+".gin.log", ginWriterConfig)
l := zerolog.New(zerolog.ConsoleWriter{Out: w, NoColor: true, FormatTimestamp: FormatTimestamp}).With().Timestamp().Caller().Logger()
// logx.InitGinWriter(logx.DefaultWriter("gin.log", true))
func InitGinWriter(writer io.Writer) {
l := zerolog.New(zerolog.ConsoleWriter{Out: writer, NoColor: true, FormatTimestamp: tool.ZerologFormatTimestamp(LoggingLocationName)}).With().Timestamp().Caller().Logger()
// 存储单例
ginWriter = l
}
Expand All @@ -28,14 +28,12 @@ func InitWriter_Gin(filePath string, ginWriterConfig NewWriterConfig) {
//
// ex:
//
// if !isTestMode {
// gin.DefaultWriter = logx.GinWriter()
// gin.DefaultErrorWriter = logx.GinWriter()
// }
// gin.DefaultWriter = logx.GinWriter()
// gin.DefaultErrorWriter = logx.GinWriter()
// r := gin.Default()
func GinWriter() io.Writer {
if ginWriter != nil {
return ginWriter
}
return zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, FormatTimestamp: FormatTimestamp}).With().Timestamp().Caller().Logger()
return zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, FormatTimestamp: tool.ZerologFormatTimestamp(LoggingLocationName)}).With().Timestamp().Caller().Logger()
}
13 changes: 7 additions & 6 deletions logx/Gorm.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package logx

import (
"io"
"os"

"github.com/5-say/go-tool/logx/tool"
"github.com/rs/zerolog"
"gorm.io/gorm/logger"
)
Expand All @@ -12,14 +14,13 @@ var (
gormWriter logger.Writer
)

// InitWriter_Gorm
// InitGormWriter
//
// ex:
//
// logx.InitWriter_Gorm(filePath, logx.DefaultNewWriterConfig(1, 10, 10))
func InitWriter_Gorm(filePath string, gormWriterConfig NewWriterConfig) {
w := NewWriter(filePath+".gorm.log", gormWriterConfig)
l := zerolog.New(zerolog.ConsoleWriter{Out: w, NoColor: true, FormatTimestamp: FormatTimestamp}).With().Timestamp().Logger()
// logx.InitGormWriter(logx.DefaultWriter("gorm.log", true))
func InitGormWriter(writer io.Writer) {
l := zerolog.New(zerolog.ConsoleWriter{Out: writer, NoColor: true, FormatTimestamp: tool.ZerologFormatTimestamp(LoggingLocationName)}).With().Timestamp().Logger()
// 存储单例
gormWriter = &l
}
Expand All @@ -39,7 +40,7 @@ func GormLogger(config logger.Config) logger.Interface {
config.Colorful = false
return logger.New(gormWriter, config)
}
l := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, FormatTimestamp: FormatTimestamp}).With().Timestamp().Logger()
l := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, FormatTimestamp: tool.ZerologFormatTimestamp(LoggingLocationName)}).With().Timestamp().Logger()
config.Colorful = true
return logger.New(&l, config)
}
13 changes: 7 additions & 6 deletions logx/Info.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package logx

import (
"io"
"os"

"github.com/5-say/go-tool/logx/tool"
"github.com/rs/zerolog"
)

Expand All @@ -11,14 +13,13 @@ var (
infoLogger *zerolog.Logger
)

// InitWriter_Info
// InitInfoWriter
//
// ex:
//
// logx.InitWriter_Info(filePath, logx.DefaultNewWriterConfig(1, 10, 10))
func InitWriter_Info(filePath string, infoWriterConfig NewWriterConfig) {
w := NewWriter(filePath+".info.log", infoWriterConfig)
l := zerolog.New(zerolog.ConsoleWriter{Out: w, NoColor: true, FormatTimestamp: FormatTimestamp}).With().Timestamp().Caller().Logger()
// logx.InitInfoWriter(logx.DefaultWriter("info.log", true))
func InitInfoWriter(writer io.Writer) {
l := zerolog.New(zerolog.ConsoleWriter{Out: writer, NoColor: true, FormatTimestamp: tool.ZerologFormatTimestamp(LoggingLocationName)}).With().Timestamp().Caller().Logger()
// 存储单例
infoLogger = &l
}
Expand All @@ -33,6 +34,6 @@ func Info() *zerolog.Event {
if infoLogger != nil {
return infoLogger.Info()
}
l := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, FormatTimestamp: FormatTimestamp}).With().Timestamp().Caller().Logger()
l := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, FormatTimestamp: tool.ZerologFormatTimestamp(LoggingLocationName)}).With().Timestamp().Caller().Logger()
return l.Info()
}
65 changes: 24 additions & 41 deletions logx/NewWriter.go
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
}
26 changes: 0 additions & 26 deletions logx/Singleton.go

This file was deleted.

60 changes: 60 additions & 0 deletions logx/main.go
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))
}
}
}
Loading

0 comments on commit a449ab4

Please sign in to comment.