tuto – ruby – logger

Le logger dans ruby, un objet très utile pour suivre au plus près votre activité (notamment quand vous faite du requêtage http).
Dans l’exemple suivant, je vais ajouter le logger en le branchant directement comme logger d’activité de l’objet mechanize. Ainsi, chaque requête http du robot mechanize loggera les infos en rapport avec les aller retours des requêtes http.

 

Les lib nécessaires ici :

require 'logger'
require 'mechanize'

 

Création de l’agent mechanize avec le logger de la class Logger (vous pouvez mettre en commentaire le logger sur STDOUT, càd le terminal, et décommenter la ligne pour logger dans un fichier directement) :

   def make_agent
      @agent = Mechanize.new do |a|
        a.user_agent_alias = 'Mac Safari'
        a.log = Logger.new(STDOUT)
        #a.log = Logger.new('mon_fichier_log')
        a.log.level = Logger::INFO # WARN ...
      end
    end

 

Vous aurez en sortie des informations du type :

# Logfile created on 2013-06-13 23:20:34 +0200 by logger.rb/25413
I, [2013-06-13T23:20:42.373640 #3243]  INFO -- : Net::HTTP::Get: /
D, [2013-06-13T23:20:42.373848 #3243] DEBUG -- : request-header: accept => */*
D, [2013-06-13T23:20:42.373939 #3243] DEBUG -- : request-header: user-agent => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/534.51.22 (KHTML, like Gecko) Version/5.1.1 Safari/534.51.22
D, [2013-06-13T23:20:42.374025 #3243] DEBUG -- : request-header: accept-encoding => gzip,deflate,identity
D, [2013-06-13T23:20:42.374106 #3243] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2013-06-13T23:20:42.374186 #3243] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2013-06-13T23:20:42.374264 #3243] DEBUG -- : request-header: host => www.monip.org
I, [2013-06-13T23:20:43.905271 #3243]  INFO -- : status: Net::HTTPOK 1.1 200 OK
D, [2013-06-13T23:20:43.905531 #3243] DEBUG -- : response-header: date => Thu, 13 Jun 2013 21:20:43 GMT
D, [2013-06-13T23:20:43.905677 #3243] DEBUG -- : response-header: server => Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny16 with Suhosin-Patch mod_ssl/2.2.9 OpenSSL/0.9.8g
D, [2013-06-13T23:20:43.905765 #3243] DEBUG -- : response-header: x-powered-by => PHP/5.2.6-1+lenny16
D, [2013-06-13T23:20:43.905855 #3243] DEBUG -- : response-header: vary => Accept-Encoding
D, [2013-06-13T23:20:43.905935 #3243] DEBUG -- : response-header: content-encoding => gzip
D, [2013-06-13T23:20:43.906057 #3243] DEBUG -- : response-header: content-length => 478
D, [2013-06-13T23:20:43.906137 #3243] DEBUG -- : response-header: content-type => text/html
D, [2013-06-13T23:20:43.906347 #3243] DEBUG -- : Read 0 bytes (0 total)
D, [2013-06-13T23:20:44.219489 #3243] DEBUG -- : Read 478 bytes (478 total)
D, [2013-06-13T23:20:44.220020 #3243] DEBUG -- : gzip response
I, [2013-06-13T23:20:44.322462 #3243]  INFO -- : Net::HTTP::Get: /
D, [2013-06-13T23:20:44.322750 #3243] DEBUG -- : request-header: accept => */*
D, [2013-06-13T23:20:44.322847 #3243] DEBUG -- : request-header: user-agent => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/534.51.22 (KHTML, like Gecko) Version/5.1.1 Safari/534.51.22
D, [2013-06-13T23:20:44.322953 #3243] DEBUG -- : request-header: accept-encoding => gzip,deflate,identity
D, [2013-06-13T23:20:44.323038 #3243] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2013-06-13T23:20:44.323117 #3243] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2013-06-13T23:20:44.323194 #3243] DEBUG -- : request-header: host => www.google.com
I, [2013-06-13T23:20:49.814330 #3243]  INFO -- : status: Net::HTTPOK 1.1 200 OK
D, [2013-06-13T23:20:49.814585 #3243] DEBUG -- : response-header: date => Thu, 13 Jun 2013 21:20:49 GMT
D, [2013-06-13T23:20:49.814682 #3243] DEBUG -- : response-header: expires => -1
D, [2013-06-13T23:20:49.814805 #3243] DEBUG -- : response-header: cache-control => private, max-age=0
D, [2013-06-13T23:20:49.814884 #3243] DEBUG -- : response-header: content-type => text/html; charset=UTF-8
D, [2013-06-13T23:20:49.814972 #3243] DEBUG -- : response-header: set-cookie => PREF=ID=61c1cf9a79ab221e:FF=0:TM=1371158449:LM=1371158449:S=ZWlIxmAdKFAhKS87; expires=Sat, 13-Jun-2015 21:20:49 GMT; path=/; domain=.google.com, NID=67=AI0HG2URXsR5QgFLWVsc-Xxo5CVrQttIkiQzNdmPUc1e9DbIAXYX5hbxlYCQmnxgqcHJ0Hr4NPpLrjU5-DrJzXh49hUsy36ZmcCKydqE5v2qZUCbzrliNeQxI4f1RFvn; expires=Fri, 13-Dec-2013 21:20:49 GMT; path=/; domain=.google.com; HttpOnly
D, [2013-06-13T23:20:49.815054 #3243] DEBUG -- : response-header: p3p => CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
D, [2013-06-13T23:20:49.815153 #3243] DEBUG -- : response-header: content-encoding => gzip
D, [2013-06-13T23:20:49.815232 #3243] DEBUG -- : response-header: server => gws
D, [2013-06-13T23:20:49.815309 #3243] DEBUG -- : response-header: x-xss-protection => 1; mode=block
D, [2013-06-13T23:20:49.815394 #3243] DEBUG -- : response-header: x-frame-options => SAMEORIGIN
D, [2013-06-13T23:20:49.815475 #3243] DEBUG -- : response-header: transfer-encoding => chunked
D, [2013-06-13T23:20:49.848745 #3243] DEBUG -- : Read 1434 bytes (1434 total)
D, [2013-06-13T23:20:49.921896 #3243] DEBUG -- : Read 1440 bytes (2874 total)
D, [2013-06-13T23:20:50.134030 #3243] DEBUG -- : Read 1440 bytes (4314 total)
D, [2013-06-13T23:20:50.136202 #3243] DEBUG -- : Read 1440 bytes (5754 total)
D, [2013-06-13T23:20:50.158409 #3243] DEBUG -- : Read 1440 bytes (7194 total)
D, [2013-06-13T23:20:50.159504 #3243] DEBUG -- : Read 597 bytes (7791 total)
D, [2013-06-13T23:20:50.546207 #3243] DEBUG -- : Read 1430 bytes (9221 total)
D, [2013-06-13T23:20:50.767451 #3243] DEBUG -- : Read 1440 bytes (10661 total)
D, [2013-06-13T23:20:50.792567 #3243] DEBUG -- : Read 1440 bytes (12101 total)
D, [2013-06-13T23:20:50.795048 #3243] DEBUG -- : Read 1440 bytes (13541 total)
D, [2013-06-13T23:20:50.797601 #3243] DEBUG -- : Read 1440 bytes (14981 total)
D, [2013-06-13T23:20:50.914760 #3243] DEBUG -- : Read 4076 bytes (19057 total)
D, [2013-06-13T23:20:50.920583 #3243] DEBUG -- : Read 1440 bytes (20497 total)
D, [2013-06-13T23:20:51.089201 #3243] DEBUG -- : Read 1440 bytes (21937 total)
D, [2013-06-13T23:20:51.091657 #3243] DEBUG -- : Read 1440 bytes (23377 total)
D, [2013-06-13T23:20:51.141959 #3243] DEBUG -- : Read 1440 bytes (24817 total)
D, [2013-06-13T23:20:51.144359 #3243] DEBUG -- : Read 1440 bytes (26257 total)
D, [2013-06-13T23:20:51.191451 #3243] DEBUG -- : Read 1440 bytes (27697 total)
D, [2013-06-13T23:20:51.211681 #3243] DEBUG -- : Read 114 bytes (27811 total)
D, [2013-06-13T23:20:51.649481 #3243] DEBUG -- : Read 1013 bytes (28824 total)
D, [2013-06-13T23:20:51.649831 #3243] DEBUG -- : Read 391 bytes (29215 total)
D, [2013-06-13T23:20:51.653773 #3243] DEBUG -- : gzip response
D, [2013-06-13T23:20:51.671535 #3243] DEBUG -- : saved cookie: PREF=ID=61c1cf9a79ab221e:FF=0:TM=1371158449:LM=1371158449:S=ZWlIxmAdKFAhKS87
D, [2013-06-13T23:20:51.672104 #3243] DEBUG -- : saved cookie: NID=67=AI0HG2URXsR5QgFLWVsc-Xxo5CVrQttIkiQzNdmPUc1e9DbIAXYX5hbxlYCQmnxgqcHJ0Hr4NPpLrjU5-DrJzXh49hUsy36ZmcCKydqE5v2qZUCbzrliNeQxI4f1RFvn

 

Pratique pour débugger un souci dans un bout de code orienté http .. notamment.

En complément, un lien sur la librairie logger : http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html