Skip to content

Commit

Permalink
Add example how to use custom async logger
Browse files Browse the repository at this point in the history
  • Loading branch information
dl1998 committed Mar 31, 2024
1 parent 6d87693 commit d268c35
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
15 changes: 13 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 29 additions & 0 deletions examples/customasynclogger/main.go
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()
}

0 comments on commit d268c35

Please sign in to comment.