2

Recently, I find out tagged logger. It is a very useful option and I config in production environment.

# config/environments/production.rb

...

config.log_tags = [ :uuid,:remote_ip ]

...

# log/production.log

[4d23e817-eca8-4db1-ba5b-7456d3af7f65] [127.0.0.1] Started GET "/resources/id" for 127.0.0.1 at 2015-11-26 21:09:11 +0900

I also want to print current login user name to log file, so I tried and found a link How to log user_name in Rails?.

But It's not working for me.

I'm using devise and rails4. How do I print?

Thanks in advance.

Community
  • 1
  • 1
chris
  • 83
  • 1
  • 7

1 Answers1

0

You can use a different approach. I've used the following one:

In application_controller.rb:

before_action: print_current_user

def print_current_user
   if user_signed_in?
      Rails.logger.debug "UserId: #{current_user.id}"    
   end
end 

Or, you can use the lograge gem.

Emu
  • 5,763
  • 3
  • 31
  • 51
  • 2
    Thank you for your answer. However this is not what I want. I want to prepend current login username to all log like this `[4d23e817-eca8-4db1-ba5b-7456d3af7f65] [127.0.0.1] [chris] Started GET ...` – chris Nov 27 '15 at 07:31