packages feed

monad-logger-extras 0.1.0.0 → 0.1.1.0

raw patch · 5 files changed

+89/−14 lines, 5 filesdep +ansi-terminalPVP ok

version bump matches the API change (PVP)

Dependencies added: ansi-terminal

API changes (from Hackage documentation)

+ Control.Monad.Logger.Extras: colorize :: Logger -> Logger
+ Control.Monad.Logger.Extras: colorizeWith :: [(LogLevel, Color)] -> Logger -> Logger
+ Control.Monad.Logger.Extras: defaultColors :: [(LogLevel, Color)]
+ Control.Monad.Logger.Extras: mapLogStrBS :: ToLogStr msg => (ByteString -> msg) -> LogStr -> LogStr
+ Control.Monad.Logger.Extras: test :: IO ()
+ Control.Monad.Logger.Extras: wrapSGRCode :: [SGR] -> ByteString -> ByteString
+ Control.Monad.Logger.Extras: wrapSGRColor :: Color -> ByteString -> ByteString

Files

CHANGELOG.md view
@@ -1,5 +1,9 @@ # Revision history for monad-logger-extras +## 0.1.1.0++* Add `colorize` functions to apply colors to log messages.+ ## 0.1.0.0  * First version.
README.lhs view
@@ -9,8 +9,10 @@  This package provides a way to compose logging actions so that you can conveniently log to multiple destinations. It also includes implementations of a few common logging actions: logging to stdout, stderr, nowhere (similar to `NoLoggingT`), and to a posix syslog (using [hsyslog](https://hackage.haskell.org/package/hsyslog)). -It also contains a couple of orphan instances for [`LoggingT`](https://hackage.haskell.org/package/monad-logger-0.3.36/docs/Control-Monad-Logger.html#t:LoggingT): `MonadPlus` and `Alternative`.+Logs can be emitted in color by using the `colorize` or `colorizeWith` function on your `Logger`. +This package also contains a couple of orphan instances for [`LoggingT`](https://hackage.haskell.org/package/monad-logger-0.3.36/docs/Control-Monad-Logger.html#t:LoggingT): `MonadPlus` and `Alternative`.+ Example usage ------------- @@ -22,7 +24,6 @@  This example can be built and run using cabal (either `cabal repl example` or `cabal build example`). - ```haskell  > {-# LANGUAGE OverloadedStrings #-}@@ -32,8 +33,15 @@ >  > main :: IO () > main = do->   let logger = logToStdout <> logToStderr <> logToSyslog "log-test"+>   let logger = colorize logToStdout <> logToStderr <> logToSyslog "log-test" >   flip runLoggerLoggingT logger $ do >     logInfoN "This is a test. You should see this on stdout, stderr, and in your system log."+>     logDebugN "This is a debug message."+>     logWarnN "This is a warning."+>     logErrorN "This is an error!"  ```++This should produce output that looks like this (note that the stdout log has been `colorize`d):++![Example output](https://i.imgur.com/nkVAHrM.png)
README.md view
@@ -9,8 +9,10 @@  This package provides a way to compose logging actions so that you can conveniently log to multiple destinations. It also includes implementations of a few common logging actions: logging to stdout, stderr, nowhere (similar to `NoLoggingT`), and to a posix syslog (using [hsyslog](https://hackage.haskell.org/package/hsyslog)). -It also contains a couple of orphan instances for [`LoggingT`](https://hackage.haskell.org/package/monad-logger-0.3.36/docs/Control-Monad-Logger.html#t:LoggingT): `MonadPlus` and `Alternative`.+Logs can be emitted in color by using the `colorize` or `colorizeWith` function on your `Logger`. +This package also contains a couple of orphan instances for [`LoggingT`](https://hackage.haskell.org/package/monad-logger-0.3.36/docs/Control-Monad-Logger.html#t:LoggingT): `MonadPlus` and `Alternative`.+ Example usage ------------- @@ -22,7 +24,6 @@  This example can be built and run using cabal (either `cabal repl example` or `cabal build example`). - ```haskell  > {-# LANGUAGE OverloadedStrings #-}@@ -32,8 +33,15 @@ >  > main :: IO () > main = do->   let logger = logToStdout <> logToStderr <> logToSyslog "log-test"+>   let logger = colorize logToStdout <> logToStderr <> logToSyslog "log-test" >   flip runLoggerLoggingT logger $ do >     logInfoN "This is a test. You should see this on stdout, stderr, and in your system log."+>     logDebugN "This is a debug message."+>     logWarnN "This is a warning."+>     logErrorN "This is an error!"  ```++This should produce output that looks like this (note that the stdout log has been `colorize`d):++![Example output](https://i.imgur.com/nkVAHrM.png)
monad-logger-extras.cabal view
@@ -1,11 +1,11 @@ cabal-version:      >=1.10 name:               monad-logger-extras-version:            0.1.0.0+version:            0.1.1.0 synopsis:-  Utilities for composing loggers, plus a few orphan instances.+  Utilities for composing loggers, coloring output, plus a few orphan instances.  description:-  Build composable logging backends for monad-logger. This package includes a few composable backends (including a posix syslog backend).+  Build composable logging backends for monad-logger. This package includes a few composable backends (including a posix syslog backend) and some extras like log output coloring utilities.  homepage:           https://github.com/obsidiansystems/monad-logger-extras bug-reports:@@ -30,11 +30,12 @@     Control.Monad.Logger.Orphans    build-depends:-      base          >=4.12   && <5-    , bytestring    >=0.10   && <0.12-    , hsyslog       >=5      && <5.1-    , monad-logger  >=0.3.36 && <0.4-    , mtl           >=2.2    && <2.3+      ansi-terminal  >=0.9    && <0.12+    , base           >=4.12   && <5+    , bytestring     >=0.10   && <0.12+    , hsyslog        >=5      && <5.1+    , monad-logger   >=0.3.36 && <0.4+    , mtl            >=2.2    && <2.3    hs-source-dirs:   src   default-language: Haskell2010
src/Control/Monad/Logger/Extras.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-| Description: Composable logging actions for monad-logger, with a few predefined loggers.@@ -6,10 +7,13 @@  import Control.Monad.Logger +import Data.ByteString.Char8 as C8 import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import System.IO import qualified System.Posix.Syslog as Posix +import System.Console.ANSI+ -- | Run a 'LoggingT' action using the provided 'Logger' runLoggerLoggingT :: LoggingT m a -> Logger -> m a runLoggerLoggingT f logger = f `runLoggingT` unLogger logger@@ -50,3 +54,53 @@   Posix.withSyslog tagstr [Posix.DelayedOpen] Posix.User $     unsafeUseAsCStringLen (fromLogStr out) $       Posix.syslog Nothing syslogPriority++-- | Add colors to your log output based on 'LogLevel'. Colors can be+-- customized by using 'colorizeWith' instead.+colorize :: Logger -> Logger+colorize = colorizeWith defaultColors++-- | Add a custom set of colors to your log output. See 'defaultColors' for an+-- example.+colorizeWith :: [(LogLevel, Color)] -> Logger -> Logger+colorizeWith colorMap f = Logger $ \loc src lvl str ->+  let c s = case lookup lvl colorMap of+        Nothing -> str+        Just color -> mapLogStrBS (wrapSGRColor color) s+  in unLogger f loc src lvl $ c str++-- | The default color mapping used by 'colorize'.+defaultColors :: [(LogLevel, Color)]+defaultColors =+  [ (LevelDebug, Green)+  , (LevelInfo, Blue)+  , (LevelWarn, Yellow)+  , (LevelError, Red)+  ]++-- | Map a function over a log string.+mapLogStrBS :: ToLogStr msg => (ByteString -> msg) -> LogStr -> LogStr+mapLogStrBS f = toLogStr . f . fromLogStr++-- | Apply 'SGR' codes to a string to modify its display attributes, resetting+-- SGR codes afterward.+wrapSGRCode :: [SGR] -> ByteString -> ByteString+wrapSGRCode codes t = mconcat+  [ C8.pack $ setSGRCode codes+  , t+  , C8.pack $ setSGRCode [Reset]+  ]++-- | Apply an SGR color code to a string, unsetting the color after the string.+wrapSGRColor :: Color -> ByteString -> ByteString+wrapSGRColor c = wrapSGRCode [SetColor Foreground Vivid c]++-- | A handy test+test :: IO ()+test = do+  let logger = colorize logToStderr <> logToSyslog "log-test"+  flip runLoggerLoggingT logger $ do+    logDebugN "This is a debug message."+    logInfoN "This is an info message."+    logWarnN "This is a warning."+    logErrorN "This is an error!"