Skip to content

Commit

Permalink
Merge pull request #32 from herp-inc/message-builders
Browse files Browse the repository at this point in the history
Payloadを構築する関数をいくつか追加
  • Loading branch information
fumieval authored Dec 27, 2022
2 parents 284ac00 + d0f6b9d commit 86a4bc1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Herp/Logger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module Herp.Logger
, P.level
, P.message
, P.object
, P.messageString
, P.messageShow
, P.messageException
-- * monad-logger
, runLoggingT
, toLoggerIO
Expand Down
36 changes: 36 additions & 0 deletions src/Herp/Logger/Payload.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ module Herp.Logger.Payload
, level
, message
, object
, messageString
, messageShow
, messageUtf8
, messageUtf8Lazy
, messageUtf8Builder
, messageException
) where

import Control.Exception
import Data.Aeson (Object, KeyValue(..))
import Data.ByteString (ByteString)
import Data.ByteString.Builder qualified as BB
import Data.ByteString.Lazy qualified as BL
import Data.Semigroup
import Data.String
import Data.Text (Text)
import Data.Text.Encoding
import Data.Typeable
import Generic.Data
import GHC.Exts
import GHC.OverloadedLabels
Expand Down Expand Up @@ -38,8 +50,32 @@ level lvl = mempty { payloadLevel = Max lvl }
message :: Text -> Payload
message txt = mempty { payloadMessage = txt }

messageString :: String -> Payload
messageString = message . fromString

messageShow :: Show a => a -> Payload
messageShow = messageString . show

messageUtf8 :: ByteString -> Payload
messageUtf8 = message . decodeUtf8

messageUtf8Lazy :: BL.ByteString -> Payload
messageUtf8Lazy = messageUtf8 . BL.toStrict

messageUtf8Builder :: BB.Builder -> Payload
messageUtf8Builder = messageUtf8Lazy . BB.toLazyByteString

messageException :: Exception e => e -> Payload
messageException e = messageString (displayException e)
<> "exception_type" .= show exceptionType
where
exceptionType = case cast e of
Just (SomeException e') -> typeOf e'
_ -> typeOf e

object :: Object -> Payload
object obj = mempty { payloadObject = obj }
{-# WARNING object "This might confuse Datadog!" #-}

instance IsString Payload where
fromString = message . fromString
Expand Down

0 comments on commit 86a4bc1

Please sign in to comment.