log-warper 1.7.2 → 1.7.3
raw patch · 6 files changed
+139/−44 lines, 6 files
Files
- CHANGES.md +6/−0
- examples/HowTo.lhs +3/−5
- examples/HowTo.md +3/−5
- examples/Playground.hs +17/−15
- log-warper.cabal +1/−1
- src/System/Wlog/Launcher.hs +109/−18
CHANGES.md view
@@ -1,3 +1,9 @@+1.7.3+=====++* [#61](https://github.com/serokell/log-warper/issues/61):+ Add `launchFromFile`, `defaultConfig` and `launchSimpleLogging` functions.+ 1.7.2 =====
examples/HowTo.lhs view
@@ -28,8 +28,7 @@ import Control.Monad.IO.Class (MonadIO, liftIO) import Data.Semigroup ((<>)) import Data.Text (Text)-import System.Wlog (WithLoggerIO, buildAndSetupYamlLogging, logError,- logInfo, productionB, usingLoggerName)+import System.Wlog (WithLoggerIO, launchFromFile, logError, logInfo) import qualified Data.Text as Text import qualified Data.Text.IO as TextIO@@ -90,9 +89,8 @@ ```haskell mainWithLogging :: IO ()-mainWithLogging = do- buildAndSetupYamlLogging productionB "examples/how-to-log-config.yaml"- usingLoggerName "new-logger" inputLengthWithLog+mainWithLogging =+ launchFromFile "examples/how-to-log-config.yaml" "new-logger" inputLengthWithLog ``` where we set up the config from the file and run `inputLengthWithLog` under the logger with name `new-logger`. Note that we use `inputLengthWithLog` now instead of `inputLength` because we can not just add logging
examples/HowTo.md view
@@ -28,8 +28,7 @@ import Control.Monad.IO.Class (MonadIO, liftIO) import Data.Semigroup ((<>)) import Data.Text (Text)-import System.Wlog (WithLoggerIO, buildAndSetupYamlLogging, logError,- logInfo, productionB, usingLoggerName)+import System.Wlog (WithLoggerIO, launchFromFile, logError, logInfo) import qualified Data.Text as Text import qualified Data.Text.IO as TextIO@@ -90,9 +89,8 @@ ```haskell mainWithLogging :: IO ()-mainWithLogging = do- buildAndSetupYamlLogging productionB "examples/how-to-log-config.yaml"- usingLoggerName "new-logger" inputLengthWithLog+mainWithLogging =+ launchFromFile "examples/how-to-log-config.yaml" "new-logger" inputLengthWithLog ``` where we set up the config from the file and run `inputLengthWithLog` under the logger with name `new-logger`. Note that we use `inputLengthWithLog` now instead of `inputLength` because we can not just add logging
examples/Playground.hs view
@@ -7,9 +7,9 @@ import Data.Monoid ((<>)) import Data.Yaml.Pretty (defConfig, encodePretty) -import System.Wlog (CanLog, buildAndSetupYamlLogging, dispatchEvents, logDebug, logError, logInfo,- logNotice, logWarning, modifyLoggerName, parseLoggerConfig, prefixB,- productionB, removeAllHandlers, runPureLog, usingLoggerName)+import System.Wlog (CanLog, defaultConfig, launchFromFile, launchSimpleLogging, logDebug, logError,+ logInfo, logNotice, logWarning, modifyLoggerName, parseLoggerConfig,+ productionB, usingLoggerName) testLoggerConfigPath :: FilePath testLoggerConfigPath = "logger-config-example.yaml"@@ -17,7 +17,7 @@ testToJsonConfigOutput :: MonadIO m => m () testToJsonConfigOutput = do cfg <- parseLoggerConfig testLoggerConfigPath- let builtConfig = cfg <> productionB <> prefixB "logs"+ let builtConfig = cfg <> productionB putStrLn $ encodePretty defConfig builtConfig testLogging :: (CanLog m) => m ()@@ -33,19 +33,21 @@ logError "BARDAQ" -showPureLog :: IO ()-showPureLog = do- (res, pureLog) <- runPureLog testLogging- putText "Pure log:"+showSomeLog :: (CanLog m, MonadIO m) => m ()+showSomeLog = do+ putText "Other log:" usingLoggerName "naked" $ do- logWarning $ "Pure log for result = " <> show res <> ":"- dispatchEvents pureLog+ logWarning "Some warning"+ logDebug "Some debug" main :: IO () main = do testToJsonConfigOutput- let config = (productionB <> prefixB "logs")- bracket_- (buildAndSetupYamlLogging config testLoggerConfigPath)- removeAllHandlers- (testLogging >> showPureLog)+ let runPlayLog = testLogging >> showSomeLog++ putStrLn $ encodePretty defConfig $ defaultConfig "example"+ putText "Default configurations.."+ launchSimpleLogging "node" runPlayLog++ putText "\nFrom file configurations.."+ launchFromFile testLoggerConfigPath "node" runPlayLog
log-warper.cabal view
@@ -1,5 +1,5 @@ name: log-warper-version: 1.7.2+version: 1.7.3 synopsis: Flexible, configurable, monadic and pretty logging homepage: https://github.com/serokell/log-warper license: MIT
src/System/Wlog/Launcher.hs view
@@ -12,21 +12,25 @@ -- Parser for configuring and initializing logger from YAML file. -- Logger configuration should look like this: ----- > rotation: # [optional] parameters for logging rotation--- > logLimit: 1024 # max size of log file in bytes--- > keepFiles: 3 # number of files with logs to keep including current one--- > node: # logger named «node»--- > severity: Warning # severity for logger «node»--- > comm: # logger named «node.comm»--- > severity: Info # severity for logger «node.comm»--- > file: patak.jpg # messages will be also printed to patak.jpg+-- > rotation: # [optional] parameters for logging rotation+-- > logLimit: 1024 # max size of log file in bytes+-- > keepFiles: 3 # number of files with logs to keep including current one+-- > loggerTree:+-- > severity: Warning+ # severities for «root» logger+-- > node: # logger named «node»+-- > severity: Warning+ # severities for logger «node»+-- > comm: # logger named «node.comm»+-- > severity: Info+ # severity for logger «node.comm»+-- > file: patak.jpg # messages will be also printed to patak.jpg -- -- And this configuration corresponds two loggers with 'LoggerName'`s -- @node@ and @node.comm@. module System.Wlog.Launcher ( buildAndSetupYamlLogging- , initLoggingFromYaml+ , defaultConfig+ , launchFromFile+ , launchSimpleLogging , parseLoggerConfig , setupLogging ) where@@ -35,19 +39,24 @@ import Control.Error.Util ((?:)) import Control.Exception (throwIO)+import Control.Lens (zoom, (.=), (?=)) import Data.Time (UTCTime) import Data.Yaml (decodeFileEither) import System.Directory (createDirectoryIfMissing) import System.FilePath ((</>)) import System.Wlog.Formatter (centiUtcTimeF, stdoutFormatter, stdoutFormatterTimeRounded)-import System.Wlog.IOLogger (addHandler, setPrefix, setSeveritiesMaybe, updateGlobalLogger)-import System.Wlog.LoggerConfig (HandlerWrap (..), LoggerConfig (..), LoggerTree (..))+import System.Wlog.IOLogger (addHandler, removeAllHandlers, setPrefix, setSeveritiesMaybe,+ updateGlobalLogger)+import System.Wlog.LoggerConfig (HandlerWrap (..), LoggerConfig (..), LoggerTree (..), fromScratch,+ lcConsoleAction, lcShowTime, lcTree, ltSeverity, productionB,+ zoomLogger) import System.Wlog.LoggerName (LoggerName (..))+import System.Wlog.LoggerNameBox (LoggerNameBox, usingLoggerName) import System.Wlog.LogHandler (LogHandler (setFormatter)) import System.Wlog.LogHandler.Roller (rotationFileHandler)-import System.Wlog.LogHandler.Simple (fileHandler)-import System.Wlog.Severity (Severities, debugPlus)+import System.Wlog.LogHandler.Simple (defaultHandleAction, fileHandler)+import System.Wlog.Severity (Severities, debugPlus, warningPlus) import System.Wlog.Terminal (initTerminalLogging) import qualified Data.HashMap.Strict as HM hiding (HashMap)@@ -56,7 +65,7 @@ = forall h . LogHandler h => HandlerFabric (FilePath -> Severities -> IO h) -- | This function traverses 'LoggerConfig' initializing all subloggers--- with 'Severity' and redirecting output in file handlers.+-- with 'Severities' and redirecting output in file handlers. -- See 'LoggerConfig' for more details. setupLogging :: MonadIO m => Maybe (UTCTime -> Text) -> LoggerConfig -> m () setupLogging mTimeFunction LoggerConfig{..} = do@@ -120,7 +129,89 @@ let builtConfig = cfg <> configBuilder setupLogging Nothing builtConfig --- | Initialize logger hierarchy from configuration file.--- See this module description.-initLoggingFromYaml :: MonadIO m => FilePath -> m ()-initLoggingFromYaml = buildAndSetupYamlLogging mempty+-- | Initializes logging using given 'FilePath' to logger configurations,+-- runs the action with the given 'LoggerName'.+launchFromFile :: (MonadIO m, MonadMask m)+ => FilePath+ -> LoggerName+ -> LoggerNameBox m a+ -> m a+launchFromFile filename loggerName action =+ bracket_+ (buildAndSetupYamlLogging productionB filename)+ removeAllHandlers+ (usingLoggerName loggerName action)++{- | Default logging configuration with the given 'LoggerName'.++Enabled flags:++ - 'ltSeverity' of the root logger is set to 'warningPlus'+ ('System.Wlog.Severity.Warning' and upper)+ - 'ltSeverity' for the given logger is set to 'debugPlus' ('System.Wlog.Severity.Debug' and upper)+ - 'lcShowTime' is set to 'Any True' which means that time is shown in the log messages.+ - 'lcConsoleAction' is set to 'defaultHandleAction' which turns the console output on.++==== __/Example/__+@ defaultConfig "example"@ will produce such configurations:++@+rotation: null+showTid: false+showTime: true+printOutput: true+logTree:+ _ltSubloggers:+ example:+ _ltSubloggers: {}+ _ltSeverity:+ - Debug+ - Info+ - Notice+ - Warning+ - Error+ _ltFiles: []+ _ltSeverity:+ - Warning+ - Error+ _ltFiles: []+termSeveritiesOut: null+filePrefix: null+termSeveritiesErr: null+@++-}+defaultConfig :: LoggerName -> LoggerConfig+defaultConfig loggerName = fromScratch $ do+ lcShowTime .= Any True+ lcConsoleAction .= Last (Just defaultHandleAction)+ zoom lcTree $ do+ ltSeverity ?= warningPlus+ zoomLogger (getLoggerName loggerName) $ do+ ltSeverity ?= debugPlus++{- | Set ups the logging with 'defaultConfig' and runs the action with the given 'LoggerName'.++==== __/Example/__+Here we can see very simple working example of logging:++>>> :{+>>> launchSimpleLogging "app" $ do+>>> logDebug "Debug message"+>>> putStrLn "Usual printing"+>>> logInfo "Job's done!"+>>> :}+[app:DEBUG] [2017-12-07 11:25:06.47 UTC] Debug message+Usual printing+[app:INFO] [2017-12-07 11:25:06.47 UTC] Job's done!++-}+launchSimpleLogging :: (MonadIO m, MonadMask m)+ => LoggerName+ -> LoggerNameBox m a+ -> m a+launchSimpleLogging loggerName action =+ bracket_+ (setupLogging Nothing $ defaultConfig loggerName)+ removeAllHandlers+ (usingLoggerName loggerName action)