From 407a2987b3ef8ec6c363059e5d85d54624960c0e Mon Sep 17 00:00:00 2001 From: Marc Scholten Date: Sat, 2 Mar 2024 22:41:15 +0100 Subject: [PATCH] added more comments --- IHP/Controller/Context.hs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/IHP/Controller/Context.hs b/IHP/Controller/Context.hs index 9a89b750e..0ddb744cd 100644 --- a/IHP/Controller/Context.hs +++ b/IHP/Controller/Context.hs @@ -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