summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshersh <>2017-12-07 11:43:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2017-12-07 11:43:00 (GMT)
commit23e53bc8b4bf667ea373370feed7a283fb03ec49 (patch)
tree34a5e9663772fcda1a39d8f2c9dc9e080a038d58
parentdbb47ca70063baf1e03e04af23da9afb64d106f5 (diff)
version 1.7.31.7.3
-rw-r--r--CHANGES.md6
-rw-r--r--examples/HowTo.lhs8
-rw-r--r--examples/HowTo.md8
-rwxr-xr-xexamples/Playground.hs32
-rw-r--r--log-warper.cabal2
-rw-r--r--src/System/Wlog/Launcher.hs127
6 files changed, 139 insertions, 44 deletions
diff --git a/CHANGES.md b/CHANGES.md
index 5e86f09..b287a88 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -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
=====
diff --git a/examples/HowTo.lhs b/examples/HowTo.lhs
index 52dee1c..49917a2 100644
--- a/examples/HowTo.lhs
+++ b/examples/HowTo.lhs
@@ -28,8 +28,7 @@ module Main (main) where
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 @@ So to make it possible we should transform this function to the following:
```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
diff --git a/examples/HowTo.md b/examples/HowTo.md
index 52dee1c..49917a2 100644
--- a/examples/HowTo.md
+++ b/examples/HowTo.md
@@ -28,8 +28,7 @@ module Main (main) where
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 @@ So to make it possible we should transform this function to the following:
```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
diff --git a/examples/Playground.hs b/examples/Playground.hs
index 4e674fa..d31db78 100755
--- a/examples/Playground.hs
+++ b/examples/Playground.hs
@@ -7,9 +7,9 @@ import Universum
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 @@ testLoggerConfigPath = "logger-config-example.yaml"
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 @@ testLogging = usingLoggerName "node" $ do
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
diff --git a/log-warper.cabal b/log-warper.cabal
index 6bdc9df..f4a3e62 100644
--- a/log-warper.cabal
+++ b/log-warper.cabal
@@ -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
diff --git a/src/System/Wlog/Launcher.hs b/src/System/Wlog/Launcher.hs
index 0a26060..e371762 100644
--- a/src/System/Wlog/Launcher.hs
+++ b/src/System/Wlog/Launcher.hs
@@ -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 Universum
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 @@ data HandlerFabric
= 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 @@ buildAndSetupYamlLogging configBuilder loggerConfigPath = do
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)