|
| 1 | +# Blammo-wai |
| 2 | + |
| 3 | +Integration of [Blammo](https://hackage.haskell.org/package/Blammo) |
| 4 | +with [wai](https://hackage.haskell.org/package/wai). |
| 5 | + |
| 6 | +<!-- |
| 7 | +```haskell |
| 8 | +{-# LANGUAGE DeriveAnyClass #-} |
| 9 | +{-# LANGUAGE DeriveGeneric #-} |
| 10 | +{-# LANGUAGE DerivingVia #-} |
| 11 | +{-# LANGUAGE EmptyCase #-} |
| 12 | +{-# LANGUAGE EmptyDataDeriving #-} |
| 13 | +{-# LANGUAGE TypeFamilies #-} |
| 14 | +
|
| 15 | +module Main (module Main) where |
| 16 | +
|
| 17 | +import Prelude |
| 18 | +
|
| 19 | +import Control.Exception (displayException) |
| 20 | +import Control.Monad (when) |
| 21 | +import Data.Aeson |
| 22 | +import Data.Text (Text) |
| 23 | +import Text.Markdown.Unlit () |
| 24 | +import Control.Lens (lens) |
| 25 | +``` |
| 26 | +--> |
| 27 | +
|
| 28 | +```haskell |
| 29 | +-- Blammo |
| 30 | +import Blammo.Logging |
| 31 | +
|
| 32 | +-- wai |
| 33 | +import Network.Wai (Middleware) |
| 34 | +
|
| 35 | +-- Blammo-wai |
| 36 | +import Network.Wai.Middleware.Logging |
| 37 | +
|
| 38 | +-- warp |
| 39 | +import qualified Network.Wai.Handler.Warp as Warp |
| 40 | +
|
| 41 | +-- yesod |
| 42 | +import Yesod.Core (Yesod) |
| 43 | +import qualified Yesod.Core as Yesod |
| 44 | +``` |
| 45 | +
|
| 46 | +<!-- |
| 47 | +```haskell |
| 48 | +main :: IO () |
| 49 | +main = pure () |
| 50 | +``` |
| 51 | +--> |
| 52 | +
|
| 53 | +```haskell |
| 54 | +data App = App |
| 55 | + { appLogger :: Logger |
| 56 | + -- etc. |
| 57 | + } |
| 58 | +
|
| 59 | +instance HasLogger App where |
| 60 | + loggerL = lens appLogger $ \x y -> x {appLogger = y} |
| 61 | +``` |
| 62 | +
|
| 63 | +## Integration with WAI |
| 64 | +
|
| 65 | +```haskell |
| 66 | +waiMiddleware :: App -> Middleware |
| 67 | +waiMiddleware app = |
| 68 | + addThreadContext ["app" .= ("my-app" :: Text)] |
| 69 | + . requestLogger app |
| 70 | + . Yesod.defaultMiddlewaresNoLogging |
| 71 | +``` |
| 72 | +
|
| 73 | +## Integration with Warp |
| 74 | +
|
| 75 | +```haskell |
| 76 | +warpSettings :: App -> Warp.Settings |
| 77 | +warpSettings app = Warp.setOnException onEx $ Warp.defaultSettings |
| 78 | + where |
| 79 | + onEx _req ex = |
| 80 | + when (Warp.defaultShouldDisplayException ex) |
| 81 | + $ runWithLogger app |
| 82 | + $ logError |
| 83 | + $ "Warp exception" :# ["exception" .= displayException ex] |
| 84 | +``` |
| 85 | +
|
| 86 | +## Integration with Yesod |
| 87 | +
|
| 88 | +<!-- |
| 89 | +```haskell |
| 90 | +instance Yesod.RenderRoute App where |
| 91 | + data Route App deriving stock Eq |
| 92 | + renderRoute = \case{} |
| 93 | +``` |
| 94 | +--> |
| 95 | +
|
| 96 | +```haskell |
| 97 | +instance Yesod App where |
| 98 | + messageLoggerSource app _logger loc source level msg = |
| 99 | + runWithLogger app $ monadLoggerLog loc source level msg |
| 100 | +``` |
0 commit comments