module Control.Eff.Log.Examples
(
exampleLogging
, exampleWithLogging
, exampleWithSomeLogging
, exampleSetLogWriter
, exampleLogTrace
, 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.Rich
import Control.Eff.LogWriter.UnixSocket
import Control.Eff.LogWriter.UDP
import Control.Eff
import Control.Lens ( view
, (%~)
, to
)
import GHC.Stack
exampleLogging :: HasCallStack => IO ()
exampleLogging = runLift
$ withConsoleLogging "my-app" local7 allLogMessages loggingExampleClient
exampleWithLogging :: HasCallStack => IO ()
exampleWithLogging = do
lw <- consoleLogWriter
runLift $ withLogging lw $ logDebug "Oh, hi there"
exampleWithSomeLogging :: HasCallStack => ()
exampleWithSomeLogging =
run $ withoutLogging $ logDebug "Oh, hi there"
exampleSetLogWriter :: HasCallStack => IO ()
exampleSetLogWriter = do
lw1 <- stdoutLogWriter renderConsoleMinimalisticWide
lw2 <- consoleLogWriter
runLift
$ withLogging lw1
$ do logAlert "test with log writer 1"
setLogWriter lw2 (logAlert "test with log writer 2")
logAlert "test with log writer 1 again"
exampleLogTrace :: IO ()
exampleLogTrace = do
lw <- consoleLogWriter
runLift
$ withRichLogging lw "test-app" local7 allLogMessages
$ 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"
where
severeMessages = view (lmSeverity . to (<= errorSeverity))
exampleAsyncLogging :: IO ()
exampleAsyncLogging = do
lw <- stdoutLogWriter renderConsoleMinimalisticWide
runLift $ withLogging lw $ withAsyncLogWriter (1000 :: Int) $ do
logInfo "test 1"
logInfo "test 2"
logInfo "test 3"
exampleRFC5424Logging :: IO Int
exampleRFC5424Logging =
runLift
$ withoutLogging
$ setLogWriter
(richLogWriter "myapp" local2 (debugTraceLogWriter renderRFC5424))
logPredicatesExampleClient
exampleRFC3164WithRFC5424TimestampsLogging :: IO Int
exampleRFC3164WithRFC5424TimestampsLogging =
runLift
$ withoutLogging
$ setLogWriter
(richLogWriter "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, IoLogging 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, IoLogging 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
)