Skip to content

Instantly share code, notes, and snippets.

@reactormonk
Created March 7, 2018 17:34
Show Gist options
  • Save reactormonk/2b582015a37b18fd8e6723751e7e8b3d to your computer and use it in GitHub Desktop.
Save reactormonk/2b582015a37b18fd8e6723751e7e8b3d to your computer and use it in GitHub Desktop.
#!/usr/bin/env stack
{- stack --install-ghc
--resolver lts-10.7
script
--compile
--package katip
--package universum
--package aeson
-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
import Data.Aeson
import Katip
import Katip.Monadic
import Universum
main = do
loggingBracket $ \logging ->
runKatipContextT (ltsLogEnv logging) (ltsContext logging) (ltsNamespace logging) app
newtype FlowId = FlowId Text
instance LogItem FlowId where
payloadKeys _ _ = AllKeys
instance ToJSON FlowId where
toJSON (FlowId flowid) = toJSON flowid
instance ToObject FlowId
app :: KatipContext m => m ()
app = do
katipAddContext (FlowId "foo") $ logFM InfoS "Foobar"
makeLogEnv :: IO LogEnv
makeLogEnv = do
let env = (Environment "start")
handleScribe <- mkHandleScribe ColorIfTerminal stdout InfoS V2
registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv (Namespace ["TestService"]) env
loggingBracket :: (KatipContextTState -> IO c) -> IO c
loggingBracket fun = do
bracket makeLogEnv closeScribes $ \le -> do
let
logging = KatipContextTState
{ ltsLogEnv = le
, ltsContext = liftPayload () -- this context will be attached to every log in your app and merged w/ subsequent contexts
, ltsNamespace = "main"
}
fun logging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment