Skip to content

Commit

Permalink
Merge pull request #1001 from ygale/exportLogFormatting
Browse files Browse the repository at this point in the history
Export log formatting
  • Loading branch information
snoyberg committed Jun 4, 2015
2 parents b1bc30a + f3d9bb2 commit f2b435f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
5 changes: 5 additions & 0 deletions yesod-core/Yesod/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ module Yesod.Core
, AuthResult (..)
, unauthorizedI
-- * Logging
, defaultMakeLogger
, defaultMessageLoggerSource
, defaultShouldLog
, defaultShouldLogIO
, formatLogMessage
, LogLevel (..)
, logDebug
, logInfo
Expand Down
59 changes: 45 additions & 14 deletions yesod-core/Yesod/Core/Class/Yesod.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import Data.Aeson (object, (.=))
import Data.List (foldl')
import Data.List (nub)
import qualified Data.Map as Map
import Data.Maybe (fromMaybe)
import Data.Monoid
import Data.Text (Text)
import qualified Data.Text as T
Expand Down Expand Up @@ -207,28 +206,22 @@ class RenderRoute site => Yesod site where
-- method return that already created value. That way, you can use that
-- 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 _ = do
loggerSet' <- newStdoutLoggerSet defaultBufSize
(getter, _) <- clockDateCacher
return $! Logger loggerSet' getter
makeLogger _ = defaultMakeLogger

-- | Send a message to the @Logger@ provided by @getLogger@.
--
-- Default implementation: checks if the message should be logged using
-- 'shouldLog' and, if so, formats using 'formatLogMessage'.
-- Default: the 'defaultMessageLoggerSource' function, using
-- 'shouldLogIO' to check whether we should log.
messageLoggerSource :: site
-> Logger
-> Loc -- ^ position in source code
-> LogSource
-> LogLevel
-> LogStr -- ^ message
-> IO ()
messageLoggerSource a logger loc source level msg = do
sl <- shouldLogIO a source level
when sl $
formatLogMessage (loggerDate logger) loc source level msg >>= loggerPutStr logger
messageLoggerSource site = defaultMessageLoggerSource $ shouldLogIO site

-- | Where to Load sripts from. We recommend the default value,
-- 'BottomOfBody'. Alternatively use the built in async yepnope loader:
Expand Down Expand Up @@ -260,9 +253,9 @@ class RenderRoute site => Yesod site where

-- | 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 _ _ level = level >= LevelInfo
shouldLog _ = defaultShouldLog

-- | Should we log the given log source/level combination.
--
Expand Down Expand Up @@ -300,6 +293,43 @@ class RenderRoute site => Yesod site where
yesodWithInternalState _ _ = bracket createInternalState closeInternalState
{-# 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
-- \"Vary: Accept, Accept-Language\" and performs authorization checks.
--
Expand Down Expand Up @@ -577,6 +607,7 @@ asyncHelper render scripts jscript jsLoc =
Nothing -> Nothing
Just j -> Just $ jelper j

-- | Default formatting for log messages.
formatLogMessage :: IO ZonedDate
-> Loc
-> LogSource
Expand Down

0 comments on commit f2b435f

Please sign in to comment.