Sha256: 8f591307ee3c2f26de2625f5e13620be292c663b1d1eb584fb35f6de4f6f7a9f

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

require "dry-configurable"
require "dry/core/extensions"
require "dry/monitor/notifications"

module Dry
  module Monitor
    Notifications.register_event(:sql)

    module SQL
      class Logger
        extend Dry::Core::Extensions
        extend Dry::Configurable

        register_extension(:default_colorizer) do
          require_relative "./colorizers/default"

          module DefaultColorizer
            def colorizer
              @colorizer ||= Colorizers::Default.new(config.theme)
            end
          end

          Logger.include(DefaultColorizer)
        end

        register_extension(:rouge_colorizer) do
          require_relative "./colorizers/rouge"

          module RougeColorizer
            def colorizer
              @colorizer ||= Colorizers::Rouge.new(config.theme)
            end
          end

          Logger.include(RougeColorizer)
        end

        setting :theme, nil
        setting :message_template, %(  Loaded %s in %sms %s)

        attr_reader :config
        attr_reader :logger
        attr_reader :template

        load_extensions(:default_colorizer)

        def initialize(logger, config = self.class.config)
          @logger = logger
          @config = config
          @template = config.message_template
        end

        def subscribe(notifications)
          notifications.subscribe(:sql) do |time:, name:, query:|
            log_query(time, name, query)
          end
        end

        def log_query(time, name, query)
          logger.info template % [name.inspect, time, colorizer.call(query)]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-monitor-0.3.2 lib/dry/monitor/sql/logger.rb