haskellloggingformattingyesod

How can date/time format of Yesod logger be configured?


I have a Haskell Yesod webserver which works perfectly, and logs perfectly1 !

postFailed :: String -> String -> Handler Value
postFailed errorMsg _filename = do
    $logInfoS "(Parser)" (Data.Text.pack errorMsg)
    returnJson (Error "FAILED" errorMsg _filename)

How can I control date/time format? removal of source code loc @(main:Main ...), etc.?

20/Oct/2024:13:12:39 +0000 [Info#(Parser)] Error[< ... omitted ... >] @(main:Main src/Main.hs:76:6)

When I look at the docs, I can't find anything related there.


1 The complete code is here


Solution

  • The logging in general is configured via the makeLogger method of your Yesod instance, which you can override. This is defaultMakeLogger by default, which in turn is defined as

    defaultMakeLogger :: IO Logger
    defaultMakeLogger = do
        loggerSet' <- newStdoutLoggerSet defaultBufSize
        (getter, _) <- clockDateCacher
        return $! Logger loggerSet' getter
    

    This getter here is simply a IO ByteString that provides the current datetime already formatted, so that's what you'd replace if you want a different format.

    You can use newTimeCache from fast-logger for this purpose, that has the right signature and lets you pass in a time format in strftime format. Or you can put in anything else you like, as long as it's a IO ByteString that returns the current time formatted however you like.