module IHP.Log
( module IHP.Log.Types
, debug
, info
, warn
, error
, fatal
, unknown
, makeRequestLogger
, defaultRequestLogger
) where
import IHP.HaskellSupport hiding (debug)
import CorePrelude hiding (putStr, putStrLn, print, error, show, log, debug)
import Control.Monad (when)
import IHP.Log.Types
import Network.Wai (Middleware)
import Network.Wai.Middleware.RequestLogger (mkRequestLogger, RequestLoggerSettings, destination)
import qualified Network.Wai.Middleware.RequestLogger as RequestLogger
import Data.Default (Default (def))
import qualified System.Log.FastLogger as FastLogger
log :: (?context :: context, LoggingProvider context, FastLogger.ToLogStr string) => LogLevel -> string -> IO ()
log :: forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
LogLevel -> string -> IO ()
log LogLevel
level string
text = do
LogLevel -> Logger -> string -> IO ()
forall string.
ToLogStr string =>
LogLevel -> Logger -> string -> IO ()
writeLog LogLevel
level context
?context::context
?context.logger string
text
debug :: (?context :: context, LoggingProvider context, FastLogger.ToLogStr string) => string -> IO ()
debug :: forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
string -> IO ()
debug = LogLevel -> string -> IO ()
forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
LogLevel -> string -> IO ()
log LogLevel
Debug
info :: (?context :: context, LoggingProvider context, FastLogger.ToLogStr string) => string -> IO ()
info :: forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
string -> IO ()
info = LogLevel -> string -> IO ()
forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
LogLevel -> string -> IO ()
log LogLevel
Info
warn :: (?context :: context, LoggingProvider context, FastLogger.ToLogStr string) => string -> IO ()
warn :: forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
string -> IO ()
warn = LogLevel -> string -> IO ()
forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
LogLevel -> string -> IO ()
log LogLevel
Warn
error :: (?context :: context, LoggingProvider context, FastLogger.ToLogStr string) => string -> IO ()
error :: forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
string -> IO ()
error = LogLevel -> string -> IO ()
forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
LogLevel -> string -> IO ()
log LogLevel
Error
fatal :: (?context :: context, LoggingProvider context, FastLogger.ToLogStr string) => string -> IO ()
fatal :: forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
string -> IO ()
fatal = LogLevel -> string -> IO ()
forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
LogLevel -> string -> IO ()
log LogLevel
Fatal
unknown :: (?context :: context, LoggingProvider context, FastLogger.ToLogStr string) => string -> IO ()
unknown :: forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
string -> IO ()
unknown = LogLevel -> string -> IO ()
forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
LogLevel -> string -> IO ()
log LogLevel
Unknown
writeLog :: (FastLogger.ToLogStr string) => LogLevel -> Logger -> string -> IO ()
writeLog :: forall string.
ToLogStr string =>
LogLevel -> Logger -> string -> IO ()
writeLog LogLevel
level Logger
logger string
text = do
let write :: (FormattedTime -> LogStr) -> IO ()
write = Logger
logger.write
let formatter :: FormattedTime -> LogLevel -> LogStr -> LogStr
formatter = Logger
logger.formatter
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (LogLevel
level LogLevel -> LogLevel -> Bool
forall a. Ord a => a -> a -> Bool
>= Logger
logger.level) do
(FormattedTime -> LogStr) -> IO ()
write (\FormattedTime
time -> FormattedTime -> LogLevel -> LogStr -> LogStr
formatter FormattedTime
time LogLevel
level (string -> LogStr
forall msg. ToLogStr msg => msg -> LogStr
toLogStr string
text))
makeRequestLogger :: RequestLoggerSettings -> Logger -> IO Middleware
makeRequestLogger :: RequestLoggerSettings -> Logger -> IO Middleware
makeRequestLogger RequestLoggerSettings
settings Logger
logger =
RequestLoggerSettings -> IO Middleware
mkRequestLogger RequestLoggerSettings
settings {
destination = RequestLogger.Callback (\LogStr
logStr ->
let ?context = ?context::Logger
Logger
logger in
LogStr
logStr LogStr -> (LogStr -> FormattedTime) -> FormattedTime
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> LogStr -> FormattedTime
fromLogStr FormattedTime -> (FormattedTime -> IO ()) -> IO ()
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> FormattedTime -> IO ()
forall context string.
(?context::context, LoggingProvider context, ToLogStr string) =>
string -> IO ()
info
)
}
defaultRequestLogger :: Logger -> IO Middleware
defaultRequestLogger :: Logger -> IO Middleware
defaultRequestLogger = RequestLoggerSettings -> Logger -> IO Middleware
makeRequestLogger RequestLoggerSettings
forall a. Default a => a
def