Sha256: 588f30aa9eee276632b568661b0975619ba7800d1de9e10f52c2faff7998e060

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

require 'dry-configurable'
require 'rouge'
require 'dry/monitor/notifications'

module Dry
  module Monitor
    Notifications.register_event(:sql)

    module SQL
      class Logger
        extend Dry::Configurable

        setting :theme, Rouge::Themes::Gruvbox.new
        setting :colorize, true
        setting :message_template, %(  Loaded %s in %sms %s).freeze

        attr_reader :config
        attr_reader :logger
        attr_reader :formatter
        attr_reader :lexer
        attr_reader :template

        def initialize(logger, config = self.class.config)
          @logger = logger
          @config = config
          @formatter = Rouge::Formatters::Terminal256.new(config.theme)
          @lexer = Rouge::Lexers::SQL.new
          @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, colorize(query)]
        end

        private

        def colorize(string)
          config.colorize ? formatter.format(lexer.lex(string)) : string
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-monitor-0.1.2 lib/dry/monitor/sql/logger.rb
dry-monitor-0.1.1 lib/dry/monitor/sql/logger.rb
dry-monitor-0.1.0 lib/dry/monitor/sql/logger.rb