의존 정리
This commit is contained in:
parent
69167604aa
commit
a50b3d42f3
|
@ -0,0 +1,17 @@
|
|||
package logger
|
||||
|
||||
import "io"
|
||||
|
||||
type CloseFunc func()
|
||||
|
||||
type WriteSyncer interface {
|
||||
io.Writer
|
||||
Sync() error
|
||||
}
|
||||
|
||||
type RotateSyncer interface {
|
||||
io.WriteCloser
|
||||
SetOnClose(CloseFunc)
|
||||
Rotate() error
|
||||
Sync() error
|
||||
}
|
|
@ -4,11 +4,12 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"log"
|
||||
"amuz.es/src/infra/logger"
|
||||
)
|
||||
|
||||
var loggers RotateSyncerSet
|
||||
var loggers logger.RotateSyncerSet
|
||||
|
||||
func NewLogWriter(FileName string, MaxSizeMb, MaxBackup, MaxDay int, logDir string) RotateSyncer {
|
||||
func NewLogWriter(FileName string, MaxSizeMb, MaxBackup, MaxDay int, logDir string) logger.RotateSyncer {
|
||||
switch FileName {
|
||||
case "Stdout":
|
||||
return newLocked(os.Stdout)
|
||||
|
@ -35,7 +36,7 @@ func NewLogWriter(FileName string, MaxSizeMb, MaxBackup, MaxDay int, logDir stri
|
|||
}
|
||||
|
||||
func Rotate() {
|
||||
loggers.Range(func(rotater RotateSyncer) {
|
||||
loggers.Range(func(rotater logger.RotateSyncer) {
|
||||
rotater.Sync()
|
||||
rotater.Rotate()
|
||||
})
|
||||
|
@ -43,7 +44,7 @@ func Rotate() {
|
|||
}
|
||||
|
||||
func Close() {
|
||||
loggers.Range(func(rotater RotateSyncer) {
|
||||
loggers.Range(func(rotater logger.RotateSyncer) {
|
||||
rotater.Sync()
|
||||
rotater.Close()
|
||||
})
|
||||
|
|
|
@ -2,31 +2,17 @@ package rotater
|
|||
|
||||
import (
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
"io"
|
||||
"sync"
|
||||
"amuz.es/src/infra/logger"
|
||||
)
|
||||
|
||||
type CloseFunc func()
|
||||
|
||||
type WriteSyncer interface {
|
||||
io.Writer
|
||||
Sync() error
|
||||
}
|
||||
|
||||
type RotateSyncer interface {
|
||||
io.WriteCloser
|
||||
SetOnClose(CloseFunc)
|
||||
Rotate() error
|
||||
Sync() error
|
||||
}
|
||||
|
||||
type rotateSyncer struct {
|
||||
setOnceOnclose *sync.Once
|
||||
onClose CloseFunc
|
||||
onClose logger.CloseFunc
|
||||
lumberjack.Logger
|
||||
}
|
||||
|
||||
func newRotater(filename string, maxSize, maxBackup, maxDay int) RotateSyncer {
|
||||
func newRotater(filename string, maxSize, maxBackup, maxDay int) logger.RotateSyncer {
|
||||
return &rotateSyncer{
|
||||
setOnceOnclose: &sync.Once{},
|
||||
Logger: lumberjack.Logger{
|
||||
|
@ -39,7 +25,7 @@ func newRotater(filename string, maxSize, maxBackup, maxDay int) RotateSyncer {
|
|||
},
|
||||
}
|
||||
}
|
||||
func (r *rotateSyncer) SetOnClose(closeFunc CloseFunc) {
|
||||
func (r *rotateSyncer) SetOnClose(closeFunc logger.CloseFunc) {
|
||||
r.setOnceOnclose.Do(func() {
|
||||
r.onClose = closeFunc
|
||||
})
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package rotater
|
||||
package logger
|
||||
|
||||
import (
|
||||
"sync"
|
|
@ -5,11 +5,12 @@ import (
|
|||
|
||||
"go.uber.org/zap/zapcore"
|
||||
"amuz.es/src/infra/logger/rotater"
|
||||
"amuz.es/src/infra/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultOptions []zap.Option
|
||||
defaultWriter rotater.RotateSyncer
|
||||
defaultWriter logger.RotateSyncer
|
||||
nopCloser = func() (err error) { return }
|
||||
)
|
||||
|
||||
|
@ -63,7 +64,7 @@ func NewOtherLogger(
|
|||
logWriter := rotater.NewLogWriter(logFilename, maxSizeMb, maxBackup, maxDay, logDir)
|
||||
core := zapcore.NewCore(formatter, logWriter, loglevel)
|
||||
|
||||
if rotateSyncer, ok := logWriter.(rotater.RotateSyncer); ok {
|
||||
if rotateSyncer, ok := logWriter.(logger.RotateSyncer); ok {
|
||||
closer = rotateSyncer.Close
|
||||
} else {
|
||||
closer = nopCloser
|
||||
|
|
|
@ -2,14 +2,14 @@ package zap
|
|||
|
||||
import (
|
||||
"go.uber.org/zap/zapcore"
|
||||
"amuz.es/src/infra/logger/rotater"
|
||||
"amuz.es/src/infra/logger"
|
||||
)
|
||||
|
||||
type zapWrappedSyncer struct {
|
||||
zapcore.WriteSyncer
|
||||
}
|
||||
|
||||
func (r *zapWrappedSyncer) SetOnClose(closeFunc rotater.CloseFunc) {}
|
||||
func (r *zapWrappedSyncer) SetOnClose(closeFunc logger.CloseFunc) {}
|
||||
func (r *zapWrappedSyncer) Rotate() (err error) { return }
|
||||
func (r *zapWrappedSyncer) Close() (err error) { return }
|
||||
func (r *zapWrappedSyncer) Sync() error { return r.WriteSyncer.Sync() }
|
||||
|
|
Loading…
Reference in New Issue