Safe Haskell | None |
---|---|
Language | Haskell2010 |
Use this module to add logging to your monad. A log is a structured value that can hold information like severity, log message, timestamp, callstack, etc.
Logging is treated like a stream of logs comming from your application and functions that
transform the logs take a stream and output a stream. Functions like logInfo
push a new log
into the stream and functions like setTimestampToNow
take a stream of logs and attach extra
info onto each log (current time in this case).
Read the documentation of individual functions to get a feel for what you can do.
- data Logging
- logEffect :: MonadEffect Logging m => Log -> m ()
- newtype Tag = Tag Text
- newtype Context = Context {
- getContext :: Text
- data Level
- data LogUser = LogUser {}
- addIfExists :: Text -> Maybe Value -> [(Text, Value)] -> [(Text, Value)]
- data Crumb = Crumb {}
- data CrumbData
- = DefaultCrumb [(Text, Value)]
- | HttpCrumb {
- crumbUrl :: Text
- crumbMethod :: Text
- crumbStatusCode :: Int
- crumbReason :: Text
- data LogData = LogData {}
- data Log = Log {
- logMessage :: Text
- logLevel :: Level
- logTags :: [Tag]
- logContext :: [Context]
- logUser :: Maybe LogUser
- logCrumbs :: [Crumb]
- logData :: LogData
- logTimestamp :: Maybe UTCTime
- logCallStack :: CallStack
- newtype GenericException = GenericException Text
- handleLogging :: Functor m => (Log -> m ()) -> RuntimeImplemented Logging m a -> m a
- layerLogs :: (HasCallStack, MonadEffect Logging m) => Context -> RuntimeImplemented Logging m a -> m a
- originContext :: Log -> Maybe Context
- logWithLevel :: (HasCallStack, MonadEffect Logging m) => Level -> Text -> m ()
- logInfo :: (HasCallStack, MonadEffect Logging m) => Text -> m ()
- logWarning :: (HasCallStack, MonadEffect Logging m) => Text -> m ()
- logError :: (HasCallStack, MonadEffect Logging m) => Text -> m ()
- logDebug :: (HasCallStack, MonadEffect Logging m) => Text -> m ()
- logFatal :: (HasCallStack, MonadEffect Logging m) => Text -> m ()
- logAndError :: (Exception e, MonadEffect Logging m, MonadThrow m, HasCallStack) => Text -> e -> m a
- logAndThrowsErr :: (MonadEffects '[Logging, Signal e Void] m, HasCallStack) => Text -> e -> m a
- logAndThrowGeneric :: (MonadEffect Logging m, MonadThrow m, HasCallStack) => Text -> m a
- logMessagesToStdout :: MonadIO m => RuntimeImplemented Logging m a -> m a
- logRawToStdout :: MonadIO m => RuntimeImplemented Logging m a -> m a
- muteLogs :: Monad m => RuntimeImplemented Logging m a -> m a
- witherLogs :: MonadEffect Logging m => (Log -> m (Maybe Log)) -> RuntimeImplemented Logging m a -> m a
- filterLogs :: MonadEffect Logging m => (Log -> Bool) -> RuntimeImplemented Logging m a -> m a
- mapLogs :: MonadEffect Logging m => (Log -> m Log) -> RuntimeImplemented Logging m a -> m a
- logIfDepthLessThan :: MonadEffect Logging m => Int -> RuntimeImplemented Logging m a -> m a
- logIfDepth :: MonadEffect Logging m => (Int -> Bool) -> RuntimeImplemented Logging m a -> m a
- messagesToCrumbs :: (MonadIO m, MonadEffect Logging m) => RuntimeImplemented Logging m a -> m a
- collectCrumbs :: MonadEffect Logging m => RuntimeImplemented Logging (StateT [Crumb] m) a -> m a
- addUserToLogs :: MonadEffect Logging m => LogUser -> RuntimeImplemented Logging m a -> m a
- addCrumbToLogs :: MonadEffect Logging m => Crumb -> RuntimeImplemented Logging m a -> m a
- setDataWithSummary :: MonadEffect Logging m => LogData -> RuntimeImplemented Logging m a -> m a
- setDataTo :: MonadEffect Logging m => ByteString -> RuntimeImplemented Logging m a -> m a
- setDataToJsonOf :: (MonadEffect Logging m, ToJSON v) => v -> RuntimeImplemented Logging m a -> m a
- setDataToShowOf :: (MonadEffect Logging m, Show v) => v -> RuntimeImplemented Logging m a -> m a
- setTimestampToNow :: (MonadEffect Logging m, MonadIO m) => RuntimeImplemented Logging m a -> m a
- highlightT :: [Highlight] -> Text -> Text
- yellow :: Text -> Text
- colorFromLevel :: Level -> [Highlight]
- writeDataToFiles :: (MonadEffect Logging m, MonadIO m) => FilePath -> RuntimeImplemented Logging m a -> m a
- truncate :: Int -> Text -> Text
- manyLines :: Int -> Text -> Text
- prettyPrintSummary :: MonadIO m => Int -> RuntimeImplemented Logging m a -> m a
- logIOExceptions :: (MonadEffect Logging m, MonadCatch m) => m a -> m a
Documentation
The logging effect.
Arbitrary piece of text. Logs contain a list of these.
A name for a "layer" of your application. Typically, a log will contain a stack of contexts. Think of it as a call stack specific for your application.
The severity of the log.
If a notion of a user exists for your application, you can add this information to your logs.
LogUser | |
|
Breadcrumbs are the steps that happened before a log.
Crumb | |
|
Crumbs come in two varieties. A normal crumb is a list of key-value pairs. There's also a
HttpCrumb
where you can put more specific information about the processed HTTP request (if
your application is a web server).
DefaultCrumb [(Text, Value)] | |
HttpCrumb | |
|
Logs can hold arbitrary data serialized as a ByteString
. Additionally, a summary can be
provided which is intended to be displayed fully in log summaries.
newtype GenericException Source #
A generic exception holding only a piece of text.
handleLogging :: Functor m => (Log -> m ()) -> RuntimeImplemented Logging m a -> m a Source #
A generic handler for logs. Since it's polymorphic in m
you can choose to emit more logs
and make it a log transformer instead.
layerLogs :: (HasCallStack, MonadEffect Logging m) => Context -> RuntimeImplemented Logging m a -> m a Source #
Add a new context on top of every log that comes from the given computation.
logWithLevel :: (HasCallStack, MonadEffect Logging m) => Level -> Text -> m () Source #
logInfo :: (HasCallStack, MonadEffect Logging m) => Text -> m () Source #
logWarning :: (HasCallStack, MonadEffect Logging m) => Text -> m () Source #
logError :: (HasCallStack, MonadEffect Logging m) => Text -> m () Source #
logDebug :: (HasCallStack, MonadEffect Logging m) => Text -> m () Source #
logFatal :: (HasCallStack, MonadEffect Logging m) => Text -> m () Source #
logAndError :: (Exception e, MonadEffect Logging m, MonadThrow m, HasCallStack) => Text -> e -> m a Source #
Log an error and then throw the given exception.
logAndThrowsErr :: (MonadEffects '[Logging, Signal e Void] m, HasCallStack) => Text -> e -> m a Source #
Log an error and then throw a checked exception.
Read about checked exceptions in Signal
.
logAndThrowGeneric :: (MonadEffect Logging m, MonadThrow m, HasCallStack) => Text -> m a Source #
Log an error and throw a generic exception containing the text of the error message.
logMessagesToStdout :: MonadIO m => RuntimeImplemented Logging m a -> m a Source #
Log a stripped-down version of the logs to the console. Only contains the message and the severity.
logRawToStdout :: MonadIO m => RuntimeImplemented Logging m a -> m a Source #
witherLogs :: MonadEffect Logging m => (Log -> m (Maybe Log)) -> RuntimeImplemented Logging m a -> m a Source #
Use the given function to transform and possibly discard logs.
filterLogs :: MonadEffect Logging m => (Log -> Bool) -> RuntimeImplemented Logging m a -> m a Source #
Only let through logs that satisfy the given predicate.
mapLogs :: MonadEffect Logging m => (Log -> m Log) -> RuntimeImplemented Logging m a -> m a Source #
Transform logs with the given function.
logIfDepthLessThan :: MonadEffect Logging m => Int -> RuntimeImplemented Logging m a -> m a Source #
Filter out logs that are comming from below a certain depth.
logIfDepth :: MonadEffect Logging m => (Int -> Bool) -> RuntimeImplemented Logging m a -> m a Source #
Filter logs whose depth satisfies the given predicate.
messagesToCrumbs :: (MonadIO m, MonadEffect Logging m) => RuntimeImplemented Logging m a -> m a Source #
For each log, add it's message to the logs breadcrumb list. This is useful so you don't have to manually add crumbs.
collectCrumbs :: MonadEffect Logging m => RuntimeImplemented Logging (StateT [Crumb] m) a -> m a Source #
Each log that passes through will get all of the crumbs of the previous logs added. If, for example, you're writing a web server, you might want to have this handler over the request handler so that if an error occurs you can see all the steps that happened before it, during the handling of that request.
addUserToLogs :: MonadEffect Logging m => LogUser -> RuntimeImplemented Logging m a -> m a Source #
Add a user to every log.
addCrumbToLogs :: MonadEffect Logging m => Crumb -> RuntimeImplemented Logging m a -> m a Source #
Add a crumb to every log.
setDataWithSummary :: MonadEffect Logging m => LogData -> RuntimeImplemented Logging m a -> m a Source #
Attach arbitrary data to every log. Typically you want to use this handler on
logX
functions directly like setDataWithSummary "some data" (logInfo "some info")
setDataTo :: MonadEffect Logging m => ByteString -> RuntimeImplemented Logging m a -> m a Source #
Attach an arbitrary ByteString
to every log. Typically you want to use this handler on
logX
functions directly like setDataTo "some data" (logInfo "some info")
setDataToJsonOf :: (MonadEffect Logging m, ToJSON v) => v -> RuntimeImplemented Logging m a -> m a Source #
Attach an arbitrary value to every log using it's ToJSON
instance.
Typically you want to use this handler on logX
functions directly like
setDataToJsonOf 123 (logInfo "some info")
setDataToShowOf :: (MonadEffect Logging m, Show v) => v -> RuntimeImplemented Logging m a -> m a Source #
Attach an arbitrary value to every log using it's Show
instance.
Typically you want to use this handler on logX
functions directly like
setDataToShowOf 123 (logInfo "some info")
setTimestampToNow :: (MonadEffect Logging m, MonadIO m) => RuntimeImplemented Logging m a -> m a Source #
Add the current time to every log.
colorFromLevel :: Level -> [Highlight] Source #
writeDataToFiles :: (MonadEffect Logging m, MonadIO m) => FilePath -> RuntimeImplemented Logging m a -> m a Source #
Puts data of each log into a separate file inside of a given directory. Replaces the data of the logs with the path to the files.
prettyPrintSummary :: MonadIO m => Int -> RuntimeImplemented Logging m a -> m a Source #
Print out the logs in rich format. Truncates at the given length. Logs will contain: message, timestamp, data, user and the call stack.
logIOExceptions :: (MonadEffect Logging m, MonadCatch m) => m a -> m a Source #
Catches all IO exceptions, logs them and throws them back. The callstack in the log is not the callstack of the exception.