[Mechanize-users] Logging response headers in multi-threaded implementation of Mechanize

Aaron Starr astarr at wiredquote.com
Mon Aug 23 16:50:51 EDT 2010

Hi, all,

I'm using Mechanize in an environment where multiple threads are running
simultaneously. Each thread uses its own Mechanize object. This works
swimmingly, except that the threads step on each other when logging. So,
I've used the following code to insure that each mechanize object has its
own log that it's writing to, for each transaction. (Also at

    @mech = Mechanize.new do |mech|
      # put the log in the Mechanize object, and not in the class
      def mech.log=(val); @my_log = val; end
      def mech.log; @my_log; end

    # [...]

    mech.log = Logger.new log_file_for_this_web_transaction

The problem is that I am only getting the request information and request
headers in the logs. The response headers go missing. Looking around a bit,
I find that the reason is that Mechanize logs the response headers like

    if Mechanize.log
        Mechanize.log.debug #...

So, the response headers are going to that non-existent class log. Anyone
have a brilliant and insightful work-around? It would be really nice to have
an independent, self-contained, whole log for each web transaction even when
multiple threads are going at once.

My versions:
> mechanize (1.0.0)
> ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]

Thanks in advance,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20100823/85c22219/attachment-0001.html>

More information about the Mechanize-users mailing list