Sha256: ce0d75ead0c5e1194271910d2b07f50f2d3d00b3c14bcceafd754f88c0ea186c

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require 'logger'
require 'yaml'
require 'singleton'
require 'active_support/inflector'
require 'ditty/services/settings'
require 'active_support/core_ext/object/blank'

module Ditty
  module Services
    # This is the central logger for Ditty. It can be configured to log to
    # multiple endpoints through Ditty Settings. The default configuration is to
    # send logs to $stdout
    class Logger
      include Singleton

      attr_reader :loggers

      def initialize
        @loggers = []
        return if config[:loggers].blank?
        config[:loggers].each do |values|
          klass = values[:class].constantize
          opts = tr(values[:options]) || nil
          logger = klass.new(opts)
          logger.level = klass.const_get(values[:level].to_sym) if values[:level]
          @loggers << logger
        end
      end

      def method_missing(method, *args, &block)
        loggers.each { |logger| logger.send(method, *args, &block) }
      end

      def respond_to_missing?(method, _include_private = false)
        loggers.any? { |logger| logger.respond_to?(method) }
      end

      private

      def config
        default.merge Ditty::Services::Settings.values(:logger) || {}
      end

      def tr(val)
        {
          '$stdout' => $stdout,
          '$stderr' => $stderr
        }[val] || val
      end

      def default
        { loggers: [{ name: 'default', class: 'Logger' }] }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ditty-0.7.2 lib/ditty/services/logger.rb
ditty-0.7.1 lib/ditty/services/logger.rb
ditty-0.7.0 lib/ditty/services/logger.rb
ditty-0.7.0.pre.rc1 lib/ditty/services/logger.rb