Skip to content

Commit

Permalink
added more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Mar 2, 2024
1 parent 399cd8d commit 407a298
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions IHP/Controller/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,25 @@ instance HasField "frameworkConfig" ControllerContext FrameworkConfig where
-- The following hack is bad, but allows us to override the logger using 'putContext'
-- The alternative would be https://github.com/digitallyinduced/ihp/pull/1921 which is also not very nice
--
-- This can be useful to customize the log formatter for all actions of an app:
--
-- > import IHP.Log.Types
-- > import IHP.Controller.Context
-- >
-- > instance InitControllerContext WebApplication where
-- > initContext = do
-- > let defaultLogger :: Logger = ?context.frameworkConfig.logger
-- > let withUserIdLogger :: Logger = { Log.formatter = userIdFormatter defaultLogger.formatter }
-- > putContext withUserIdLogger
-- >
-- > userIdFormatter :: (?context :: Context) => Log.LogFormatter -> Log.LogFormatter
-- > userIdFormatter existingFormatter time level string =
-- > existingFormatter time level (prependUserId string)
-- >
-- > preprendUserId :: (?context :: Context) => Text -> Text
-- > preprendUserId string = "userId: " <> show currentUserId <> " " <> string
--
-- This design mistake should be fixed in IHP v2
instance HasField "logger" ControllerContext Logger where
getField context@(FrozenControllerContext { customFields }) = fromMaybe context.frameworkConfig.logger (TypeMap.lookup @Logger)
getField context = (unsafePerformIO (freeze context)).logger
getField context@(FrozenControllerContext { customFields }) = fromMaybe context.frameworkConfig.logger (TypeMap.lookup @Logger customFields)
getField context = (unsafePerformIO (freeze context)).logger -- Hacky, but there's no better way. The only way to retrieve the logger here, is by reading from the IORef in an unsafe way

0 comments on commit 407a298

Please sign in to comment.