Use ISO8601 for logging

This commit is contained in:
Paul Brinkmeier 2025-08-25 16:42:34 +02:00
parent c5846599ea
commit c1e79230ba

View File

@ -1,6 +1,7 @@
module Yore.Log (Yore.Log.error, info) where
import Data.Time (getZonedTime)
import Data.Time (ZonedTime, getZonedTime)
import Data.Time.Format (defaultTimeLocale, formatTime)
import GHC.Stack (HasCallStack, SrcLoc (..), callStack, getCallStack)
import Text.Printf (printf)
@ -14,9 +15,14 @@ doLog :: (HasCallStack) => String -> String -> IO ()
doLog level msg = do
now <- getZonedTime
let location = getLocation $ getCallStack callStack
printf "(%s) (%s) (%s) %s\n" (show now) location level msg
printf "(%s) (%s) (%s) %s\n" (iso8601Show now) location level msg
where
getLocation :: [(String, SrcLoc)] -> String
-- First entry is always a function from this module, skip it
getLocation (_ : (_, srcLoc) : _) = printf "%s:%d" (srcLocFile srcLoc) (srcLocStartLine srcLoc)
getLocation _ = ""
-- | Unlike 'Data.Time.Format.ISO8601.iso8601Show', this function
-- aligns the second decimals. This is purely for asthetic reasons.
iso8601Show :: ZonedTime -> String
iso8601Show = formatTime defaultTimeLocale "%FT%T%02Q%Ez"