diff --git a/fiber_orm.nimble b/fiber_orm.nimble index 0b15f2f..4595ebc 100644 --- a/fiber_orm.nimble +++ b/fiber_orm.nimble @@ -11,4 +11,4 @@ srcDir = "src" # Dependencies requires @["nim >= 1.4.0", "uuids"] -requires "namespaced_logging >= 0.3.0" +requires "namespaced_logging >= 1.0.0" diff --git a/src/fiber_orm.nim b/src/fiber_orm.nim index 5b3d569..f3c2cbe 100644 --- a/src/fiber_orm.nim +++ b/src/fiber_orm.nim @@ -284,7 +284,7 @@ ## ## .. _pool.DbConnPool: fiber_orm/pool.html#DbConnPool ## -import std/[logging, macros, options, sequtils, strutils] +import std/[json, macros, options, sequtils, strutils] import db_connector/db_common import namespaced_logging, uuids @@ -321,11 +321,18 @@ type NotFoundError* = object of CatchableError ##\ ## Error type raised when no record matches a given ID -var logNs {.threadvar.}: LoggingNamespace +var logService {.threadvar.}: Option[LogService] -template log(): untyped = - if logNs.isNil: logNs = getLoggerForNamespace(namespace = "fiber_orm", level = lvlNotice) - logNs +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 + # build the log object if we're not actually logging + if logService.isNone: return + var log = %*{ "method": methodName, "sql": sqlStmt } + for (k, v) in args: log[k] = %v + logService.getLogger("fiber_orm/query").debug(log) + +proc enableDbLogging*(svc: LogService) = + logService = some(svc) proc newMutateClauses(): MutateClauses = return MutateClauses( @@ -351,7 +358,7 @@ proc createRecord*[D: DbConnType, T](db: D, rec: T): T = " VALUES (" & mc.placeholders.join(",") & ") " & " RETURNING " & columnNamesForModel(rec).join(",") - log().debug "createRecord: [" & sqlStmt & "]" + logQuery("createRecord", sqlStmt) let newRow = db.getRow(sql(sqlStmt), mc.values) result = rowToModel(T, newRow) @@ -367,7 +374,7 @@ proc updateRecord*[D: DbConnType, T](db: D, rec: T): bool = " SET " & setClause & " WHERE id = ? " - log().debug "updateRecord: [" & sqlStmt & "] id: " & $rec.id + logQuery("updateRecord", sqlStmt, [("id", $rec.id)]) let numRowsUpdated = db.execAffectedRows(sql(sqlStmt), mc.values.concat(@[$rec.id])) return numRowsUpdated > 0; @@ -380,7 +387,7 @@ proc createOrUpdateRecord*[D: DbConnType, T](db: D, rec: T): T = ## Note that this does not perform partial updates, all fields are updated. let findRecordStmt = "SELECT id FROM " & tableName(rec) & " WHERE id = ?" - log().debug "createOrUpdateRecord: [" & findRecordStmt & "] id: " & $rec.id + logQuery("createOrUpdateRecord", findRecordStmt, [("id", $rec.id)]) let rows = db.getAllRows(sql(findRecordStmt), [$rec.id]) if rows.len == 0: result = createRecord(db, rec) @@ -393,7 +400,7 @@ proc createOrUpdateRecord*[D: DbConnType, T](db: D, rec: T): T = template deleteRecord*[D: DbConnType](db: D, modelType: type, id: typed): untyped = ## Delete a record by id. let sqlStmt = "DELETE FROM " & tableName(modelType) & " WHERE id = ?" - log().debug "deleteRecord: [" & sqlStmt & "] id: " & $id + logQuery("deleteRecord", sqlStmt, [("id", $id)]) db.tryExec(sql(sqlStmt), $id) proc deleteRecord*[D: DbConnType, T](db: D, rec: T): bool = @@ -401,7 +408,7 @@ proc deleteRecord*[D: DbConnType, T](db: D, rec: T): bool = ## ## .. _id: #model-class-id-field let sqlStmt = "DELETE FROM " & tableName(rec) & " WHERE id = ?" - log().debug "deleteRecord: [" & sqlStmt & "] id: " & $rec.id + logQuery("deleteRecord", sqlStmt, [("id", $rec.id)]) return db.tryExec(sql(sqlStmt), $rec.id) template getRecord*[D: DbConnType](db: D, modelType: type, id: typed): untyped = @@ -411,7 +418,7 @@ template getRecord*[D: DbConnType](db: D, modelType: type, id: typed): untyped = " FROM " & tableName(modelType) & " WHERE id = ?" - log().debug "getRecord: [" & sqlStmt & "] id: " & $id + logQuery("getRecord", sqlStmt, [("id", $id)]) let row = db.getRow(sql(sqlStmt), @[$id]) if allIt(row, it.len == 0): @@ -448,7 +455,7 @@ template findRecordsWhere*[D: DbConnType]( fetchStmt &= " LIMIT " & $p.pageSize & " OFFSET " & $p.offset - log().debug "findRecordsWhere: [" & fetchStmt & "] values: (" & values.join(", ") & ")" + logQuery("findRecordsWhere", fetchStmt, [("values", values.join(", "))]) let records = db.getAllRows(sql(fetchStmt), values).mapIt(rowToModel(modelType, it)) PagedRecords[modelType]( @@ -480,7 +487,7 @@ template getAllRecords*[D: DbConnType]( fetchStmt &= " LIMIT " & $p.pageSize & " OFFSET " & $p.offset - log().debug "getAllRecords: [" & fetchStmt & "]" + logQuery("getAllRecords", fetchStmt) let records = db.getAllRows(sql(fetchStmt)).mapIt(rowToModel(modelType, it)) PagedRecords[modelType]( @@ -520,7 +527,7 @@ template findRecordsBy*[D: DbConnType]( fetchStmt &= " LIMIT " & $p.pageSize & " OFFSET " & $p.offset - log().debug "findRecordsBy: [" & fetchStmt & "] values (" & values.join(", ") & ")" + logQuery("findRecordsBy", fetchStmt, [("values", values.join(", "))]) let records = db.getAllRows(sql(fetchStmt), values).mapIt(rowToModel(modelType, it)) PagedRecords[modelType]( diff --git a/src/fiber_orm/pool.nim b/src/fiber_orm/pool.nim index a8b46aa..15a82e7 100644 --- a/src/fiber_orm/pool.nim +++ b/src/fiber_orm/pool.nim @@ -4,13 +4,12 @@ ## Simple database connection pooling implementation compatible with Fiber ORM. -import std/[logging, sequtils, strutils, sugar] +import std/[sequtils, strutils, sugar] import db_connector/db_common from db_connector/db_sqlite import getRow, close from db_connector/db_postgres import getRow, close -import namespaced_logging import ./db_common as fiber_db_common type @@ -44,20 +43,12 @@ type cfg: DbConnPoolConfig[D] lastId: int -var logNs {.threadvar.}: LoggingNamespace - -template log(): untyped = - if logNs.isNil: logNs = getLoggerForNamespace(namespace = "fiber_orm/pool", level = lvlNotice) - logNs - proc initDbConnPool*[D: DbConnType](cfg: DbConnPoolConfig[D]): DbConnPool[D] = - log().debug("Initializing new pool (size: " & $cfg.poolSize) result = DbConnPool[D]( conns: @[], cfg: cfg) proc newConn[D: DbConnType](pool: DbConnPool[D]): PooledDbConn[D] = - log().debug("Creating a new connection to add to the pool.") pool.lastId += 1 {.gcsafe.}: let conn = pool.cfg.connect() @@ -68,7 +59,6 @@ proc newConn[D: DbConnType](pool: DbConnPool[D]): PooledDbConn[D] = pool.conns.add(result) proc maintain[D: DbConnType](pool: DbConnPool[D]): void = - log().debug("Maintaining pool. $# connections." % [$pool.conns.len]) pool.conns.keepIf(proc (pc: PooledDbConn[D]): bool = if not pc.free: return true @@ -80,9 +70,6 @@ proc maintain[D: DbConnType](pool: DbConnPool[D]): void = except: discard "" return false ) - log().debug( - "Pruned dead connections. $# connections remaining." % - [$pool.conns.len]) let freeConns = pool.conns.filterIt(it.free) if pool.conns.len > pool.cfg.poolSize and freeConns.len > 0: @@ -94,9 +81,6 @@ proc maintain[D: DbConnType](pool: DbConnPool[D]): void = for culled in toCull: try: culled.conn.close() except: discard "" - log().debug( - "Trimming pool size. Culled $# free connections. $# connections remaining." % - [$toCull.len, $pool.conns.len]) proc take*[D: DbConnType](pool: DbConnPool[D]): tuple[id: int, conn: D] = ## Request a connection from the pool. Returns a DbConn if the pool has free @@ -109,20 +93,15 @@ proc take*[D: DbConnType](pool: DbConnPool[D]): tuple[id: int, conn: D] = pool.maintain let freeConns = pool.conns.filterIt(it.free) - log().debug( - "Providing a new connection ($# currently free)." % [$freeConns.len]) - let reserved = if freeConns.len > 0: freeConns[0] else: pool.newConn() reserved.free = false - log().debug("Reserve connection $#" % [$reserved.id]) return (id: reserved.id, conn: reserved.conn) proc release*[D: DbConnType](pool: DbConnPool[D], connId: int): void = ## Release a connection back to the pool. - log().debug("Reclaiming released connaction $#" % [$connId]) let foundConn = pool.conns.filterIt(it.id == connId) if foundConn.len > 0: foundConn[0].free = true