0

I am using the Apartment gem to switch the tenant (database) being used for a multi tenancy Rails application.

In my server logs I would like to output the current tenant (database) being used for every single line in the log file.

When I do rails s the server never actually starts with the code below that is in the initializers directory. The server just hangs... so odd. No error message and no running server. If I take out #{Apartment::Tenant.current} below everything is fine... but... I really want to know the current tenant (database) in my log files.

/initializers/log_formatting.rb:

class ActiveSupport::Logger::SimpleFormatter 
  def call(severity, time, progname, msg)
    "#{Apartment::Tenant.current} #{msg.strip} (pid:#{$$})\n"
  end
end

Any ideas on how to get the current tenant (database) being used output to every line of my log file?

Thank you!

nourza
  • 2,215
  • 2
  • 16
  • 42
slindsey3000
  • 4,053
  • 5
  • 36
  • 56

1 Answers1

2

I would suggest you to use log_tags. From the rails documentation :

config.log_tags accepts a list of: methods that the request object responds to, a Proc that accepts the request object, or something that responds to to_s. This makes it easy to tag log lines with debug information like subdomain and request id - both very helpful in debugging multi-user production applications.

You can add this configuration in application.rb or production.rb whichever fits your need.

For ex: config.log_tags = [ :subdomain, :request_id, lambda { |request| request.headers["tenant_name"] } ]

Note: In case you are adding this for development environment and you are running on your_subdomain.localhost:3000 then subdomain won't be present as localhost doesn't support subdomains. There are some workarounds like modifying /etc/hosts file but i won't recommend it. The more cleaner solution is to use your_subdomain.lvh.me:3000

Akshay Goyal
  • 895
  • 5
  • 13
  • subdomain doesnt appear to do anything... but it is really what I want... would solve my problem but there is no output for it. – slindsey3000 Oct 15 '18 at 18:17
  • Did you restart your server? I checked it in my local and its working fine for me. If its isn't working for you then you can always customise it using lambda as given in example. – Akshay Goyal Oct 15 '18 at 18:19
  • yes. restarted server. Added your exact code above. Not getting the subdomain though. – slindsey3000 Oct 15 '18 at 18:21
  • [c2375bdf-efcb-4eb6-b064-1856b1c38f67] ConversationsPerson Load (1.4ms) SELECT `conversations_people`.* FROM `conversations_people` WHERE `conversations_people`.`person_id` = 24 [c2375bdf-efcb-4eb6-b064-1856b1c38f67] Person Load (2.4ms) SELECT `people`.* FROM `people` [607c0a29-a913-4e15-b9ae-fccf2a29b304] Started GET "/conversations/show_for_cust_service.json" for ::1 at 2018-10-15 14:18:13 -0400 [607c0a29-a913-4e15-b9ae-fccf2a29b304] (0.2ms) use `iApp1_development` [607c0a29-a913-4e15-b9ae-fccf2a29b304] (0.1ms) use `aba` – slindsey3000 Oct 15 '18 at 18:22
  • looks like request_id is in there... but no subdomain – slindsey3000 Oct 15 '18 at 18:23
  • This is strange. Anyways you can use config.log_tags = [ :request_id, lambda { |request| request.subdomain } ]. This should work. – Akshay Goyal Oct 15 '18 at 18:25
  • With your new code I am still not seeing subdomain. added it to development.rb and restarted server – slindsey3000 Oct 15 '18 at 18:31
  • [ffeb390d-c338-46af-8437-2f6d2ff0eed8] (0.6ms) SELECT `report_results`.`data` FROM `report_results` WHERE `report_results`.`report_run_id` = 2 [ffeb390d-c338-46af-8437-2f6d2ff0eed8] (0.9ms) SELECT `report_results`.`data` FROM `report_results` WHERE `report_results`.`report_run_id` = 4 [ffeb390d-c338-46af-8437-2f6d2ff0eed8] Completed 200 OK in 580ms (Views: 116.9ms | ActiveRecord: 77.0ms) [ffeb390d-c338-46af-8437-2f6d2ff0eed8] (0.3ms) use `iApp1_development` – slindsey3000 Oct 15 '18 at 18:31
  • I definitely have a subdomain too. I am hitting this url: http://aba.localhost:3000/dashboards/ – slindsey3000 Oct 15 '18 at 18:33
  • Also, I don't see :subdomain in https://api.rubyonrails.org/classes/ActionDispatch/Request.html which I believe (reading more, Google more) ... I am trying :original_fullpath which I do in in ActionDispatch/Request – slindsey3000 Oct 15 '18 at 18:37
  • 1
    Okay now i see. You cannot add subdomains on localhost. You need to use aba.lvh.me:3000/dashboards – Akshay Goyal Oct 15 '18 at 18:38
  • Yeah so using [:original_url] does show the subdomain locally. But I wish I could JUST show the subdomain... :original_url is too much info really – slindsey3000 Oct 15 '18 at 18:41
  • 1
    Kindly mark this answer as accepted if it solves the question. This way it will help anyone who get stuck at same point. Thanks :) – Akshay Goyal Oct 17 '18 at 09:27