Cache logger instance.

This commit is contained in:
Jonathan Bernard 2025-01-20 06:39:02 -06:00
parent af44d48df1
commit 9d1cc4bbec
2 changed files with 4 additions and 2 deletions

View File

@ -1,6 +1,6 @@
# Package # Package
version = "3.1.0" version = "3.1.1"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "Lightweight Postgres ORM for Nim." description = "Lightweight Postgres ORM for Nim."
license = "GPL-3.0" license = "GPL-3.0"

View File

@ -309,14 +309,16 @@ type
## Error type raised when no record matches a given ID ## Error type raised when no record matches a given ID
var logService {.threadvar.}: Option[LogService] var logService {.threadvar.}: Option[LogService]
var logger {.threadvar.}: Option[Logger]
proc logQuery*(methodName: string, sqlStmt: string, args: openArray[(string, string)] = []) = proc logQuery*(methodName: string, sqlStmt: string, args: openArray[(string, string)] = []) =
# namespaced_logging would do this check for us, but we don't want to even # namespaced_logging would do this check for us, but we don't want to even
# build the log object if we're not actually logging # build the log object if we're not actually logging
if logService.isNone: return if logService.isNone: return
if logger.isNone: logger = logService.getLogger("fiber_orm/query")
var log = %*{ "method": methodName, "sql": sqlStmt } var log = %*{ "method": methodName, "sql": sqlStmt }
for (k, v) in args: log[k] = %v for (k, v) in args: log[k] = %v
logService.getLogger("fiber_orm/query").debug(log) logger.debug(log)
proc enableDbLogging*(svc: LogService) = proc enableDbLogging*(svc: LogService) =
logService = some(svc) logService = some(svc)