How to get user's IP

Hi Pawel,

Pawel Stawicki wrote:

I want to log users who enter my site and what they are doing.
Registration and login is not required, so what is left is IP.
(something like wikipedia, you can edit without logging in, and then
there is your IP in log). How to get this IP in Ruby on Rails?

And if you know some nice tutorial about logging in ruby I would also
appreciate :wink: I mean logging events to some file, not logging users to
service :slight_smile:

Rails is probably already logging everything you're looking for in #{RAILS_ROOT}/log/production.log. Every request/reponse cycle generates an entry containing the time of the request, the requesting IP address, the session id, controller / action requested, parameters passed, and response. You might want to consider using the session id rather than IP address to track visitors. If the visitor is using AOL, for example, the IP address you see can change during their visit. I haven't tested it, but I think Rails will maintain the same session ID throughout.

If you're in development mode, your development.log will contain more info than your production.log will when you switch to that environment. If you want to see what you'll be getting when you move to production, just uncomment the line (around line 27) in #{RAILS_ROOT}/config/environment.rb that reads "#config.log_level = :debug" and change it to "config.log_level = :info".

Hope that helps,

Youโ€™ll be better of writing a log analyzer (and visualizer) than a method call and another hit on your db on each request. The logs arenโ€™t that difficult to parse and you can parse the whole lot during the night and generate staticly cached pages.

Best regards

Peter De Berdt