module MailchimpSimple.Logger ( LogLevels(..)
, writeLog ) where
import Data.Time
import System.FilePath.Posix
import System.Directory ( doesDirectoryExist , createDirectory )
data LogLevels = ERROR | DEBUG | INFO deriving (Show, Eq)
data Logger = LogEntry LogLevels String String String deriving (Show, Eq)
toString :: Logger -> IO String
toString (LogEntry lLevel lMethod lInputData lMessage) = do
utcTime <- getCurrentTime
let myTime = addUTCTime 19800 utcTime
return $ show myTime ++ ", [" ++ show lLevel ++ "], [" ++ lMethod ++ "], Input: " ++ lInputData ++ ", " ++ lMessage
writeLog :: LogLevels -> String -> String -> String -> IO ()
writeLog lLevel lMethod lInputData lMessage = do
logEntry <- toString (LogEntry lLevel lMethod lInputData lMessage)
let logEntryProcessed = logEntry ++ "\n"
exists <- doesDirectoryExist "log"
if (exists == True) then return () else (createDirectory "log")
if lLevel == ERROR
then appendFile ("log" ++ [pathSeparator] ++ "error.log") logEntryProcessed
else appendFile ("log" ++ [pathSeparator] ++ "access.log") logEntryProcessed