-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: use ringBuffer to make log not blocking and configurable
Signed-off-by: Starnop <[email protected]>
- Loading branch information
Showing
8 changed files
with
227 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package logbuffer | ||
|
||
import ( | ||
"github.com/alibaba/pouch/daemon/logger" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// LogBuffer is uses to cache the container's logs with ringBuffer. | ||
type LogBuffer struct { | ||
ringBuffer *RingBuffer | ||
logger logger.LogDriver | ||
} | ||
|
||
// NewLogBuffer return a new BufferLog. | ||
func NewLogBuffer(logDriver logger.LogDriver, maxBytes int64) (logger.LogDriver, error) { | ||
bl := &LogBuffer{ | ||
logger: logDriver, | ||
ringBuffer: NewRingBuffer(maxBytes), | ||
} | ||
|
||
// use a goroutine to write logs continuously with specified log driver | ||
go bl.run() | ||
return bl, nil | ||
} | ||
|
||
// Name return the log driver's name. | ||
func (bl *LogBuffer) Name() string { | ||
return bl.logger.Name() | ||
} | ||
|
||
// WriteLogMessage will write the LogMessage to the ringBuffer. | ||
func (bl *LogBuffer) WriteLogMessage(msg *logger.LogMessage) error { | ||
return bl.ringBuffer.Push(msg) | ||
} | ||
|
||
// Close close the ringBuffer and drain the messages. | ||
func (bl *LogBuffer) Close() error { | ||
bl.ringBuffer.Close() | ||
for _, msg := range bl.ringBuffer.Drain() { | ||
if err := bl.logger.WriteLogMessage(msg); err != nil { | ||
logrus.Debugf("failed to write log %v when closing with log driver %s", msg, bl.logger.Name()) | ||
} | ||
} | ||
|
||
return bl.logger.Close() | ||
} | ||
|
||
// write logs continuously with specified log driver from ringBuffer. | ||
func (bl *LogBuffer) run() { | ||
for { | ||
msg, err := bl.ringBuffer.Pop() | ||
if err != nil { | ||
return | ||
} | ||
|
||
if err := bl.logger.WriteLogMessage(msg); err != nil { | ||
logrus.Debugf("failed to write log %v with log driver %s", msg, bl.logger.Name()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.