Smart logging hacks in ruby
Posted Fri, 12 Nov 2010
This lead to me subclassing Logger and additionally providing my own logger format class. The code for this is in logstash, logging.rb.
What did I add? Two main goals: First, improve context. Second, log structured data (objects). This is achieved by style changes (log objects, not strings), adding awesome_print support, adding code context to each log (line/file/method), etc.
To support the first goal (context), if the loglevel is 'debug' I will inspect the call stack and include the file and line of code that is logging. I also set the 'progname' to the name of the program by default. To support the second goal, log objects and improve how objects are formatted into strings with Object#inspect (or awesome_inspect, if available).
If you have awesome_print available, the object output by my formatter gets even more useful for human viewing:
Why log structured? Easier to parse and query later, like in a logstash query.