Copyright | (c) 2018-2019 Kowainik |
---|---|
License | MPL-2.0 |
Maintainer | Kowainik <xrom.xkov@gmail.com> |
Safe Haskell | None |
Language | Haskell2010 |
This module contains logging messages data types along with the formatting and logging actions for them.
Synopsis
- data Msg sev = Msg {
- msgSeverity :: !sev
- msgStack :: !CallStack
- msgText :: !Text
- type Message = Msg Severity
- log :: WithLog env (Msg sev) m => sev -> Text -> m ()
- logDebug :: WithLog env Message m => Text -> m ()
- logInfo :: WithLog env Message m => Text -> m ()
- logWarning :: WithLog env Message m => Text -> m ()
- logError :: WithLog env Message m => Text -> m ()
- logException :: forall e m env. (WithLog env Message m, Exception e) => e -> m ()
- fmtMessage :: Message -> Text
- showSeverity :: Severity -> Text
- showSourceLoc :: CallStack -> Text
- type family FieldType (fieldName :: Symbol) :: Type
- newtype MessageField (m :: Type -> Type) (fieldName :: Symbol) where
- MessageField :: forall fieldName m. m (FieldType fieldName) -> MessageField m fieldName
- unMessageField :: forall fieldName m. MessageField m fieldName -> m (FieldType fieldName)
- extractField :: Applicative m => Maybe (MessageField m fieldName) -> m (Maybe (FieldType fieldName))
- type FieldMap (m :: Type -> Type) = TypeRepMap (MessageField m)
- defaultFieldMap :: MonadIO m => FieldMap m
- data RichMessage (m :: Type -> Type) = RichMessage {
- richMessageMsg :: !Message
- richMessageMap :: !(FieldMap m)
- fmtRichMessageDefault :: MonadIO m => RichMessage m -> m Text
- upgradeMessageAction :: forall m. FieldMap m -> LogAction m (RichMessage m) -> LogAction m Message
Basic message type
General logging message data type. Contains the following fields:
- Polymorhic severity. This can be anything you want if you need more flexibility.
- Function
CallStack
. It provides useful information about source code locations where each particular function was called. - Custom text for logging.
log :: WithLog env (Msg sev) m => sev -> Text -> m () Source #
Logs the message with given severity sev
.
logWarning :: WithLog env Message m => Text -> m () Source #
Logs the message with the Warning
severity.
logException :: forall e m env. (WithLog env Message m, Exception e) => e -> m () Source #
Logs Exception
message.
Formatting functions
fmtMessage :: Message -> Text Source #
Formats the Message
type in according to the following format:
[Severity] [SourceLocation] <Text message>
Examples:
[Warning] [Main.app#39] Starting application... [Debug] [Main.example#34] app: First message...
See fmtRichMessageDefault
for richer format.
showSeverity :: Severity -> Text Source #
Formats severity in different colours with alignment.
showSourceLoc :: CallStack -> Text Source #
Show source code locations in the following format:
[Main.example#35]
Externally extensible message type
Field of the dependent map
type family FieldType (fieldName :: Symbol) :: Type Source #
Open type family that maps some user defined tags (type names) to actual types. The type family is open so you can add new instances.
newtype MessageField (m :: Type -> Type) (fieldName :: Symbol) where Source #
newtype
wrapper. Stores monadic ability to extract value of FieldType
.
Implementation detail: this exotic writing of MessageField
is required in
order to use it nicer with type applications. So users can write
MessageField @"threadId" myThreadId
instead of
MessageField _
"threadId" myThreadId
Simpler version of this newtype
:
newtype MessageField m fieldName = MessageField { unMesssageField :: m (FieldType fieldName) }
MessageField :: forall fieldName m. m (FieldType fieldName) -> MessageField m fieldName |
Instances
(KnownSymbol fieldName, a ~ m (FieldType fieldName)) => IsLabel fieldName (a -> WrapTypeable (MessageField m)) Source # | |
Defined in Colog.Message fromLabel :: a -> WrapTypeable (MessageField m) # |
unMessageField :: forall fieldName m. MessageField m fieldName -> m (FieldType fieldName) Source #
Extracts field from the MessageField
constructor.
extractField :: Applicative m => Maybe (MessageField m fieldName) -> m (Maybe (FieldType fieldName)) Source #
Helper function to deal with MessageField
when looking it up in the FieldMap
.
Dependent map that allows to extend logging message
type FieldMap (m :: Type -> Type) = TypeRepMap (MessageField m) Source #
Depedent map from type level strings to the corresponding types. See
FieldType
for mapping between names and types.
defaultFieldMap :: MonadIO m => FieldMap m Source #
Default message map that contains actions to extract ThreadId
and
Time
. Basically, the following mapping:
"threadId" ->myThreadId
"posixTime" ->now
data RichMessage (m :: Type -> Type) Source #
Contains additional data to Message
to display more verbose information.
RichMessage | |
|
fmtRichMessageDefault :: MonadIO m => RichMessage m -> m Text Source #
Formats RichMessage
in the following way:
[Severity] [Time] [SourceLocation] [ThreadId] <Text message>
Examples:
[Debug] [03 05 2019 05:23:19.058] [Main.example#34] [ThreadId 11] app: First message... [Info] [03 05 2019 05:23:19.059] [Main.example#35] [ThreadId 11] app: Second message...
See fmtMessage
if you don't need both time and thread id.
upgradeMessageAction :: forall m. FieldMap m -> LogAction m (RichMessage m) -> LogAction m Message Source #
Allows to extend basic Message
type with given dependent map of fields.