@hackage Blammo-wai0.0.0.1

Using Blammo with WAI

Blammo-wai

Integration of Blammo with wai.

-- Blammo
import Blammo.Logging

-- wai
import Network.Wai (Middleware)

-- Blammo-wai
import Network.Wai.Middleware.Logging

-- warp
import qualified Network.Wai.Handler.Warp as Warp

-- yesod
import Yesod.Core (Yesod)
import qualified Yesod.Core as Yesod
data App = App
  { appLogger :: Logger
  -- etc.
  }

instance HasLogger App where
  loggerL = lens appLogger $ \x y -> x {appLogger = y}

Integration with WAI

waiMiddleware :: App -> Middleware
waiMiddleware app =
  addThreadContext ["app" .= ("my-app" :: Text)]
    . requestLogger app
    . Yesod.defaultMiddlewaresNoLogging

Integration with Warp

warpSettings :: App -> Warp.Settings
warpSettings app = Warp.setOnException onEx $ Warp.defaultSettings
 where
  onEx _req ex =
    when (Warp.defaultShouldDisplayException ex)
      $ runWithLogger app
      $ logError
      $ "Warp exception" :# ["exception" .= displayException ex]

Integration with Yesod

instance Yesod App where
  messageLoggerSource app _logger loc source level msg =
    runWithLogger app $ monadLoggerLog loc source level msg