Commit Graph
29 Commits
Author SHA1 Message Date
jdb 46245b3f1e Create log timestamps in UTC to avoid local-time races
Use getTime().utc in both message constructors, document the timezone change, and cover concurrent construction and structured formatting. Restore ordinary logging calls in the parallel writer fixture.

Validation: 32 unit tests and 29 regression tests pass. Timestamp sanitizer checks pass under UTC and non-UTC timezones. The full suite retains the known allocator reports in factory-state and allocator.
2026-09-29 09:03:31 -05:00
jdb 011780e6ab Define cached threshold behavior for isEnabled
Document and test explicit refresh boundaries for isEnabled guards, and
only read the shared root threshold when no namespace threshold applies.
Include the portable Nimble configuration loader.
2026-09-29 08:43:31 -05:00
jdb 14e71c72ca Preserve cached configuration when refresh fails
Build replacement appenders and thresholds before installing them and
advancing the local version. Release partial replacements and old caches
outside the configuration lock.  In order to make the `ensureFreshness`
version check as cheap as posible and only incur overhead when we
actually need to update the local cache, isolate the refresh logic in a
separate scope to prevent initialization and cleanup of temporary
variables when the version has not changed

Add regression coverage for repeated clone failures, cache preservation,
cleanup, and retry without another configuration change. Update
documentation to reflect the remaining freshness issue.
2026-09-29 08:10:12 -05:00
jdb d5e3b9c301 Drain background writers before process exit
Serialize writer submission and shutdown, enqueue explicit stop messages after accepted records, and finish the final file batch before joining. Keep drained process-wide channel storage until exit to avoid the Nim 2.2.10 Channel.close mutex teardown defect.

Cover empty records, multiple destinations, joined producers, and concurrent shutdown. Include writer lifecycle sanitizer tests in nimble test and update usage documentation.

Validation: all 32 unit tests, eight writer exit scenarios, and writer lifecycle ThreadSanitizer tests pass. The full suite retains the known configuration refresh, timestamp, and default allocator failures.
2026-09-28 15:00:18 -05:00
jdb b2d0e9dade Define explicit ownership for custom appenders and formatters
Add SharedLogState for synchronized value data and factories for
independently owned appender state and captured formatter closures.
Reject unsafe configuration state, update the test collector to value
snapshots, and document the breaking APIs in version 3.0.0.

