公寓 gem - 将当前数据库添加到所有服务器日志
Apartment gem - Adding current database to all server logs
我正在使用公寓 gem 来切换用于多租户 Rails 应用程序的租户(数据库)。
在我的服务器日志中,我想输出当前租户(数据库)用于日志文件中的每一行。
当我执行 rails s
时,服务器实际上从未以初始化程序目录中的以下代码启动。服务器刚刚挂起......太奇怪了。没有错误消息,也没有 运行 服务器。如果我在下面取出 #{Apartment::Tenant.current}
一切都很好......但是......我真的想知道我的日志文件中的当前租户(数据库)。
/initializers/log_formatting.rb:
class ActiveSupport::Logger::SimpleFormatter
def call(severity, time, progname, msg)
"#{Apartment::Tenant.current} #{msg.strip} (pid:#{$$})\n"
end
end
关于如何将正在使用的当前租户(数据库)输出到我的日志文件的每一行有什么想法吗?
谢谢!
我建议您使用 log_tags
。
来自 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.
您可以根据需要在 application.rb 或 production.rb 中添加此配置。
例如:config.log_tags = [ :subdomain, :request_id, lambda { |request| request.headers["tenant_name"] } ]
注意:如果您为开发环境添加此内容并且您在 your_subdomain.localhost:3000
上 运行,则子域将不会出现,因为本地主机不支持子域。有一些解决方法,例如修改 /etc/hosts 文件,但我不推荐它。更清洁的解决方案是使用 your_subdomain.lvh.me:3000
我正在使用公寓 gem 来切换用于多租户 Rails 应用程序的租户(数据库)。
在我的服务器日志中,我想输出当前租户(数据库)用于日志文件中的每一行。
当我执行 rails s
时,服务器实际上从未以初始化程序目录中的以下代码启动。服务器刚刚挂起......太奇怪了。没有错误消息,也没有 运行 服务器。如果我在下面取出 #{Apartment::Tenant.current}
一切都很好......但是......我真的想知道我的日志文件中的当前租户(数据库)。
/initializers/log_formatting.rb:
class ActiveSupport::Logger::SimpleFormatter
def call(severity, time, progname, msg)
"#{Apartment::Tenant.current} #{msg.strip} (pid:#{$$})\n"
end
end
关于如何将正在使用的当前租户(数据库)输出到我的日志文件的每一行有什么想法吗?
谢谢!
我建议您使用 log_tags
。
来自 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.
您可以根据需要在 application.rb 或 production.rb 中添加此配置。
例如:config.log_tags = [ :subdomain, :request_id, lambda { |request| request.headers["tenant_name"] } ]
注意:如果您为开发环境添加此内容并且您在 your_subdomain.localhost:3000
上 运行,则子域将不会出现,因为本地主机不支持子域。有一些解决方法,例如修改 /etc/hosts 文件,但我不推荐它。更清洁的解决方案是使用 your_subdomain.lvh.me:3000