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