0

I am a newbie to rails. I am using log4r for logging. I observed that when i use just logger in my models, it doesn't work. But if i use Rails.logger the logs are coming fine. Why is it happening? Please help me to understand.

  def self.create_user_from_params(auth_params)
    Rails.logger.debug "auth params are ";
    return nil;
  end

works. But if i use

  def self.create_user_from_params(auth_params)
    logger.debug "auth params are ";
    return nil;
  end

it doesn't work. Here is my log4r yml

log4r_config:
  # define all loggers ...
  loggers:
    - name      : production
      level     : INFO
      trace     : 'false'
      outputters :
      - datefile
    - name      : development
      level     : DEBUG
      trace     : 'true'
      outputters :
      - datefile

  # define all outputters (incl. formatters)
  outputters:
  - type: DateFileOutputter
    name: datefile
    dirname: "log"
    filename: "server.log" # notice the file extension is needed!
    formatter:
      date_pattern: '%H:%M:%S'
      pattern     : '%d %l: %m '
      type        : PatternFormatter

update I just remembered that i had added 'config.active_record.logger = Logger.new("log/sql.log")' in my config/application.rb

I used that as i wanted to log my sqls to separate file. Now i got another question. How to log sqls to separate file without changing config.active_record.logger?

Anirudhan J
  • 2,072
  • 6
  • 27
  • 45
  • Are the logs being appended to server.log? – Jesse Wolgamott Jun 11 '13 at 14:27
  • No. If i just use logger.info/logger.debug it doesnt go to server.log. If i use Rail.logger then it goes to server.log – Anirudhan J Jun 11 '13 at 15:31
  • It looks like you are not using log4r -- you're just using the default logger. My recommendation: don't spend time on this -- spend time on features that matter, and when you've learned more about Rails, come back and tackle the configuration and use of the logger. – Jesse Wolgamott Jun 11 '13 at 15:36

1 Answers1

0

Look at this in-depth tutorial on how to use Log4r.

Specifically your issue can be solved by setting config.logger to Log4r::Logger["development"] (and therefore Rails.logger will log to Log4r) or specifying logger = Log4r::Logger["development"] in the files you want to use "logger". Setting the config.logger flag however should work with Rails.logger.debug as well as logger.debug.

SQL in a separate file is also described in detail in the linked blogpost. Basically you use the Rails Notification Pipeline and write to a custom logger that logs to another file.

mmlac
  • 1,091
  • 1
  • 9
  • 24