#56
Jul 11, 2007

The Logger

Learn all about the logger in this episode. See how to send messages to the log and how to customize it.
Tags: tools
Download (46.6 MB, 9:49)
alternative download for iPod & Apple TV (19.1 MB, 9:49)

Resources

# ~/.irbrc
if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
  require 'logger'
  RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
end

# carts_controller.rb
def show
  @cart = current_cart
  logger.debug "Hello world! #{@cart.to_yaml}"
  # debug, info, warn, error, fatal
end

# environment.rb
Rails::Initializer.run do |config|
  config.active_record.colorize_logging = false
end

class Logger
  def format_message(level, time, progname, msg)
    "#{time.to_s(:db)} #{level} -- #{msg}\n"
  end
end

# environments/production.rb
config.log_level = :debug
config.logger = Logger.new(...) # or log4r

RSS Feed for Episode Comments 6 comments

1. chineseGuy Jul 11, 2007 at 00:02

sofa!


2. Rafael Jul 11, 2007 at 01:42

:-)

thank you, great!


3. Ekolguy Jul 11, 2007 at 01:56

Thanks for the great screencast! One question though:

config.active_record.colorize_logging = false

There is no way one could find colorize_logging method of active record in API (or is it not a method? Anyway how to find it?). Some time ago colored logging was annoying me under windows and I just couldn't find a way to disable it. (before watching your screeny of course:))

These kind of small things are still keeping me away from diving into rails because I'm afraid of not finding what I'm looking for :(

Any advices?
Thanks!


4. art Jul 11, 2007 at 02:49

I don't know about Mongrel but with Webrick you can use "puts" instead of logger.debug to display whatever you like in the Webrick log. This works in plugins, views, controllers, models...I think everywhere =)


5. Ryan Bates Jul 11, 2007 at 07:24

@Ekolguy, I don't think it's documented in the API. I learned about it from other sites which talk about customizing Rails.

@art, I don't recommend using puts. This will cause problems when you switch to other web servers (apache or lighty). It messes up the HTTP headers which can cause strange bugs.


6. Gary Jul 11, 2007 at 08:03

@art: I'm not sure if it's the same for the ruby logger but in with log4j using the logger meant that you could send any text to /dev/null so that it didn't take up any CPU time in production. Using puts would remove that benefit unless you'd launch your server and redirect all output to /dev/null explicitely.


7. Geoff Buesing Jul 11, 2007 at 08:25

Jamis Buck posted about a nice script/console trick that allows you to toggle on ActiveRecord log output -- handy when you want to see the generated sql from an AR query: http://weblog.jamisbuck.org/2007/1/31/more-on-watching-activerecord


8. art Jul 12, 2007 at 02:37

@Ryan & Gary: Thanks for the tip. I didn't know that.


9. brb Sep 05, 2007 at 11:15

You can add tail to windows C:\Program Files\Windows Resource Kits\Tools.. Tail.exe . Thanks Ryan.Great Work!!!!!!!!!!!!!


10. rand Nov 11, 2007 at 17:21

Thanks for the code. It has helped me.


11. jc Nov 21, 2008 at 20:58

If anyone is having trouble on Windows I've found a solution:
http://ifakedit.com/log/2008/11/21/pretty-rails-logs-in-windows/

It uses MSYS that Git comes with to tail the file. This enables coloring and such.


12. hd lcd monitor Jul 24, 2009 at 04:39

thanks for this.


13. AlexBaranosky Jan 30, 2010 at 09:19

Does this work under the new versions of Rails? I'm having trouble with it.


14. xbox 360 Feb 05, 2010 at 02:22

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.


15. dlbaobei Mar 15, 2010 at 06:08

I like your rails cast topic, it is very helpful for me to study rails.


16. Pick Up Japanese Girls Apr 14, 2010 at 11:24

Downloaded and installed. Will post feedback later.


18. biology degree Aug 03, 2010 at 00:27

Good Job


19. free directory list Aug 11, 2010 at 22:20

I really appreciate what you post.


20. Air Jordan Spizike Aug 20, 2010 at 22:49

Thanks for the code. It has helped me. I enjoyed reading your post and I like your take on the issue. I really enjoy watching the RailsCasts. I think type of site that is useful in sharing information and it is important to share.


21. christian louboutin flats Aug 25, 2010 at 00:40

it's a good application. Thanks


22. snow boots Aug 31, 2010 at 01:23

I don't recommend using puts. This will cause problems when you switch to other web servers (apache or lighty).

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source