Sha256: 659e0e752544b1d54031dd1d09037a7b03710df2fd4f8c872e2f586ea8622f87

Contents?: true

Size: 1001 Bytes

Versions: 2

Compression:

Stored size: 1001 Bytes

Contents

# frozen_string_literal: true

require 'singleton'
require 'logger'

module MovieOrganizer
  class Logger
    include Singleton

    attr_accessor :log_provider

    def initialize(provider = default_logger)
      @log_provider = provider
    end

    def log_exception(e, data = {})
      msg = "EXCEPTION : #{e.class.name} : #{e.message}"
      msg += "\n data : #{data.inspect}" if data && !data.empty?
      msg += "\n  #{e.backtrace[0, 6].join("\n  ")}"
      log_provider.error(msg)
    end

    def method_missing(meth, *args, &block)
      if log_provider.respond_to?(meth)
        log_provider.send(meth, *args, &block)
      else
        super
      end
    end

    def respond_to?(meth, include_private = false)
      if log_provider.respond_to?(meth)
        true
      else
        super
      end
    end

    private

    def default_logger
      logger = ::Logger.new(STDOUT)
      logger.formatter = proc do |_, _, _, msg|
        "#{msg}\n"
      end
      logger
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
movie_organizer-1.0.2 lib/movie_organizer/logger.rb
movie_organizer-1.0.1 lib/movie_organizer/logger.rb