@hackage log4hs0.0.1.0

A python logging style log library

A python logging style log library.

A full example:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}
{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE TemplateHaskell   #-}


module Main (main) where

import           Data.Aeson.QQ.Simple (aesonQQ)
import           Prelude hiding (error)
import           Logging (runJson, debug, info, warn, error, fatal, logv)

main :: IO ()
main = runJson manager app

myLogger = "MyLogger.Main"

app :: IO ()
app = do
  $(debug) myLogger "this message should print into MyLogger"
  $(info) myLogger "this message should print into MyLogger"
  $(warn) myLogger "this message should print into MyLogger"
  $(error) myLogger "this message should print into MyLogger"
  $(fatal) myLogger "this message should print into MyLogger"
  $(logv) myLogger "LEVEL 100" "this message should print into MyLogger"

-- The best practice is putting all config into a separate file,
-- e.g "Logging.json"
manager = [aesonQQ|{
  "loggers": {
    "root": {
      "level": "DEBUG",
      "handlers": ["console"],
      "propagate": false
    },
    "MyLogger": {
      "level": "INFO",
      "filterer": ["MyLogger.Main"],
      "handlers": ["file"],
      "propagate": false
    }
  },
  "handlers": {
    "console": {
      "type": "StreamHandler",
      "stream": "stderr",
      "level": "DEBUG",
      "formatter": "defaultFormatter"
    },
    "file": {
      "type": "FileHandler",
      "level": "INFO",
      "formatter": "defaultFormatter",
      "file": "./default.log"
    }
  },
  "formatters": {
    "defaultFormatter": {
      "fmt": "%(asctime)s - %(level)s - %(logger)s - %(pathname)s/%(filename)s:%(lineno)d] %(message)s"
    }
  }
}|]