-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from dl1998/async-logger
Add async logger
- Loading branch information
Showing
13 changed files
with
2,136 additions
and
1,559 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2,442 changes: 1,302 additions & 1,140 deletions
2,442
docs/architecture/diagrams/svg/class_diagram.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,29 @@ | ||
// Example that shows how to create and use custom async logger. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/dl1998/go-logging/pkg/common/level" | ||
"github.com/dl1998/go-logging/pkg/logger" | ||
"github.com/dl1998/go-logging/pkg/logger/formatter" | ||
"github.com/dl1998/go-logging/pkg/logger/handler" | ||
"time" | ||
) | ||
|
||
func main() { | ||
asyncQueueSize := 10 | ||
|
||
applicationLogger := logger.NewAsyncLogger("example", time.DateTime, asyncQueueSize) | ||
|
||
applicationFormatter := formatter.New("%(datetime) [%(level)] %(message)") | ||
consoleHandler := handler.NewConsoleHandler(level.Debug, level.Null, applicationFormatter) | ||
applicationLogger.AddHandler(consoleHandler) | ||
|
||
for index := 0; index < asyncQueueSize; index++ { | ||
applicationLogger.Warning("This message will be displayed.") | ||
} | ||
|
||
fmt.Println("This will be printed before the last warning log message.") | ||
|
||
applicationLogger.WaitToFinishLogging() | ||
} |
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,101 @@ | ||
package logger | ||
|
||
import ( | ||
"github.com/dl1998/go-logging/pkg/common/level" | ||
"github.com/dl1998/go-logging/pkg/logger/handler" | ||
"github.com/dl1998/go-logging/pkg/logger/logrecord" | ||
"sync" | ||
) | ||
|
||
// baseAsyncLogger struct contains basic fields for the async logger. | ||
type baseAsyncLogger struct { | ||
// baseLogger is a base logger. | ||
*baseLogger | ||
// messageQueue is a channel for the log messages. | ||
messageQueue chan logrecord.Interface | ||
// waitGroup is a wait group for the async logger messages. | ||
waitGroup sync.WaitGroup | ||
} | ||
|
||
// startListeningMessages starts listening for messages in the messageQueue. | ||
func (logger *baseAsyncLogger) startListeningMessages() { | ||
for record := range logger.messageQueue { | ||
for _, registeredHandler := range logger.handlers { | ||
registeredHandler.Write(record) | ||
} | ||
logger.waitGroup.Done() | ||
} | ||
} | ||
|
||
// WaitToFinishLogging waits for all messages to be logged. | ||
func (logger *baseAsyncLogger) WaitToFinishLogging() { | ||
logger.waitGroup.Wait() | ||
} | ||
|
||
// Open opens the messageQueue with the provided queueSize and starts listening | ||
// for messages. | ||
func (logger *baseAsyncLogger) Open(queueSize int) { | ||
logger.messageQueue = make(chan logrecord.Interface, queueSize) | ||
go logger.startListeningMessages() | ||
} | ||
|
||
// Close closes the messageQueue. | ||
func (logger *baseAsyncLogger) Close() { | ||
close(logger.messageQueue) | ||
} | ||
|
||
// Log logs interpolated message with the provided level.Level. | ||
func (logger *baseAsyncLogger) Log(level level.Level, message string, parameters ...any) { | ||
logger.waitGroup.Add(1) | ||
record := logrecord.New(logger.name, level, logger.timeFormat, message, parameters, 3) | ||
logger.messageQueue <- record | ||
} | ||
|
||
// AsyncLoggerInterface defines async logger interface. | ||
type AsyncLoggerInterface interface { | ||
Interface | ||
WaitToFinishLogging() | ||
Open(queueSize int) | ||
Close() | ||
} | ||
|
||
// AsyncLogger represents an asynchronous logger. | ||
type AsyncLogger struct { | ||
// Logger is a standard logger. | ||
*Logger | ||
} | ||
|
||
// NewAsyncLogger creates a new AsyncLogger with the provided name, timeFormat, and queueSize. | ||
func NewAsyncLogger(name string, timeFormat string, queueSize int) *AsyncLogger { | ||
newBaseLogger := &baseAsyncLogger{ | ||
baseLogger: &baseLogger{ | ||
name: name, | ||
timeFormat: timeFormat, | ||
handlers: make([]handler.Interface, 0), | ||
}, | ||
messageQueue: make(chan logrecord.Interface, queueSize), | ||
waitGroup: sync.WaitGroup{}, | ||
} | ||
go newBaseLogger.startListeningMessages() | ||
return &AsyncLogger{ | ||
Logger: &Logger{ | ||
baseLogger: newBaseLogger, | ||
}, | ||
} | ||
} | ||
|
||
// WaitToFinishLogging waits for all messages to be logged. | ||
func (logger *AsyncLogger) WaitToFinishLogging() { | ||
logger.baseLogger.(*baseAsyncLogger).WaitToFinishLogging() | ||
} | ||
|
||
// Open opens the messageQueue with the provided queueSize and starts listening | ||
// for messages. | ||
func (logger *AsyncLogger) Open(queueSize int) { | ||
logger.baseLogger.(*baseAsyncLogger).Open(queueSize) | ||
} | ||
|
||
// Close closes the messageQueue. | ||
func (logger *AsyncLogger) Close() { | ||
logger.baseLogger.(*baseAsyncLogger).Close() | ||
} |
Oops, something went wrong.