Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
kakine committed Jul 1, 2016
1 parent e321e6a commit d790019
Showing 1 changed file with 31 additions and 52 deletions.
83 changes: 31 additions & 52 deletions rotationfile_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ type RotationFileHandler struct {
maxSize int64
async bool
asyncFlushInterval int
logBuffer *rotationFileLogBuffer
bufferSize int
buffer *bytes.Buffer
lastLogEvent LogEvent
scheduledFlush bool
logFileSize int64
lastModifiedTime time.Time
Expand All @@ -41,7 +43,7 @@ func (h *RotationFileHandler) Write(loggerName string, logEvent LogEvent, format
h.mutex.Lock()
defer h.mutex.Unlock()
if h.async {
lastLogEvent, formattedLog, full := h.logBuffer.addBuffer(logEvent, formattedLog)
lastLogEvent, formattedLog, full := h.pushBuffer(logEvent, formattedLog)
if full {
h.writeLog(lastLogEvent.Time(), formattedLog)
} else {
Expand Down Expand Up @@ -124,10 +126,10 @@ func (h *RotationFileHandler) SetAsyncFlushInterval(asyncFlushInterval int) {
h.asyncFlushInterval = asyncFlushInterval
}

func (h *RotationFileHandler) SeaAsyncBufferSize(bufferSize int) {
func (h *RotationFileHandler) SetBufferSize(bufferSize int) {
h.mutex.Lock()
defer h.mutex.Unlock()
h.logBuffer.setBufferSize(bufferSize)
h.bufferSize = bufferSize
}

func (h *RotationFileHandler) logBufferFlushTimer() {
Expand All @@ -139,7 +141,7 @@ func (h *RotationFileHandler) logBufferFlushTimer() {
}

func (h *RotationFileHandler) logBufferFlush() {
lastLogEvent, logBuffer, remain := h.logBuffer.drainBuffer()
lastLogEvent, logBuffer, remain := h.popBuffer()
if remain {
h.writeLog(lastLogEvent.Time(), logBuffer)
}
Expand Down Expand Up @@ -254,63 +256,40 @@ func (h *RotationFileHandler) deleteOldLogFiles() {
}
}

func NewRotationFileHandler() (handler Handler) {
return &RotationFileHandler{
logFileName: fmt.Sprintf("%v.log", filepath.Base(os.Args[0])),
logDirPath: fmt.Sprintf("/var/log/%v", filepath.Base(os.Args[0])),
maxAge: 7,
async: false,
asyncFlushInterval: rotationFileDefaultAsyncFlushInterval,
logBuffer: newRotationFileLogBuffer(),
mutex: new(sync.Mutex),
}
}

type rotationFileLogBuffer struct {
bufferSize int
buffer *bytes.Buffer
lastLogEvent LogEvent
mutex *sync.RWMutex
}

func (b *rotationFileLogBuffer) addBuffer(logEvent LogEvent, formattedLog string) (lastLogEvent LogEvent, logBuffer string, full bool) {
b.mutex.RLock()
defer b.mutex.RUnlock()
b.buffer.WriteString(formattedLog)
b.lastLogEvent = logEvent
if b.buffer.Len() > b.bufferSize {
logBuffer := b.buffer.String()
b.buffer.Truncate(0)
lastLogEvent := b.lastLogEvent
b.lastLogEvent = nil
return lastLogEvent, logBuffer, true
func (h *RotationFileHandler) pushBuffer(logEvent LogEvent, formattedLog string) (lastLogEvent LogEvent, logBuffer string, full bool) {
h.buffer.WriteString(formattedLog)
h.lastLogEvent = logEvent
if h.buffer.Len() > h.bufferSize {
return h.popBufferBase()
}
return nil, "", false
}

func (b *rotationFileLogBuffer) drainBuffer() (lastLogEvent LogEvent, logBuffer string, remain bool) {
b.mutex.RLock()
defer b.mutex.RUnlock()
if b.buffer.Len() > 0 {
logBuffer := b.buffer.String()
b.buffer.Truncate(0)
lastLogEvent := b.lastLogEvent
b.lastLogEvent = nil
return lastLogEvent, logBuffer, true
func (h *RotationFileHandler) popBuffer() (lastLogEvent LogEvent, logBuffer string, remain bool) {
if h.buffer.Len() > 0 {
return h.popBufferBase()
}
return nil, "", false
}

func (b *rotationFileLogBuffer) setBufferSize(bufferSize int) {
b.mutex.Lock()
defer b.mutex.Unlock()
b.bufferSize = bufferSize
func (h *RotationFileHandler) popBufferBase() (lastLogEvent LogEvent, logBuffer string, remain bool) {
logBuffer = h.buffer.String()
h.buffer.Truncate(0)
lastLogEvent = h.lastLogEvent
h.lastLogEvent = nil
return lastLogEvent, logBuffer, true
}

func newRotationFileLogBuffer() (logBuffer *rotationFileLogBuffer) {
return &rotationFileLogBuffer{
buffer: bytes.NewBuffer(make([]byte, 0, rotationFileDefaultBufferSize)),
bufferSize: rotationFileDefaultBufferSize,
func NewRotationFileHandler() (handler Handler) {
return &RotationFileHandler{
logFileName: fmt.Sprintf("%v.log", filepath.Base(os.Args[0])),
logDirPath: fmt.Sprintf("/var/log/%v", filepath.Base(os.Args[0])),
maxAge: 7,
async: false,
asyncFlushInterval: rotationFileDefaultAsyncFlushInterval,
buffer: bytes.NewBuffer(make([]byte, 0, rotationFileDefaultBufferSize)),
bufferSize: rotationFileDefaultBufferSize,
mutex: new(sync.Mutex),
}
}

Expand Down

0 comments on commit d790019

Please sign in to comment.