module ActiveRecordSharding
  module LogSubscriber
    extend ActiveSupport::Concern

    included do
      alias_method_chain :sql, :shard
      MAGENTA = "\e[35m"
      CYAN    = "\e[36m"
    end

    def sql_with_shard(event)
      self.class.runtime += event.duration
      return unless logger.debug?

      payload = event.payload

      return if ActiveRecord::LogSubscriber::IGNORE_PAYLOAD_NAMES.include?(payload[:name])

      name  = "#{payload[:name]} (#{event.duration.round(1)}ms)"
      sql   = payload[:sql]
      binds = nil
      database = payload[:database]

      unless (payload[:binds] || []).empty?
        binds = "  " + payload[:binds].map { |col,v|
          render_bind(col, v)
        }.inspect
      end

      if odd?
        name = color(name, CYAN, true)
        database = color(database, CYAN, true)
        sql  = color(sql, nil, true)
      else
        name = color(name, MAGENTA, true)
        database = color(database, MAGENTA, true)
      end

      debug "  #{name} #{database}  #{sql}#{binds}"
    end
  end
end

module ActiveRecordSharding
  module LogSubscriber
    extend ActiveSupport::Concern

    included do
      class << self
      end
    end

    def sql(event)
      self.class.runtime += event.duration
      return unless logger.debug?

      payload = event.payload

      return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])

      name  = "#{payload[:name]} (#{event.duration.round(1)}ms)"
      sql   = payload[:sql]
      binds = nil
      database = payload[:database]

      unless (payload[:binds] || []).empty?
        binds = "  " + payload[:binds].map { |col,v|
          render_bind(col, v)
        }.inspect
      end

      if odd?
        name = color(name, CYAN, true)
        database = color(database, CYAN, true)
        sql  = color(sql, nil, true)
      else
        name = color(name, MAGENTA, true)
        database = color(database, MAGENTA, true)
      end

      debug "  #{name} #{database}  #{sql}#{binds}"
    end

    module ClassMethods

    end
  end
end

--- 
+++ 
@@ -3,17 +3,18 @@
     extend ActiveSupport::Concern
 
     included do
-      class << self
-      end
+      alias_method_chain :sql, :shard
+      MAGENTA = "\e[35m"
+      CYAN    = "\e[36m"
     end
 
-    def sql(event)
+    def sql_with_shard(event)
       self.class.runtime += event.duration
       return unless logger.debug?
 
       payload = event.payload
 
-      return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
+      return if ActiveRecord::LogSubscriber::IGNORE_PAYLOAD_NAMES.include?(payload[:name])
 
       name  = "#{payload[:name]} (#{event.duration.round(1)}ms)"
       sql   = payload[:sql]
@@ -37,9 +38,5 @@
 
       debug "  #{name} #{database}  #{sql}#{binds}"
     end
-
-    module ClassMethods
-
-    end
   end
 end
Fix show connection name in log
