module Network.Mattermost.Logging
(
Logger
, LogEvent(..)
, LogEventType(..)
, mmLoggerInfo
, mmLoggerInfoFilter
, mmLoggerDebug
, mmLoggerDebugFilter
, mmLoggerInfoErr
, mmLoggerInfoFilterErr
, mmLoggerDebugErr
, mmLoggerDebugFilterErr
) where
import Control.Monad (when)
import Data.Time.Clock (getCurrentTime)
import System.IO (Handle, hFlush, hPutStr, stderr
, hIsSeekable, hSeek, SeekMode(..))
import Network.Mattermost.Types.Base
mmLoggerDebugFilter :: (LogEvent -> Bool) -> Handle -> Logger
mmLoggerDebugFilter p h l
| p l = mmLoggerDebug h l
| otherwise = return ()
mmLoggerDebug :: Handle -> Logger
mmLoggerDebug h LogEvent { logFunction = f, logEventType = e } = do
now <- getCurrentTime
canSeek <- hIsSeekable h
when canSeek $ hSeek h SeekFromEnd 0
mapM_ (hPutStr h)
[ "[", show now, "] ", f, ": ", show e, "\n" ]
hFlush h
mmLoggerDebugErr :: Logger
mmLoggerDebugErr = mmLoggerDebug stderr
mmLoggerDebugFilterErr :: (LogEvent -> Bool) -> Logger
mmLoggerDebugFilterErr p l = mmLoggerDebugFilter p stderr l
mmLoggerInfoFilter :: (LogEvent -> Bool) -> Handle -> Logger
mmLoggerInfoFilter p h l
| p l = mmLoggerInfo h l
| otherwise = return ()
mmLoggerInfo :: Handle -> Logger
mmLoggerInfo h LogEvent { logFunction = f, logEventType = e } = do
now <- getCurrentTime
canSeek <- hIsSeekable h
when canSeek $ hSeek h SeekFromEnd 0
mapM_ (hPutStr h)
[ "[", show now, "] ", f, ": ", info e, "\n" ]
hFlush h
where info (HttpRequest m s _) = show m ++ " " ++ s
info (HttpResponse n s _) = show n ++ " from " ++ s
info (WebSocketRequest _) = "websocket request"
info (WebSocketResponse _) = "websocket request"
info WebSocketPing = "websocket ping"
info WebSocketPong = "websocket pong"
mmLoggerInfoErr :: Logger
mmLoggerInfoErr = mmLoggerInfo stderr
mmLoggerInfoFilterErr :: (LogEvent -> Bool) -> Logger
mmLoggerInfoFilterErr p l = mmLoggerInfoFilter p stderr l