Incerteza
Incerteza

Reputation: 34884

Log networks requests

Having this code:

import Data.Conduit.Binary (sinkFile)
 import Network.HTTP.Conduit
 import qualified Data.Conduit as C

 main :: IO ()
 main = do
      request <- parseUrl "http://google.com/"
      withManager $ \manager -> do
          response <- http request manager
          responseBody response C.$$+- sinkFile "google.html"

How do I log it: write to the console or a file all the events and data sent/received? I didn't find any mention about logging in the documentation.

Upvotes: 0

Views: 64

Answers (1)

Boyd Stephen Smith Jr.
Boyd Stephen Smith Jr.

Reputation: 3202

sinkFile doesn't do any logging. If you want a conduit that does some logging in addition to what sinkFile does, you'll have to write it yourself, although it should be relatively simple.

logSinkFile filename = logSinkIOHandle (IO.openBinaryFile fp IO.WriteMode)

logSinkIOHandle alloc = bracketP alloc IO.hClose logSinkHandle

logSinkHandle h = go
 where go = do
    liftIO $ putStrLn "Awaiting data."
    mbs <- await
    case mbs of
     Nothing -> return ()
     Maybe bs -> do
        liftIO $ do
            putStrLn "Data Ready."
            putStr "Writing data: "
            S.put bs
            putStrLn ""
            S.hPut h bs
            putStrLn "Data written."
        go

Or something like that.

Upvotes: 2

Related Questions