title: How do `pass` and `listen` work in WriterT?
body: <p>The code below probably isn't a good way to do this, but it's what I've managed to cobble together. Basically, I run a series of complex tasks, during which several things get logged. At the end of each one I dump the log into a .txt file and move on to the next batch in a loop.</p>
  
  <p>To achieve this I make use of <code>listen</code> and <code>pass</code> in <code>WriterT</code> (as part of <code>RWST</code>). The code is below:</p>
  
  <pre><code>-- Miscelaneous stuff
  newtype Log = Log [String]
  type ConnectT a = EitherT String (RWST ConnectReader Log ConnectState IO) a
  
  timeStampLog :: String -&gt; Log
  timeStampLog msg = do
      theTime &lt;- liftIO $ fmap zonedTimeToLocalTime getZonedTime
      let msgStart = show theTime ++ ": "
      tell $ Log [msgStart ++ msg]
  
  logToFileIO :: Log -&gt; IO ()
  logToFileIO (Log xs) = appendFile "Log.txt" $ "\r\n" ++ intercalate "\r\n" (reverse xs)
  ---------------------
  
  
  logToFile :: ConnectT a -&gt; ConnectT ()
  logToFile cta = let ctaw = listen cta
                  in  pass $ do
                                  (_,w) &lt;- ctaw
                                  liftIO $ logToFileIO w
                                  return ((),const mempty)
  
  mapFunction :: (Show a) =&gt; a -&gt; ConnectT ()
  mapFunction a = logToFile $ do
      timeStampLog $ "Starting sequence for " ++ show a
      lotsOfLogging a
      timeStampLog $ "Finishing sequence for " ++ show a
  
  loopFunction :: ConnectT ()
  loopFunction = logToFile $ do
      timeStampLog "Starting Loop"
      mapM_ mapFunction someList
      timeStampLog "Finishing Loop"
  </code></pre>
  
  <p>What I end up with is something like this:</p>
  
  <pre><code>2015-03-17 20:21:40.8198823: Starting sequence for a
  2015-03-17 20:21:41.8198823: (logs for a)
  2015-03-17 20:21:41.8198823: Finishing sequence for a
  2015-03-17 20:21:41.8198823: Starting sequence for b
  2015-03-17 20:21:42.8198823: (logs for b)
  2015-03-17 20:21:42.8198823: Finishing sequence for b
  2015-03-17 20:21:39.8198823: Starting Loop
  2015-03-17 20:21:42.8198823: Finishing Loop
  </code></pre>
  
  <p>Where the log entry for starting/finishing the loop end up together at the end.</p>
  
  <p>I'm not entirely surprised that the call to <code>logToFile</code> in <code>mapFunction</code> doesn't include the log information from the <code>loopFunction</code> as the information hasn't passed to it via a bind.</p>
  
  <p>But I'm still having trouble understanding how <code>pass</code> and <code>listen</code> work. And also how I would go about fixing this (admittedly minor) issue.</p>