Sha256: f5f48a0da6211e692dc07d954f65e6e196269ce2e43169f5015be58dc68c05e2

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'dry-configurable'
require 'rouge'

module Dry
  module Monitor
    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.event(:sql).subscribe(:sql) do |time, id, payload|
            log_query(time, payload[:name], payload[: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

1 entries across 1 versions & 1 rubygems

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