Sha256: 2102bdea4a2b1b8f8f0cec0035925ab486bcc5e675bed9ce9fab925f76ef42c3

Contents?: true

Size: 1.87 KB

Versions: 25

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true
require 'English'

module Dynflow
  module LoggerAdapters
    class Simple < Abstract
      require 'logger'

      attr_reader :logger, :action_logger, :dynflow_logger

      def initialize(output = $stdout, level = Logger::DEBUG, formatters = [Formatters::Exception])
        @logger           = Logger.new(output)
        @logger.level     = level
        @logger.formatter = method(:formatter).to_proc
        @action_logger    = apply_formatters ProgNameWrapper.new(@logger, ' action'), formatters
        @dynflow_logger   = apply_formatters ProgNameWrapper.new(@logger, 'dynflow'), formatters
      end

      def level
        @logger.level
      end

      def level=(v)
        @logger.level = v
      end

      private

      def formatter(severity, datetime, prog_name, msg)
        format "[%s #%d] %5s -- %s%s\n",
               datetime.strftime('%Y-%m-%d %H:%M:%S.%L'),
               $PID,
               severity,
               (prog_name ? prog_name + ': ' : ''),
               msg.to_s
      end

      class ProgNameWrapper
        def initialize(logger, prog_name)
          @logger    = logger
          @prog_name = prog_name
        end

        { fatal: 4, error: 3, warn: 2, info: 1, debug: 0 }.each do |method, level|
          define_method method do |message = nil, &block|
            @logger.add level, message, @prog_name, &block
          end
        end

        def respond_to_missing?(method_name, include_private = false)
          @logger.respond_to?(method_name, include_private) || super
        end

        def method_missing(name, *args, &block)
          if @logger.respond_to?(name)
            @logger.send(name, *args, &block)
          else
            super
          end
        end

        def level=(v)
          @logger.level = v
        end

        def level
          @logger.level
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
dynflow-1.8.2 lib/dynflow/logger_adapters/simple.rb
dynflow-1.8.1 lib/dynflow/logger_adapters/simple.rb
dynflow-1.8.0 lib/dynflow/logger_adapters/simple.rb
dynflow-1.7.0 lib/dynflow/logger_adapters/simple.rb
dynflow-1.6.11 lib/dynflow/logger_adapters/simple.rb
dynflow-1.6.10 lib/dynflow/logger_adapters/simple.rb
dynflow-1.6.8 lib/dynflow/logger_adapters/simple.rb
dynflow-1.6.7 lib/dynflow/logger_adapters/simple.rb
dynflow-1.6.6 lib/dynflow/logger_adapters/simple.rb
dynflow-1.6.5 lib/dynflow/logger_adapters/simple.rb
dynflow-1.6.4 lib/dynflow/logger_adapters/simple.rb
dynflow-1.6.3 lib/dynflow/logger_adapters/simple.rb
dynflow-1.6.2 lib/dynflow/logger_adapters/simple.rb
dynflow-1.6.1 lib/dynflow/logger_adapters/simple.rb
dynflow-1.4.9 lib/dynflow/logger_adapters/simple.rb
dynflow-1.4.8 lib/dynflow/logger_adapters/simple.rb
dynflow-1.5.0 lib/dynflow/logger_adapters/simple.rb
dynflow-1.4.7 lib/dynflow/logger_adapters/simple.rb
dynflow-1.4.6 lib/dynflow/logger_adapters/simple.rb
dynflow-1.4.5 lib/dynflow/logger_adapters/simple.rb