MyNixOS website logo
Description

A simple and fast logging monad.

This package is a fork of great `monad-log`, as the original author is unreachable.

It provides a mtl style MonadLog class and a concrete monad transformer LogT, the main difference between this package and monad-logger are:

  • Base monad has to be an instance of MonadIO.

  • Parametrized logging environment for extensibility.

  • Basic logging environment type(Label,Loc,NameSpace,ThreadId) are included, and you can easily make your own.

  • JSON logging built-in.

  • default to fast-logger backend, with good stdout and file support.

If you are an application author, you can use LogT transformer, it's just a specialized reader monad to inject `Logger env`.

If you are a library author, you should do following two things:

  • make your monad stack an instance of MonadLog, usually you can do this by embedding `Logger env` into your monad's reader part.

  • provide a default formatter, and API to run with customized formatter.

A fast & simple logging monad

Hackage Travis-CI

This package is a fork of great monad-log, as the original author is unreachable.

It provides a mtl style MonadLog class and a concrete monad transformer LogT, the main difference between this package and monad-logger are:

  • Base monad has to be an instance of MonadIO.

  • Parametrized logging environment for extensibility.

  • Basic logging environment type(Label,Loc,NameSpace,ThreadId) are included, and you can easily make your own.

  • JSON logging built-in.

  • default to fast-logger backend, with good stdout and file support.

If you are an application author, you can use LogT transformer, a specialized reader monad to inject Logger env.

If you are a library author, you should:

  • make your monad stack an instance of 'MonadLog', usually you can do this by embedding Logger env into your monad's reader part.

  • provide a default formatter, and API to run with customized formatter.

Example

  • A simple labelled logger:
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Control.Monad.Log
import Control.Monad.Log.Label

-- Following log will be output to stdout:
-- [INFO] [25-Apr-2016 12:51:56] [main] This is simple log 1
-- [INFO] [25-Apr-2016 12:51:56] [foo] This is simple log 2

main :: IO ()
main = do
    logger <- makeDefaultLogger
        simpleTimeFormat'
        (LogStdout 4096)
        levelDebug
        (Label "main")

    runLogTSafe logger $ do
        info "This is simple log 1"

        withLabel (Label "foo") $ do
            info "This is simple log 2"

  • Logging with ThreadId:
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Control.Monad.Log
import Control.Monad.IO.Class
import Control.Monad
import Control.Concurrent
import Control.Monad.Log.LogThreadId

-- Following log will be output to stdout:
-- [INFO] [25-Apr-2016 15:06:10] [ThreadId 671] This is simple log 1
-- [INFO] [25-Apr-2016 15:06:10] [ThreadId 674] This is simple log 2
-- [INFO] [25-Apr-2016 15:06:10] [ThreadId 675] This is simple log 2
-- [INFO] [25-Apr-2016 15:06:10] [ThreadId 676] This is simple log 2
-- [INFO] [25-Apr-2016 15:06:10] [ThreadId 677] This is simple log 2
-- [INFO] [25-Apr-2016 15:06:10] [ThreadId 678] This is simple log 2
-- [INFO] [25-Apr-2016 15:06:10] [ThreadId 679] This is simple log 2
...

main :: IO ()
main = do
    tid <- myLogThreadId
    logger <- makeDefaultLogger
        simpleTimeFormat'
        (LogStdout 4096)
        levelDebug
        tid

    runLogTSafe logger $ do
        info "This is simple log 1"

    replicateM_ 100 $
        forkIO . runLogT' logger . withMyLogThreadId $ do
            info "This is simple log 2"
  • Customized logging environment:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleContexts #-}

module Main where

import Control.Monad.Log
import Control.Monad.Log.LogLoc
import Control.Monad.Log.NameSpace
import Data.Aeson.TH
import Data.Text (Text)

-- Following JSON log will be output to stdout:
-- {"level":"INFO","time":"25-Apr-2016 13:54:32"
-- ,"env":{"loc":{"filename":"Test.hs","module":"Test","package":"monad_GM54RwU2jZ84vGJIhnMYMH","line":33},"ns":["root"]}
-- ,"msg":"This is simple log 1"}
-- {"level":"INFO","time":"25-Apr-2016 13:54:32"
-- ,"env":{"loc":{"filename":"Test.hs","module":"Test","package":"monad_GM54RwU2jZ84vGJIhnMYMH","line":33},"ns":["foo","root"]}
-- ,"msg":"This is simple log 2"}

-- | Define your logging environment type.
-- To use 'defaultFomatter', provide a 'TextShow' instance
-- To use 'defaultJSONFomatter', provide a 'ToJSON' instance

data MyEnv = MyEnv {
        loc :: LogLoc  -- This is shared by every log within one 'MonadLog'.
    ,   ns  :: NameSpace
    } deriving (Show, Eq)

$(deriveJSON defaultOptions ''MyEnv)

subMyNS :: (MonadLog MyEnv m) => Text -> m a -> m a
subMyNS sub = localEnv $ \env -> env { ns = pushNameSpace sub (ns env) }

main :: IO ()
main = do
    logger <- makeDefaultJSONLogger
        simpleTimeFormat'
        (LogStdout 4096)
        levelDebug
        (MyEnv $myLogLoc (NameSpace ["root"]))

    runLogTSafe logger $ do
        info "This is simple log 1"

        subMyNS "foo" $ do
            info "This is simple log 2"

Metadata

Version

0.1.1.1

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows