Add README examples for autoconfigured use-cases.

This commit is contained in:
2025-07-07 21:09:38 -05:00
parent 49755fa2af
commit a4464c7275
2 changed files with 59 additions and 1 deletions

View File

@ -136,6 +136,43 @@ proc configureLogging(localLogSvc: ThreadLocalLogService, verbose: bool) =
# Changes automatically propagate to all threads # Changes automatically propagate to all threads
``` ```
### Autoconfigured Logging in Library Code, Falling Back to `std/logging`
One of the primary uses-cases for the autoconfigured option is for use in
libraries or other packaged code where the main application may not be using
or even aware of namespaced\_logging, especially when paired with the
[*StdLoggingAppender*][#StandingLoggingAppender], which can be configured to
fallback to std/logging when no appenders have been configured for
namespaced\_logging.
```nim
import namespaced_logging/autoconfigured
# Add a StdLoggingAppender to forward logs to std/logging
addLogAppender(initStdLoggingAppender(fallbackOnly = true))
# will be forwarded to std/logging.debug
debug("log from library code")
addLogAppender(initConsoleLogAppender())
# will no longer be forwarded to std/logging.debug
debug("log from library code")
```
### Providing A Custom Configuration to Replace Autoconfigured Service
```nim
import namespace_logging
var ls = initLogService()
ls.addAppender(initConsoleLogAppender())
useForAutoconfiguredLogging(ls)
# from this point on any autoconfigured LogService or Loggers will use the
# configuration defined by ls
```
## Loggers and Appenders ## Loggers and Appenders
The logging system is composed of two main components: loggers and appenders. The logging system is composed of two main components: loggers and appenders.
@ -225,6 +262,26 @@ by destination file, opens the file with mode `fmAppend`, writes the current
batch of log messages, and then closes the file handle. Because of this, it has batch of log messages, and then closes the file handle. Because of this, it has
no problem if another process moves or truncates any of the target log files. no problem if another process moves or truncates any of the target log files.
### StdLoggingAppender
Provides a fallback to [std/logging][std-logging]-based logging. This is
primarily intended for use in libraries or other contexts where you want to
fall back to std/logging if the application is not using or hasn't configured
namespaced\_logging.
By default the *StdLoggingAppender* only logs when no namespaced\_logging
appenders are configured but it can also be configured to always forward log
messages regardless of whether namespaced\_logging has other appenders by
setting `fallbackOnly = false`.
```nim
func initStdLoggingAppender*(
fallbackOnly = true,
formatter = formatForwardedLog,
namespace = "",
threshold = lvlAll): StdLoggingAppender {.gcsafe.}
```
### CustomLogAppender ### CustomLogAppender
Provides an extension point for custom logging implementations. Provides an extension point for custom logging implementations.
@ -488,6 +545,7 @@ logService.setErrorHandler(silentErrorHandler)
### Best Practices ### Best Practices
#### Provide Fallbacks #### Provide Fallbacks
```nim ```nim
proc robustErrorHandler(err: ref Exception, msg: string) {.gcsafe, nimcall.} = proc robustErrorHandler(err: ref Exception, msg: string) {.gcsafe, nimcall.} =
# Primary: Send to monitoring system # Primary: Send to monitoring system

View File

@ -862,7 +862,7 @@ func initStdLoggingAppender*(
fallbackOnly = true, fallbackOnly = true,
formatter = formatForwardedLog, formatter = formatForwardedLog,
namespace = "", namespace = "",
threshold = lvlAll): StdLoggingAppender = threshold = lvlAll): StdLoggingAppender {.gcsafe.} =
result = StdLoggingAppender( result = StdLoggingAppender(
namespace: namespace, namespace: namespace,