class Apartment::LogSubscriber
Custom Log subscriber to include database name and schema name in sql logs
def apartment_log
def apartment_log database = color("[#{database_name}] ", ActiveSupport::LogSubscriber::MAGENTA, true) schema = current_search_path schema = color("[#{schema.tr('"', '')}] ", ActiveSupport::LogSubscriber::YELLOW, true) unless schema.nil? "#{database}#{schema}" end
def current_search_path
def current_search_path if Apartment.connection.respond_to?(:schema_search_path) Apartment.connection.schema_search_path else Apartment::Tenant.current # all others end end
def database_name
def database_name db_name = Apartment.connection.raw_connection.try(:db) # PostgreSQL, PostGIS db_name ||= Apartment.connection.raw_connection.try(:query_options)&.dig(:database) # Mysql db_name ||= Apartment.connection.current_database # Failover db_name end
def debug(progname = nil, &block)
def debug(progname = nil, &block) progname = " #{apartment_log}#{progname}" unless progname.nil? super(progname, &block) end
def sql(event)
NOTE: for some reason, if the method definition is not here, then the custom debug method is not called
def sql(event) super(event) end