module Control.Eff.Log.Examples
(
exampleLogging
, exampleWithLogging
, exampleWithSomeLogging
, exampleLogPredicate
, exampleLogCapture
, exampleAsyncLogging
, exampleRFC5424Logging
, exampleRFC3164WithRFC5424TimestampsLogging
, exampleDevLogSyslogLogging
, exampleDevLogRFC5424Logging
, exampleUdpRFC5424Logging
, exampleUdpRFC3164Logging
, loggingExampleClient
, logPredicatesExampleClient
)
where
import Control.Eff.Log
import Control.Eff.LogWriter.Async
import Control.Eff.LogWriter.Console
import Control.Eff.LogWriter.DebugTrace
import Control.Eff.LogWriter.Capture
import Control.Eff.LogWriter.IO
import Control.Eff.LogWriter.UnixSocket
import Control.Eff.LogWriter.UDP
import Control.Eff
import Control.Lens ( view
, (%~)
, to
)
import Data.Text as T
import Data.Text.IO as T
import GHC.Stack
exampleLogging :: HasCallStack => IO ()
exampleLogging = runLift
$ withConsoleLogging "my-app" local7 allLogMessages loggingExampleClient
exampleWithLogging :: HasCallStack => IO ()
exampleWithLogging =
runLift $ withLogging consoleLogWriter $ logDebug "Oh, hi there"
exampleWithSomeLogging :: HasCallStack => ()
exampleWithSomeLogging =
run $ withSomeLogging @PureLogWriter $ logDebug "Oh, hi there"
exampleLogPredicate :: HasCallStack => IO Int
exampleLogPredicate =
runLift
$ withSomeLogging @IO
$ setLogWriter consoleLogWriter
$ logPredicatesExampleClient
exampleLogCapture :: IO ()
exampleLogCapture = go >>= T.putStrLn
where
go =
fmap (T.unlines . Prelude.map renderLogMessageConsoleLog . snd)
$ runLift
$ runCaptureLogWriter
$ withLogging captureLogWriter
$ addLogWriter
(mappingLogWriter (lmMessage %~ ("CAPTURED " <>)) captureLogWriter)
$ addLogWriter
(filteringLogWriter
severeMessages
(mappingLogWriter (lmMessage %~ ("TRACED " <>))
(debugTraceLogWriter renderRFC5424)
)
)
$ do
logEmergency "test emergencySeverity 1"
logCritical "test criticalSeverity 2"
logAlert "test alertSeverity 3"
logError "test errorSeverity 4"
logWarning "test warningSeverity 5"
logInfo "test informationalSeverity 6"
logDebug "test debugSeverity 7"
severeMessages = view (lmSeverity . to (<= errorSeverity))
exampleAsyncLogging :: IO ()
exampleAsyncLogging =
runLift $ withLogging consoleLogWriter $ withAsyncLogWriter (1000 :: Int) $ do
logInfo "test 1"
logInfo "test 2"
logInfo "test 3"
exampleRFC5424Logging :: IO Int
exampleRFC5424Logging =
runLift
$ withSomeLogging @IO
$ setLogWriter
(defaultIoLogWriter "myapp" local2 (debugTraceLogWriter renderRFC5424))
$ logPredicatesExampleClient
exampleRFC3164WithRFC5424TimestampsLogging :: IO Int
exampleRFC3164WithRFC5424TimestampsLogging =
runLift
$ withSomeLogging @IO
$ setLogWriter
(defaultIoLogWriter "myapp" local2 (debugTraceLogWriter renderRFC3164WithRFC5424Timestamps))
$ logPredicatesExampleClient
exampleDevLogSyslogLogging :: IO Int
exampleDevLogSyslogLogging =
runLift
$ withUnixSocketLogging renderLogMessageSyslog "/dev/log" "myapp" local2 allLogMessages
logPredicatesExampleClient
exampleDevLogRFC5424Logging :: IO Int
exampleDevLogRFC5424Logging =
runLift
$ withUnixSocketLogging renderRFC5424 "/dev/log" "myapp" local2 allLogMessages
logPredicatesExampleClient
exampleUdpRFC5424Logging :: IO Int
exampleUdpRFC5424Logging =
runLift
$ withUDPLogging renderRFC5424 "localhost" "514" "myapp" local2 allLogMessages
logPredicatesExampleClient
exampleUdpRFC3164Logging :: IO Int
exampleUdpRFC3164Logging =
runLift
$ withUDPLogging renderRFC3164 "localhost" "514" "myapp" local1 allLogMessages
logPredicatesExampleClient
loggingExampleClient :: (HasCallStack, Monad h, LogsTo h e) => Eff e ()
loggingExampleClient = do
logDebug "test 1.1"
logError "test 1.2"
censorLogs (prefixLogMessagesWith "NESTED: ") $ do
addLogWriter (debugTraceLogWriter renderRFC3164)
$ setLogPredicate (\m -> view lmMessage m /= "not logged")
$ do
logInfo "not logged"
logMsg "test 2.1"
logWarning "test 2.2"
logCritical "test 1.3"
logPredicatesExampleClient :: (HasCallStack, Monad h, LogsTo h e) => Eff e Int
logPredicatesExampleClient = do
logInfo "test"
setLogPredicate
(lmMessageStartsWith "OMG")
(do
logInfo "this message will not be logged"
logInfo "OMG logged"
modifyLogPredicate (\p lm -> p lm || lmSeverityIs errorSeverity lm) $ do
logDebug "OMG logged"
logInfo "Not logged"
logError "Logged"
logEmergency "Not Logged"
includeLogMessages (lmSeverityIsAtLeast warningSeverity) $ do
logInfo "Not logged"
logError "Logged"
logEmergency "Logged"
logWarning "Logged"
logDebug "OMG still Logged"
excludeLogMessages (lmMessageStartsWith "OMG") $ do
logDebug "OMG NOT Logged"
logError "OMG ALSO NOT Logged"
logEmergency "Still Logged"
logWarning "Still Logged"
logWarning "Logged"
logDebug "OMG still Logged"
return 42
)