packages feed

micrologger-0.3.1.0: src/LuminescentDreams/Logger.hs

{- | This is the main API module for the logger. You will generally want to import this directly.

Usage suggestions:


 -}
{-# LANGUAGE RecordWildCards  #-}
module LuminescentDreams.Logger (
    module X
  , LogConfig(..), LogLevel(..), Logger(..)
  , withLogger
  )
  where

import           LuminescentDreams.Logger.Internal
import           LuminescentDreams.Logger.JSON        as X
import           LuminescentDreams.Logger.Standard    as X

import qualified Data.Text.Lazy                       as T
import           System.IO                            (IOMode(..), hPutStrLn, withFile)

{- | Ordinary configurations for a file-based logger. -}
data LogConfig = LogConfig { path :: FilePath
                           , level :: LogLevel
                           }
  deriving (Show)


{- | Run an IO action with a log safely available. Logs will be properly closed in the case of an exception. Logging is meant to happen to a file, specified in LogConfig, so this may not be suitable if you want to log to a different resource. -}
withLogger :: LogConfig -> (Logger -> IO a) -> IO a
withLogger LogConfig{..} act =
  withFile path AppendMode $ \h -> act (Logger (hPutStrLn h . T.unpack) level)