Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable IP address logging Rails

I am trying to disable logging of IP addresses while handling the request. But I am not able find a way to do this. I want to disable logging of IP for limited portion of my app where user is not yet authenticated.

So my questions is

How to disable logging of IP in rails log for specific pages(so the IP will not be saved in any log)

I am using Rails 3.2.17

EDIT: Here is sample log (from environment.log)

Started GET "/my_path" for 192.168.0.109 at 2014-03-28 11:53:20 +0530

I do not want to save 192.168.0.109 in log file

like image 563
Hardik Avatar asked Feb 03 '26 07:02

Hardik


1 Answers

In config/initializers, add file log_fomat.rb with:

class ActiveSupport::BufferedLogger
  def formatter=(formatter)
    @log.formatter = formatter
  end
end

class Formatter
  SEVERITY_TO_TAG_MAP     = {'DEBUG'=>'meh', 'INFO'=>'fyi', 'WARN'=>'hmm', 'ERROR'=>'wtf', 'FATAL'=>'omg', 'UNKNOWN'=>'???'}
  SEVERITY_TO_COLOR_MAP   = {'DEBUG'=>'0;37', 'INFO'=>'32', 'WARN'=>'33', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37'}
  USE_HUMOROUS_SEVERITIES = true

  def call(severity, time, progname, msg)
    if USE_HUMOROUS_SEVERITIES
      formatted_severity = sprintf("%-3s","#{SEVERITY_TO_TAG_MAP[severity]}")
    else
      formatted_severity = sprintf("%-5s","#{severity}")
    end

    formatted_time = time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..2].rjust(3)
    color = SEVERITY_TO_COLOR_MAP[severity]

    "\033[0;37m#{formatted_time}\033[0m [\033[#{color}m#{formatted_severity}\033[0m] #{msg.strip} (pid:#{$$})\n"
  end

end

Rails.logger.formatter = Formatter.new

References:

  1. http://rubyjunky.com/cleaning-up-rails-4-production-logging.html
  2. http://cbpowell.wordpress.com/2012/04/05/beautiful-logging-for-ruby-on-rails-3-2/
  3. Rails logger format string configuration
like image 154
Lenin Raj Rajasekaran Avatar answered Feb 04 '26 20:02

Lenin Raj Rajasekaran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!