Skip to content

Commit 0c1063d

Browse files
committed
Made log of queries a bit less verbose
1 parent 39dfead commit 0c1063d

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

IHP/ModelSupport.hs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ textToId text = case parsePrimaryKey (cs text) of
342342

343343
-- | Measure and log the query time for a given query action if the log level is Debug.
344344
-- If the log level is greater than debug, just perform the query action without measuring time.
345-
measureTimeIfLogging :: (?modelContext :: ModelContext, Show q) => IO a -> Query -> q -> IO a
345+
measureTimeIfLogging :: (?modelContext :: ModelContext, PG.ToRow q) => IO a -> Query -> q -> IO a
346346
measureTimeIfLogging queryAction theQuery theParameters = do
347347
let currentLogLevel = get #logger ?modelContext |> get #level
348348
if currentLogLevel == Debug
@@ -364,7 +364,7 @@ measureTimeIfLogging queryAction theQuery theParameters = do
364364
--
365365
-- *AutoRefresh:* When using 'sqlQuery' with AutoRefresh, you need to use 'trackTableRead' to let AutoRefresh know that you have accessed a certain table. Otherwise AutoRefresh will not watch table of your custom sql query.
366366
--
367-
sqlQuery :: (?modelContext :: ModelContext, PG.ToRow q, PG.FromRow r, Show q) => Query -> q -> IO [r]
367+
sqlQuery :: (?modelContext :: ModelContext, PG.ToRow q, PG.FromRow r) => Query -> q -> IO [r]
368368
sqlQuery theQuery theParameters = do
369369
measureTimeIfLogging
370370
(withDatabaseConnection \connection -> enhanceSqlError theQuery theParameters do
@@ -379,7 +379,7 @@ sqlQuery theQuery theParameters = do
379379
-- __Example:__
380380
--
381381
-- > sqlExec "CREATE TABLE users ()" ()
382-
sqlExec :: (?modelContext :: ModelContext, PG.ToRow q, Show q) => Query -> q -> IO Int64
382+
sqlExec :: (?modelContext :: ModelContext, PG.ToRow q) => Query -> q -> IO Int64
383383
sqlExec theQuery theParameters = do
384384
measureTimeIfLogging
385385
(withDatabaseConnection \connection -> enhanceSqlError theQuery theParameters do
@@ -426,7 +426,7 @@ withDatabaseConnection block =
426426
-- > usersCount <- sqlQueryScalar "SELECT COUNT(*) FROM users"
427427
--
428428
-- Take a look at "IHP.QueryBuilder" for a typesafe approach on building simple queries.
429-
sqlQueryScalar :: (?modelContext :: ModelContext) => (PG.ToRow q, Show q, FromField value) => Query -> q -> IO value
429+
sqlQueryScalar :: (?modelContext :: ModelContext) => (PG.ToRow q, FromField value) => Query -> q -> IO value
430430
sqlQueryScalar theQuery theParameters = do
431431
result <- measureTimeIfLogging
432432
(withDatabaseConnection \connection -> enhanceSqlError theQuery theParameters do
@@ -446,7 +446,7 @@ sqlQueryScalar theQuery theParameters = do
446446
-- > usersCount <- sqlQueryScalarOrNothing "SELECT COUNT(*) FROM users"
447447
--
448448
-- Take a look at "IHP.QueryBuilder" for a typesafe approach on building simple queries.
449-
sqlQueryScalarOrNothing :: (?modelContext :: ModelContext) => (PG.ToRow q, Show q, FromField value) => Query -> q -> IO (Maybe value)
449+
sqlQueryScalarOrNothing :: (?modelContext :: ModelContext) => (PG.ToRow q, FromField value) => Query -> q -> IO (Maybe value)
450450
sqlQueryScalarOrNothing theQuery theParameters = do
451451
result <- measureTimeIfLogging
452452
(withDatabaseConnection \connection -> enhanceSqlError theQuery theParameters do
@@ -584,7 +584,7 @@ class
584584
default primaryKeyCondition :: forall id. (HasField "id" record id, ToField id) => record -> [(Text, PG.Action)]
585585
primaryKeyCondition record = [("id", toField (get #id record))]
586586

587-
logQuery :: (?modelContext :: ModelContext, Show query, Show parameters) => query -> parameters -> NominalDiffTime -> IO ()
587+
logQuery :: (?modelContext :: ModelContext, PG.ToRow parameters) => Query -> parameters -> NominalDiffTime -> IO ()
588588
logQuery query parameters time = do
589589
let ?context = ?modelContext
590590
-- NominalTimeDiff is represented as seconds, and doesn't provide a FormatTime option for printing in ms.
@@ -595,7 +595,14 @@ logQuery query parameters time = do
595595
Just RowLevelSecurityContext { rlsUserId = PG.Plain rlsUserId } -> formatRLSInfo (cs (Builder.toLazyByteString rlsUserId))
596596
Just RowLevelSecurityContext { rlsUserId = rlsUserId } -> formatRLSInfo (tshow rlsUserId)
597597
Nothing -> ""
598-
Log.debug ("Query (" <> tshow queryTimeInMs <> "ms): " <> tshow query <> " " <> tshow parameters <> rlsInfo)
598+
let
599+
-- We don't use the normal 'show' here as it adds lots of noise like 'Escape' or 'Plain' to the output
600+
showAction (PG.Plain builder) = cs (Builder.toLazyByteString builder)
601+
showAction (PG.Escape byteString) = cs byteString
602+
showAction (PG.EscapeByteA byteString) = cs byteString
603+
showAction (PG.EscapeIdentifier byteString) = cs byteString
604+
showAction (PG.Many actions) = concatMap showAction actions
605+
Log.debug ("Query (" <> tshow queryTimeInMs <> "ms): " <> tshow query <> " [" <> (intercalate ", " $ map showAction $ PG.toRow parameters) <> "]" <> rlsInfo)
599606
{-# INLINABLE logQuery #-}
600607

601608
-- | Runs a @DELETE@ query for a record.

0 commit comments

Comments
 (0)