From a50b3d42f36f9c8769ccca4b8357e7e28e1e3d9d Mon Sep 17 00:00:00 2001 From: Sangbum Kim Date: Mon, 6 Nov 2017 22:46:49 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9D=98=EC=A1=B4=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.go | 17 +++++++++++++++++ rotater/global.go | 9 +++++---- rotater/writeRotater.go | 22 ++++------------------ rotater/set_logcore.go => set_logcore.go | 2 +- zap/logger.go | 5 +++-- zap/wrapped.go | 10 +++++----- 6 files changed, 35 insertions(+), 30 deletions(-) create mode 100644 common.go rename rotater/set_logcore.go => set_logcore.go (98%) diff --git a/common.go b/common.go new file mode 100644 index 0000000..e80d63a --- /dev/null +++ b/common.go @@ -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 +} diff --git a/rotater/global.go b/rotater/global.go index 7162c3f..ff3104f 100644 --- a/rotater/global.go +++ b/rotater/global.go @@ -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() }) diff --git a/rotater/writeRotater.go b/rotater/writeRotater.go index 9583d7e..2b98f5f 100644 --- a/rotater/writeRotater.go +++ b/rotater/writeRotater.go @@ -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 }) diff --git a/rotater/set_logcore.go b/set_logcore.go similarity index 98% rename from rotater/set_logcore.go rename to set_logcore.go index 82fb0c0..155a466 100644 --- a/rotater/set_logcore.go +++ b/set_logcore.go @@ -1,4 +1,4 @@ -package rotater +package logger import ( "sync" diff --git a/zap/logger.go b/zap/logger.go index 9a171c6..44d8888 100644 --- a/zap/logger.go +++ b/zap/logger.go @@ -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 diff --git a/zap/wrapped.go b/zap/wrapped.go index 8eb5fd5..2a1cd6a 100644 --- a/zap/wrapped.go +++ b/zap/wrapped.go @@ -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) Rotate() (err error) { return } -func (r *zapWrappedSyncer) Close() (err error) { return } -func (r *zapWrappedSyncer) Sync() error { return r.WriteSyncer.Sync() } +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() }