amuz.es/src/infra/goutils@v0.1.3/logger/rotater/writeRotater.go (about)

     1  package rotater
     2  
     3  import (
     4  	"sync"
     5  	"amuz.es/src/infra/goutils/logger"
     6  	"github.com/lestrrat-go/file-rotatelogs"
     7  )
     8  
     9  type Option = rotatelogs.Option
    10  
    11  type rotateSyncer struct {
    12  	setOnceOnclose *sync.Once
    13  	onClose        func()
    14  	*rotatelogs.RotateLogs
    15  }
    16  
    17  func NewRotater(filename string, options ...Option) (logger.RotateSyncer, error) {
    18  	if rotateLogger, err := rotatelogs.New(filename, options...); err != nil {
    19  		return nil, err
    20  	} else {
    21  		return &rotateSyncer{
    22  			setOnceOnclose: &sync.Once{},
    23  			RotateLogs:     rotateLogger,
    24  		}, nil
    25  	}
    26  }
    27  func (r *rotateSyncer) SetOnClose(closeFunc func()) {
    28  	r.setOnceOnclose.Do(func() {
    29  		r.onClose = closeFunc
    30  	})
    31  }
    32  
    33  func (r *rotateSyncer) Rotate() error {
    34  	return r.RotateLogs.Rotate()
    35  }
    36  func (r *rotateSyncer) Close() error {
    37  	defer func() {
    38  		if r.onClose != nil {
    39  			r.onClose()
    40  		}
    41  	}()
    42  	return r.RotateLogs.Close()
    43  }
    44  
    45  func (r *rotateSyncer) Sync() error {
    46  	return nil
    47  }
    48  
    49  func (s *rotateSyncer) Write(bs []byte) (int, error) {
    50  	return s.RotateLogs.Write(bs)
    51  }