Skip to content

Commit

Permalink
Rename AsyncStructuredLogger to AsyncLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
dl1998 committed Apr 1, 2024
1 parent 1147dce commit b4719de
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion examples/customasyncstructuredlogger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func main() {
asyncQueueSize := 10

applicationLogger := structuredlogger.NewAsyncStructuredLogger("example", time.DateTime, asyncQueueSize)
applicationLogger := structuredlogger.NewAsyncLogger("example", time.DateTime, asyncQueueSize)

applicationFormatter := formatter.NewJSON(map[string]string{
"time": "%(datetime)",
Expand Down
21 changes: 10 additions & 11 deletions pkg/structuredlogger/asyncstructuredlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,22 @@ func (logger *baseAsyncLogger) Log(logLevel level.Level, parameters ...any) {
logger.messageQueue <- logRecord
}

// AsyncStructuredLoggerInterface defines async structured logger interface.
type AsyncStructuredLoggerInterface interface {
// AsyncLoggerInterface defines async structured logger interface.
type AsyncLoggerInterface interface {
Interface
WaitToFinishLogging()
Open(queueSize int)
Close()
}

// AsyncStructuredLogger is a structured logger that logs messages
// asynchronously.
type AsyncStructuredLogger struct {
// AsyncLogger is a structured logger that logs messages asynchronously.
type AsyncLogger struct {
// Logger is a standard structured logger.
*Logger
}

// NewAsyncStructuredLogger creates a new async structured logger.
func NewAsyncStructuredLogger(name string, timeFormat string, queueSize int) *AsyncStructuredLogger {
// NewAsyncLogger creates a new async structured logger.
func NewAsyncLogger(name string, timeFormat string, queueSize int) *AsyncLogger {
newBaseLogger := &baseAsyncLogger{
baseLogger: &baseLogger{
name: name,
Expand All @@ -79,25 +78,25 @@ func NewAsyncStructuredLogger(name string, timeFormat string, queueSize int) *As
waitGroup: sync.WaitGroup{},
}
go newBaseLogger.startListeningMessages()
return &AsyncStructuredLogger{
return &AsyncLogger{
Logger: &Logger{
baseLogger: newBaseLogger,
},
}
}

// WaitToFinishLogging waits for all messages to be logged.
func (logger *AsyncStructuredLogger) WaitToFinishLogging() {
func (logger *AsyncLogger) WaitToFinishLogging() {
logger.baseLogger.(*baseAsyncLogger).WaitToFinishLogging()
}

// Open opens the messageQueue with the provided queueSize and starts listening
// for messages.
func (logger *AsyncStructuredLogger) Open(queueSize int) {
func (logger *AsyncLogger) Open(queueSize int) {
logger.baseLogger.(*baseAsyncLogger).Open(queueSize)
}

// Close closes the messageQueue.
func (logger *AsyncStructuredLogger) Close() {
func (logger *AsyncLogger) Close() {
logger.baseLogger.(*baseAsyncLogger).Close()
}
38 changes: 18 additions & 20 deletions pkg/structuredlogger/asyncstructuredlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,29 +151,27 @@ func BenchmarkBaseAsyncLogger_Log(b *testing.B) {
}
}

// TestNewAsyncStructuredLogger tests that NewAsyncStructuredLogger creates a new
// AsyncStructuredLogger.
func TestNewAsyncStructuredLogger(t *testing.T) {
newAsyncStructuredLogger := NewAsyncStructuredLogger(loggerName, timeFormat, messageQueueSize)
// TestNewAsyncLogger tests that NewAsyncLogger creates a new AsyncLogger.
func TestNewAsyncLogger(t *testing.T) {
newAsyncStructuredLogger := NewAsyncLogger(loggerName, timeFormat, messageQueueSize)

testutils.AssertNotNil(t, newAsyncStructuredLogger)
testutils.AssertEquals(t, loggerName, newAsyncStructuredLogger.Name())
testutils.AssertEquals(t, messageQueueSize, cap(newAsyncStructuredLogger.baseLogger.(*baseAsyncLogger).messageQueue))
}

// BenchmarkNewAsyncStructuredLogger benchmarks NewAsyncStructuredLogger.
func BenchmarkNewAsyncStructuredLogger(b *testing.B) {
// BenchmarkNewAsyncLogger benchmarks NewAsyncLogger.
func BenchmarkNewAsyncLogger(b *testing.B) {
for index := 0; index < b.N; index++ {
NewAsyncStructuredLogger(loggerName, timeFormat, messageQueueSize)
NewAsyncLogger(loggerName, timeFormat, messageQueueSize)
}
}

// TestAsyncStructuredLogger_WaitToFinishLogging tests that
// AsyncStructuredLogger.WaitToFinishLogging waits until async logger finish
// logging messages.
func TestAsyncStructuredLogger_WaitToFinishLogging(t *testing.T) {
// TestAsyncLogger_WaitToFinishLogging tests that AsyncLogger.WaitToFinishLogging
// waits until async logger finish logging messages.
func TestAsyncLogger_WaitToFinishLogging(t *testing.T) {
mockHandler := &MockHandler{}
newAsyncStructuredLogger := &AsyncStructuredLogger{
newAsyncStructuredLogger := &AsyncLogger{
Logger: &Logger{
baseLogger: &baseAsyncLogger{
baseLogger: &baseLogger{
Expand All @@ -199,11 +197,11 @@ func TestAsyncStructuredLogger_WaitToFinishLogging(t *testing.T) {
testutils.AssertEquals(t, true, waited)
}

// TestAsyncStructuredLogger_Open tests that AsyncStructuredLogger.Open creates a
// new message queue and start listening messages.
func TestAsyncStructuredLogger_Open(t *testing.T) {
// TestAsyncLogger_Open tests that AsyncLogger.Open creates a new message queue
// and start listening messages.
func TestAsyncLogger_Open(t *testing.T) {
mockHandler := &MockHandler{}
newAsyncStructuredLogger := &AsyncStructuredLogger{
newAsyncStructuredLogger := &AsyncLogger{
Logger: &Logger{
baseLogger: &baseAsyncLogger{
baseLogger: &baseLogger{
Expand All @@ -221,11 +219,11 @@ func TestAsyncStructuredLogger_Open(t *testing.T) {
testutils.AssertNotNil(t, newAsyncStructuredLogger.baseLogger.(*baseAsyncLogger).messageQueue)
}

// TestAsyncStructuredLogger_Close tests that AsyncStructuredLogger.Close closes
// message queue channel.
func TestAsyncStructuredLogger_Close(t *testing.T) {
// TestAsyncLogger_Close tests that AsyncLogger.Close closes message queue
// channel.
func TestAsyncLogger_Close(t *testing.T) {
mockHandler := &MockHandler{}
newAsyncStructuredLogger := &AsyncStructuredLogger{
newAsyncStructuredLogger := &AsyncLogger{
Logger: &Logger{
baseLogger: &baseAsyncLogger{
baseLogger: &baseLogger{
Expand Down

0 comments on commit b4719de

Please sign in to comment.