micrologger 0.3.1.1 → 0.4.0.0
raw patch · 3 files changed
+32/−12 lines, 3 filesdep ~aesondep ~timedep ~transformers
Dependency ranges changed: aeson, time, transformers
Files
- micrologger.cabal +4/−4
- src/LuminescentDreams/Logger.hs +13/−8
- test/LogSpec.hs +15/−0
micrologger.cabal view
@@ -1,5 +1,5 @@ name: micrologger-version: 0.3.1.1+version: 0.4.0.0 synopsis: A super simple logging module. Only for use for very simple projects. description: A super simple logging module. Only for use for very simple projects. homepage: https://github.com/savannidgerinel/micrologger#readme@@ -24,12 +24,12 @@ LuminescentDreams.Logger.Standard build-depends: base >= 4.7 && < 5- , aeson >= 0.9 && < 0.12+ , aeson >= 1.0 && < 1.1 , containers >= 0.5.6 && < 0.6 , text >= 1.2 && < 1.3 , text-format >= 0.3 && < 0.4- , time >= 1.5 && < 1.6- , transformers >= 0.4 && < 0.5+ , time >= 1.5 && < 1.7+ , transformers >= 0.4 && < 0.6 test-suite micrologger-test type: exitcode-stdio-1.0
src/LuminescentDreams/Logger.hs view
@@ -4,7 +4,6 @@ -}-{-# LANGUAGE RecordWildCards #-} module LuminescentDreams.Logger ( module X , LogConfig(..), LogLevel(..), Logger(..)@@ -16,17 +15,23 @@ import LuminescentDreams.Logger.JSON as X import LuminescentDreams.Logger.Standard as X +import Data.Monoid+import Data.IORef (IORef, modifyIORef) import qualified Data.Text.Lazy as T-import System.IO (IOMode(..), hPutStrLn, hFlush, withFile)+import System.IO (IOMode(..), hPutStrLn, hFlush, withFile, stdout) {- | Ordinary configurations for a file-based logger. -}-data LogConfig = LogConfig { path :: FilePath- , level :: LogLevel- }- deriving (Show)+data LogConfig = Stdout LogLevel+ | Buffer (IORef T.Text) LogLevel+ | FileLog FilePath LogLevel {- | 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 (\m -> hPutStrLn h (T.unpack m) >> hFlush h) level)+withLogger (Stdout level) act =+ act (Logger (\m -> putStrLn (T.unpack m) >> hFlush stdout) level)+withLogger (Buffer ref level) act =+ act (Logger (\m -> modifyIORef ref (\buf -> buf <> m <> T.pack "\n")) level)+withLogger (FileLog path level) act =+ withFile path AppendMode $ \h ->+ act (Logger (\m -> hPutStrLn h (T.unpack m) >> hFlush h) level)
test/LogSpec.hs view
@@ -33,7 +33,22 @@ IORef.readIORef logRef >>= flip forM_ (putStrLn . T.unpack) +loggerTypes :: Spec+loggerTypes = describe "verify that the built-in logger types work" $ do+ it "outputs to stdout" $ pendingWith "how does one even verify this?"++ it "outputs to a file buffer" $ do+ logRef <- IORef.newIORef T.empty+ withLogger (Buffer logRef LogInfo) $ \l -> do+ logMsgStd l LogInfo [("name", "value")] "message"+ logMsgStd l LogDebug [("name", "value")] "message2"+ logMsgJs l LogDebug [("name", String "value"), ("timing", toJSON (0.5 :: Float)), ("msg", String "message")]+ logMsgJs l LogInfo [("name", String "value"), ("timing", toJSON (0.5 :: Float)), ("msg", String "message2")]+ IORef.readIORef logRef >>= putStrLn . T.unpack++ spec :: Spec spec = do basicLogger jsonLogger+ loggerTypes