diff --git a/examples/README.md b/examples/README.md index c8e1696..61e0203 100644 --- a/examples/README.md +++ b/examples/README.md @@ -2,13 +2,24 @@ This package contains examples how to use logger. +## customasynclogger + +Demonstrates how to create a new custom logger for the application. + +It creates an async logger with provided queue size that writes messages on the console asynchronously. It also provides +an example how to wait for all messages to be written. + ## customlogger -Demonstrates how to create a new custom logger for the application. It creates a simple logger that writes messages on the console. +Demonstrates how to create a new custom logger for the application. + +It creates a simple logger that writes messages on the console. ## customstructuredlogger -Demonstrates how to create a new custom logger for the application. It creates a simple logger that writes messages on the console in structured format. +Demonstrates how to create a new custom logger for the application. + +It creates a simple logger that writes messages on the console in structured format. ## defaultlogger diff --git a/examples/customasynclogger/main.go b/examples/customasynclogger/main.go new file mode 100644 index 0000000..4728a1b --- /dev/null +++ b/examples/customasynclogger/main.go @@ -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() +}