Expand ORC ownership and sanitizer coverage and reject stale test
executables.
2026-09-28 14:28:24 -05:00
jdb 851caef706 Clarify shared logging state and owning handle types 2026-09-22 07:25:38 -05:00
jdb 761bd66793 Separate shared service ownership from thread-local logging caches 2026-09-22 07:18:22 -05:00
jdb 15af03e5c5 Remove global JNull reference. 2025-07-25 11:12:37 -05:00
jdb 7e1671b26e Allow inclusion of Exceptions in warn convenience templates. 2025-07-18 10:26:15 -05:00
jdb f0f0084cfd Changing our mind: message -> msg; timestamp -> ts; error -> err 2025-07-14 15:30:21 -05:00
jdb bff544ab89 Add convenience methods for setting thresholds. 2025-07-12 13:22:17 -05:00
jdb 3178c50936 Remove autoconfigured (didn't work as implemented). 2025-07-11 17:38:04 -05:00
jdb a4464c7275 Add README examples for autoconfigured use-cases. 2025-07-07 21:09:38 -05:00
jdb 49755fa2af Add more unit tests. 2025-07-07 17:02:45 -05:00
jdb b2d43d1c6d StdLoggingAppender - forward logs to std/logging
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, even when namespaced_logging is configured.
2025-07-07 16:34:04 -05:00
jdb c22e7edd5d Move autoconfiguration implementation into the main module.
Autoconfiguration implementation really needs access to internal fields
and data structures to work properly.

Additionally introduces the concept of GlobalLogService takeover
internally, which allows existing LogService instances to become aware
of a new GlobalLogService. This is needed for
`useForAutoconfiguredLogging` to work as one would naturally expect,
where Loggers that may have already been created (explicitly or
implicitly) by library or third-party code are kept up to date when the
application explicitly configures logging.
2025-07-07 16:29:58 -05:00
jdb 269cc81c82 Rewrite the log procs for better performance.
Previously, log message parsing and construction were performed before
we checked if we were actually going to log anything (based on our
thresholds). This change moves the logic for both the LogMessage
construction, as well as any logic in the calling context (string
concatenation etc.) after the threshold check.

This is to enable use-cases like this:

    logger.debug("Something interesting happened\p    context: " &
        expensiveCallToConstructFullContext())

and not have to pay the performance penalty of the string concatenation
in production settings when debug logging is turned off.
2025-07-07 16:21:59 -05:00
jdb 2f761833bd Use message for structures message contents, not msg.
I like `msg`, but `message` is more common and likely to the more
expected name.
2025-07-06 04:12:47 -05:00
jdb f80e5807db refactor: Complete rewrite of logging system with thread-safe architecture
This commit represents a complete architectural overhaul of the namespaced
logging library, transitioning from a shared-memory approach to a robust
thread-safe design with proper synchronization primitives.

- **NEW**: internal `GlobalLogService` with atomic configuration versioning
- **NEW**: Thread-local configuration caching with freshness checking
- **NEW**: Separate `LogService` (copyable) and `ThreadLocalLogService` (ref)
- **REMOVED**: Manual thread state reloading in favor of automatic freshness

- All shared state protected by locks and atomics
- Configuration changes use atomic version numbers for efficient sync
- Proper cleanup with `=destroy` implementing graceful shutdown
- Thread-safe appender cloning via `clone()` method pattern

- **NEW**: Dedicated writer threads for console and file output
- **NEW**: Channel-based message passing to writer threads
- **NEW**: Batched file I/O with optimized write patterns
- **NEW**: Graceful thread shutdown on service destruction

- **NEW**: Configurable error handling with `ErrorHandlerFunc`
- **NEW**: `defaultErrorHandlerFunc` with stderr fallback
- **NEW**: Thread-safe error reporting with separate lock
- **NEW**: Robust error recovery in all I/O operations

- **NEW**: `autoconfigured` module for zero-config usage
- **NEW**: `formatSimpleTextLog` as default formatter
- **NEW**: Optional logger support for ergonomic usage
- **NEW**: Generic `CustomLogAppender[T]` with state parameter
- **NEW**: `FileLogAppender` with proper multithreaded file I/O
- **BREAKING**: Logger `name` field renamed to `scope`
- **BREAKING**: Configuration methods renamed (e.g., `setRootLevel` → `setRootThreshold`)

- **NEW**: Comprehensive test suite with 20+ test cases
- **NEW**: `testutil` module with thread-safe test infrastructure
- **NEW**: Cross-thread synchronization testing
- **NEW**: File I/O testing with temporary files
- **REMOVED**: Old test suite replaced with more comprehensive version

- Atomic version checking prevents unnecessary config copies
- Writer threads use efficient polling with 100ms idle sleep
- File writer batches messages and optimizes file operations
- Thread-local caching reduces lock contention

1. **API Changes**:
   - `LogService` returned by `iniLogService` is fundamentally different
   - `threadLocalRef()` required for thread-local operations
   - `reloadThreadState()` removed (automatic freshness)
   - Logger field `name` → `scope`

2. **Configuration**:
   - `setRootLevel()` → `setRootThreshold()`
   - `setThreshold()` API simplified
   - `clearAppenders()` removed

3. **Appenders**:
   - `initThreadCopy()` → `clone()`
   - `appendLogMessage()` signature changed
   - Custom appenders now generic with state parameter

```nim
let ls = initLogService()
ls.addAppender(initConsoleLogAppender())
reloadThreadState(ls)
let logger = ls.getLogger("app")
logger.info("Hello world")
```

```nim
let ls = initLogService()
let tlls = threadLocalRef(ls)
tlls.addAppender(initConsoleLogAppender())
let logger = tlls.getLogger("app")
logger.info("Hello world")
```
```nim
import namespaced_logging/autoconfigured
addLogAppender(initConsoleLogAppender())
info("Hello world")
```
2025-07-06 01:03:35 -05:00
jdb e0dba8125c Add CustomLogAppender to allow extensible log appenders. 2025-05-19 18:05:06 -05:00
jdb 0eb0d33573 Expose appender config, default ConsoleLogAppender threshold to ALL. 2025-02-04 17:56:01 -06:00
jdb 92c2dec54d Major refactor to better support multi-threading.
See README for details.
2025-01-05 06:25:35 -06:00
jdb ab20a30434 Swallow impossible exception. 2023-02-25 14:45:05 -07:00
jdb f3dbac0896 Fix default prefix format. 2022-11-03 15:14:43 -05:00
jdb c0f818ef30 setLevelForNamespace now instantiates the namespace if necessary
- Hide initLoggingNamespace. getLoggerForNamespace should be used
  instead.
- setLevelForNamespace now calls getLoggerForNamespace to ensure the
  namespace exists when a caller tries to set the logging level.
  Previously it was possible to call setLevelForNamespace before the
  namespace was initialized, meaning that the setting had no effect.
  This is especially problematic for cases where a library is using
  namespaced logging with the filter set to a high level by default.
  The code using that library may want to enable debug logs, and is
  likely to call setLevelForNamespace in initialization code that runs
  prior to the library code which instantiates the namespace.
2022-11-03 15:03:48 -05:00
jdb 8245cfbdf7 Use a table for thread-global cache, hierarchical logging namespaces. 2022-06-04 21:48:25 -05:00
jdb 79c3701f26 Add thread-global cache of namespaces, change default formatting. 2022-01-13 17:29:56 -06:00
jdb 5f21b2f263 Initial implementation. 2022-01-13 14:12:10 -06:00
jdb 3222260baa Initial commit following nimble init. 2022-01-13 13:22:13 -06:00