logger


What is logger?

  • Rails makes use of ruby’s standard logger, Log4r, or another logger that provides a similar interface can also be substituted if you wish.
  • A logger object is made available to each of ActiveRecord, ActiveController and ActiveMailer.

How to write logger?

  • logger.info "Starting process fubar..."

Log level?

  • IThe available log levels are: :debug, :info, :warn, :error, :fatal. The ruby Logger class normally also accepts :any, but that doesn’t seem to work with rails.
  • config.log_level = :debug

Log path?

  • The log file path is relative to the RAILS_ROOT directory. Make sure that the file exists and has permissions that allow writing for the user that rails will run under.
  • config.log_path = 'log/debug.log'

Initialize logger in ruby app?

  • ActiveRecord::Base.logger = Logger.new(STDERR)
  • ActiveRecord::Base.logger = Logger.new(File.open(‘log/database.log’, ‘a’))

Example?

begin
  RAILS_DEFAULT_LOGGER = Logger.new(“#{RAILS_ROOT}/log/#{RAILS_ENV}.log”)
rescue StandardError
  RAILS_DEFAULT_LOGGER = Logger.new(STDERR)
  RAILS_DEFAULT_LOGGER.level = Logger::WARN
  RAILS_DEFAULT_LOGGER.warn(
    “Rails Error: Unable to access log file. Please ensure that log/#{RAILS_ENV}.log exists and 
is chmod 0666. " +
    "The log level has been raised to WARN and the output directed to STDERR until the problem 
is fixed.”
  )
end

Q Is it possible to display the logs in the console rather than using the “debug.log” file?
-cd /railsapp/log
-tail -f development.log