1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Logging what Mechanize Does


In order to analyze what went wrong when Mechanize raises an exception, you'll need a log file. Mechanize can log everything to a file, including every header passed. In order to log everything, pass a new Logger object to the Mechanize#log method.

agent = Mechanize.new do|a|
  a.log = Logger.new('log.txt')
  a.user_agent_alias = "Windows IE 6"

However, this will indeed log everything, making for a very fat log file. If you don't need all that information, you can dial back the amount that logger will actually write to the log file. General messages are logged on the INFO level, while all the (possibly irrelevant) details are logged on the DEBUG level. Simply dial back the logger level to Logger::INFO.

log = Logger.new('log.txt')
log.level = Logger::INFO

agent = Mechanize.new do|a|
  a.log = log
  a.user_agent_alias = "Windows IE 6"
  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. The Mechanize 2.0 Handbook
  6. Logging what Mechanize Does

©2014 About.com. All rights reserved.