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")
```
This commit is contained in:
File diff suppressed because it is too large
Load Diff
98
src/namespaced_logging/autoconfigured.nim
Normal file
98
src/namespaced_logging/autoconfigured.nim
Normal file
@@ -0,0 +1,98 @@
|
||||
import std/[json, options]
|
||||
from logging import Level
|
||||
import ../namespaced_logging
|
||||
|
||||
export
|
||||
# Types
|
||||
Level,
|
||||
Logger,
|
||||
LogAppender,
|
||||
LogMessage,
|
||||
ConsoleLogAppender,
|
||||
CustomLogAppender,
|
||||
CustomLogAppenderFunction,
|
||||
FileLogAppender,
|
||||
|
||||
# Procs/Funcs
|
||||
`%`,
|
||||
initConsoleLogAppender,
|
||||
initCustomLogAppender,
|
||||
initFileLogAppender,
|
||||
formatJsonStructuredLog
|
||||
|
||||
var globalLogServiceRef: ThreadLocalLogService = new(LogService)
|
||||
globalLogServiceRef[] = initLogService()
|
||||
|
||||
var threadLocalLogServiceRef {.threadvar.}: ThreadLocalLogService
|
||||
var defaultLogger {.threadvar.}: Option[Logger]
|
||||
|
||||
|
||||
proc getThreadLocalLogServiceRef(): ThreadLocalLogService {.inline.} =
|
||||
if threadLocalLogServiceRef.isNil:
|
||||
threadLocalLogServiceRef = new(LogService)
|
||||
threadLocalLogServiceRef[] = globalLogServiceRef[]
|
||||
|
||||
return threadLocalLogServiceRef
|
||||
|
||||
proc getDefaultLogger(): Logger {.inline.} =
|
||||
|
||||
if defaultLogger.isNone:
|
||||
defaultLogger = some(getThreadLocalLogServiceRef().getLogger(""))
|
||||
|
||||
return defaultLogger.get
|
||||
|
||||
|
||||
proc useForAutoconfiguredLogging*(ls: LogService) =
|
||||
globalLogServiceRef[] = ls
|
||||
|
||||
|
||||
proc setRootLoggingThreshold*(lvl: Level) =
|
||||
setRootThreshold(getThreadLocalLogServiceRef(), lvl)
|
||||
|
||||
|
||||
proc setLoggingThreshold*(scope: string, lvl: Level) =
|
||||
setThreshold(getThreadLocalLogServiceRef(), scope, lvl)
|
||||
|
||||
|
||||
proc addLogAppender*(appender: LogAppender) =
|
||||
addAppender(getThreadLocalLogServiceRef(), appender)
|
||||
|
||||
|
||||
proc getLogger*(scope: string, lvl: Option[Level] = none[Level]()): Logger =
|
||||
getLogger(getThreadLocalLogServiceRef(), scope, lvl)
|
||||
|
||||
|
||||
proc log*(lvl: Level, msg: string) = getDefaultLogger().log(lvl, msg)
|
||||
proc log*(lvl: Level, msg: JsonNode) = getDefaultLogger().log(lvl, msg)
|
||||
|
||||
proc log*(lvl: Level, error: ref Exception, msg: string) =
|
||||
getDefaultLogger().log(lvl, error, msg)
|
||||
|
||||
template debug*[T](msg: T) = log(lvlDebug, msg)
|
||||
template info*[T](msg: T) = log(lvlInfo, msg)
|
||||
template notice*[T](msg: T) = log(lvlNotice, msg)
|
||||
template warn*[T](msg: T) = log(lvlWarn, msg)
|
||||
template error*[T](msg: T) = log(lvlError, msg)
|
||||
template error*(error: ref Exception, msg: string) = log(lvlError, error, msg)
|
||||
template fatal*[T](msg: T) = log(lvlFatal, msg)
|
||||
template fatal*(error: ref Exception, msg: string) = log(lvlFatal, error, msg)
|
||||
|
||||
when isMainModule:
|
||||
import std/unittest
|
||||
import ./testutil
|
||||
|
||||
suite "Autoconfigured Logging":
|
||||
setup:
|
||||
globalLogServiceRef[] = initLogService()
|
||||
let loggedMessages = initLoggedMessages()
|
||||
let testAppender = initTestLogAppender(loggedMessages)
|
||||
|
||||
test "simple no-config logging":
|
||||
addLogAppender(testAppender)
|
||||
info("test message")
|
||||
|
||||
let lm = loggedMessages.get()
|
||||
check:
|
||||
lm.len == 1
|
||||
lm[0].level == lvlInfo
|
||||
lm[0].message == "test message"
|
||||
51
src/namespaced_logging/testutil.nim
Normal file
51
src/namespaced_logging/testutil.nim
Normal file
@@ -0,0 +1,51 @@
|
||||
import std/[locks, sequtils, syncio, os, times]
|
||||
from logging import Level
|
||||
from ../namespaced_logging import CustomLogAppender, initCustomLogAppender, LogMessage
|
||||
|
||||
type
|
||||
LoggedMessages* = ref object
|
||||
messages*: seq[LogMessage]
|
||||
lock: Lock
|
||||
|
||||
proc initLoggedMessages*(): LoggedMessages =
|
||||
result = LoggedMessages(messages: @[])
|
||||
initLock(result.lock)
|
||||
|
||||
|
||||
proc add*(lm: LoggedMessages, msg: LogMessage) =
|
||||
withLock lm.lock: lm.messages.add(msg)
|
||||
|
||||
|
||||
proc clear*(lm: LoggedMessages) =
|
||||
withLock lm.lock: lm.messages = @[]
|
||||
|
||||
|
||||
proc get*(lm: LoggedMessages): seq[LogMessage] =
|
||||
withLock lm.lock: return lm.messages.mapIt(it)
|
||||
|
||||
|
||||
proc testLogAppenderProc(state: LoggedMessages, msg: LogMessage) {.gcsafe, noconv.} =
|
||||
state.add(msg)
|
||||
|
||||
proc initTestLogAppender*(
|
||||
lm: LoggedMessages,
|
||||
namespace = "",
|
||||
threshold = lvlAll): CustomLogAppender[LoggedMessages] =
|
||||
|
||||
initCustomLogAppender(
|
||||
state = lm,
|
||||
doLogMessage = testLogAppenderProc,
|
||||
namespace = namespace,
|
||||
threshold = threshold)
|
||||
|
||||
|
||||
proc waitForFileContent*(
|
||||
path: string,
|
||||
expectedLines: int,
|
||||
timeoutMs: int = 1000): seq[string] =
|
||||
let startTime = getTime()
|
||||
while (getTime() - startTime).inMilliseconds < timeoutMs:
|
||||
if fileExists(path):
|
||||
result = readLines(path)
|
||||
if result.len >= expectedLines: break
|
||||
sleep(10)
|
||||
Reference in New Issue
Block a user