Merge pull request #1001 from ygale/exportLogFormatting

Export log formatting
This commit is contained in:
Michael Snoyman 2015-06-04 09:41:22 +03:00
commit f2b435fa35
2 changed files with 50 additions and 14 deletions

View File

@ -30,6 +30,11 @@ module Yesod.Core
, AuthResult (..) , AuthResult (..)
, unauthorizedI , unauthorizedI
-- * Logging -- * Logging
, defaultMakeLogger
, defaultMessageLoggerSource
, defaultShouldLog
, defaultShouldLogIO
, formatLogMessage
, LogLevel (..) , LogLevel (..)
, logDebug , logDebug
, logInfo , logInfo

View File

@ -26,7 +26,6 @@ import Data.Aeson (object, (.=))
import Data.List (foldl') import Data.List (foldl')
import Data.List (nub) import Data.List (nub)
import qualified Data.Map as Map import qualified Data.Map as Map
import Data.Maybe (fromMaybe)
import Data.Monoid import Data.Monoid
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
@ -207,17 +206,14 @@ class RenderRoute site => Yesod site where
-- method return that already created value. That way, you can use that -- method return that already created value. That way, you can use that
-- same @Logger@ for printing messages during app initialization. -- same @Logger@ for printing messages during app initialization.
-- --
-- Default: Sends to stdout and automatically flushes on each write. -- Default: the 'defaultMakeLogger' function.
makeLogger :: site -> IO Logger makeLogger :: site -> IO Logger
makeLogger _ = do makeLogger _ = defaultMakeLogger
loggerSet' <- newStdoutLoggerSet defaultBufSize
(getter, _) <- clockDateCacher
return $! Logger loggerSet' getter
-- | Send a message to the @Logger@ provided by @getLogger@. -- | Send a message to the @Logger@ provided by @getLogger@.
-- --
-- Default implementation: checks if the message should be logged using -- Default: the 'defaultMessageLoggerSource' function, using
-- 'shouldLog' and, if so, formats using 'formatLogMessage'. -- 'shouldLogIO' to check whether we should log.
messageLoggerSource :: site messageLoggerSource :: site
-> Logger -> Logger
-> Loc -- ^ position in source code -> Loc -- ^ position in source code
@ -225,10 +221,7 @@ class RenderRoute site => Yesod site where
-> LogLevel -> LogLevel
-> LogStr -- ^ message -> LogStr -- ^ message
-> IO () -> IO ()
messageLoggerSource a logger loc source level msg = do messageLoggerSource site = defaultMessageLoggerSource $ shouldLogIO site
sl <- shouldLogIO a source level
when sl $
formatLogMessage (loggerDate logger) loc source level msg >>= loggerPutStr logger
-- | Where to Load sripts from. We recommend the default value, -- | Where to Load sripts from. We recommend the default value,
-- 'BottomOfBody'. Alternatively use the built in async yepnope loader: -- 'BottomOfBody'. Alternatively use the built in async yepnope loader:
@ -260,9 +253,9 @@ class RenderRoute site => Yesod site where
-- | Should we log the given log source/level combination. -- | Should we log the given log source/level combination.
-- --
-- Default: Logs everything at or above 'logLevel' -- Default: the 'defaultShouldLog' function.
shouldLog :: site -> LogSource -> LogLevel -> Bool shouldLog :: site -> LogSource -> LogLevel -> Bool
shouldLog _ _ level = level >= LevelInfo shouldLog _ = defaultShouldLog
-- | Should we log the given log source/level combination. -- | Should we log the given log source/level combination.
-- --
@ -300,6 +293,43 @@ class RenderRoute site => Yesod site where
yesodWithInternalState _ _ = bracket createInternalState closeInternalState yesodWithInternalState _ _ = bracket createInternalState closeInternalState
{-# INLINE yesodWithInternalState #-} {-# INLINE yesodWithInternalState #-}
-- | Default implementation of 'makeLogger'. Sends to stdout and
-- automatically flushes on each write.
defaultMakeLogger :: IO Logger
defaultMakeLogger = do
loggerSet' <- newStdoutLoggerSet defaultBufSize
(getter, _) <- clockDateCacher
return $! Logger loggerSet' getter
-- | Default implementation of 'messageLoggerSource'. Checks if the
-- message should be logged using the provided function, and if so,
-- formats using 'formatLogMessage'. You can use 'defaultShouldLogIO'
-- as the provided function.
defaultMessageLoggerSource ::
(LogSource -> LogLevel -> IO Bool) -- ^ Check whether we should
-- log this
-> Logger
-> Loc -- ^ position in source code
-> LogSource
-> LogLevel
-> LogStr -- ^ message
-> IO ()
defaultMessageLoggerSource ckLoggable logger loc source level msg = do
loggable <- ckLoggable source level
when loggable $
formatLogMessage (loggerDate logger) loc source level msg >>=
loggerPutStr logger
-- | Default implementation of 'shouldLog'. Logs everything at or
-- above 'LevelInfo'.
defaultShouldLog :: LogSource -> LogLevel -> Bool
defaultShouldLog _ level = level >= LevelInfo
-- | A default implementation of 'shouldLogIO' that can be used with
-- 'defaultMessageLoggerSource'. Just uses 'defaultShouldLog'.
defaultShouldLogIO :: LogSource -> LogLevel -> IO Bool
defaultShouldLogIO a b = return $ defaultShouldLog a b
-- | Default implementation of 'yesodMiddleware'. Adds the response header -- | Default implementation of 'yesodMiddleware'. Adds the response header
-- \"Vary: Accept, Accept-Language\" and performs authorization checks. -- \"Vary: Accept, Accept-Language\" and performs authorization checks.
-- --
@ -577,6 +607,7 @@ asyncHelper render scripts jscript jsLoc =
Nothing -> Nothing Nothing -> Nothing
Just j -> Just $ jelper j Just j -> Just $ jelper j
-- | Default formatting for log messages.
formatLogMessage :: IO ZonedDate formatLogMessage :: IO ZonedDate
-> Loc -> Loc
-> LogSource -> LogSource