Skip to content

Commit ad413cb

Browse files
authored
Merge pull request #34 from dl1998/update-async-loggers-documentation
Update documentation with async loggers usage examples
2 parents fdd3889 + a148247 commit ad413cb

File tree

1 file changed

+79
-2
lines changed

1 file changed

+79
-2
lines changed

README.md

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ All options available for the configuration are:
108108
| WithFile | "" | Set file where to log messages, if not set, then logging to file will be disabled. |
109109
| WithFormat | "json" | Set format for structured logging.<br/><br/>Could be one of the following<br/><ul><li>json</li><li>key-value</li></ul> |
110110
| WithPretty | false | Set if json message should be pretty printed.<br/>*Option works only with "json" format.* |
111-
| WithKeyValueDelimiter | ":" | Set key-value delimiter (eg. "key=value", where '=' is the delimiter).<br/>*Option works only with "key-value" format.* |
111+
| WithKeyValueDelimiter | "=" | Set key-value delimiter (eg. "key=value", where '=' is the delimiter).<br/>*Option works only with "key-value" format.* |
112112
| WithPairSeparator | " " | Set key-value separator (eg. "key1=value1,key2=value2", where ',' is the separator).<br/>*Option works only with "key-value" format.* |
113113
| WithName | "root" | Set logger name. |
114114
| WithTimeFormat | time.RFC3339 | Set time format for logging message. |
@@ -123,10 +123,22 @@ Alternatively you could create application logger. To do this you would need to
123123
applicationLogger := logger.New("application-logger", time.RFC3339)
124124
```
125125

126+
- Standard async logger
127+
128+
```go
129+
applicationLogger := logger.NewAsyncLogger("application-logger", time.RFC3339, 100)
130+
```
131+
126132
- Structured logger
127133

128134
```go
129-
applicationStructuredLogger := structuredlogger.New("application-logger", time.RFC3339)
135+
applicationLogger := structuredlogger.New("application-logger", time.RFC3339)
136+
```
137+
138+
- Structured async logger
139+
140+
```go
141+
applicationLogger := structuredlogger.NewAsyncLogger("application-logger", time.RFC3339, 100)
130142
```
131143

132144
After this you need to set up it, for this create a new formatter that says how to log the message by providing a
@@ -211,8 +223,11 @@ It takes two additional arguments writer for standard messages and for error mes
211223
After handler has been created it shall be registered.
212224

213225
```go
226+
// Register console stdout handler.
214227
applicationLogger.AddHandler(newConsoleHandler)
228+
// Register console stderr handler.
215229
applicationLogger.AddHandler(newConsoleErrorHandler)
230+
// Register file handler.
216231
applicationLogger.AddHandler(newFileHandler)
217232
```
218233

@@ -225,6 +240,15 @@ arguments.
225240
applicationLogger.Info("My message: %s.", "logged using application logger")
226241
```
227242

243+
- Standard async logger
244+
245+
```go
246+
applicationLogger.Info("My message: %s.", "logged using application async logger")
247+
248+
// Wait for all messages to be logged before exiting the program.
249+
applicationLogger.WaitToFinishLogging()
250+
```
251+
228252
- Structured logger
229253
- Varargs
230254

@@ -240,6 +264,59 @@ applicationLogger.Info("My message: %s.", "logged using application logger")
240264
})
241265
```
242266

267+
- Structured async logger
268+
- Varargs
269+
270+
```go
271+
applicationLogger.Info("message", "Logged using structured logger with varargs.")
272+
273+
// Wait for all messages to be logged before exiting the program.
274+
applicationLogger.WaitToFinishLogging()
275+
```
276+
277+
- Map
278+
279+
```go
280+
applicationLogger.Info(map[string]string{
281+
"message": "Logged using structured logger with map.",
282+
})
283+
284+
// Wait for all messages to be logged before exiting the program.
285+
applicationLogger.WaitToFinishLogging()
286+
```
287+
288+
#### Async Loggers - Additional Information
289+
290+
Async loggers are used to log messages asynchronously. It is useful when you want to log messages without blocking the
291+
main thread. However, you need to wait for all messages to be logged before exiting the program. You can do this by
292+
calling the `WaitToFinishLogging` method, it will block the main thread until all messages are logged. Alternatively,
293+
you can close the logger by calling the `Close` method, it will close the message queue without waiting for all messages
294+
to be logged. This is useful when you want to exit the program without waiting for all messages to be logged. After
295+
calling the `Close` method, you can open the logger again by calling the `Open` method, it accepts the new message queue
296+
size as an argument. `Open` method will open the logger with the new message queue size and start listening for the
297+
messages.
298+
299+
Example that waits for all messages to be logged, then close the logger and open it again with a new message queue size:
300+
301+
```go
302+
for index := 0; index < 1000; index++ {
303+
applicationLogger.Info("Counter: %d.", index)
304+
}
305+
306+
// Wait for all messages to be logged before exiting the program.
307+
applicationLogger.WaitToFinishLogging()
308+
309+
// Close the logger.
310+
applicationLogger.Close()
311+
312+
// Open the logger with a new message queue size.
313+
applicationLogger.Open(100)
314+
```
315+
316+
*Note: if you assign a new message queue size that is smaller than the number of messages sent to the queue, the logger
317+
will add messages to the queue until it is not full, then it will wait (blocking the process) until the message from the
318+
queue will be processed and free up the space in the message queue.*
319+
243320
## Class Diagram
244321

245322
![Class Diagram](./docs/architecture/diagrams/png/class_diagram.png)

0 commit comments

Comments
 (0)