Sha256: fc0c52d800a606c44633387366aa8634ba2926144ddb0074b68606be68214e81

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

require 'logger'
require 'colorize'
require 'json'

module UU
  class LoggerStderr < Logger
    class Formatter
      def initialize(context)
        @context = context
      end

      attr_reader :context

      def call(severity, time, _progname, msg)
        loc = meaningful_location
        short_severity = severity[0, 1]
        time_format = time.utc.strftime('%T')
        context = @context.context
        context_msg = context.empty? ? '' : "#{context.to_json} "

        "#{short_severity}]#{time_format}" \
          "[#{loc[:filename]}##{loc[:method]}:#{loc[:lineno]}] " \
          "#{context_msg}" \
          "#{msg}\n"
      end

      def meaningful_location
        location = find_location
        {
          filename: File.basename(location.path),
          method: location.label,
          lineno: location.lineno,
        }
      end

      PATHS = %w[
        /log.rb
        /logger.rb
        /loggable.rb
        /forwardable.rb
      ].freeze

      def find_location
        caller_locations.find do |location_|
          location_.path != __FILE__ &&
            PATHS.none? { |path| location_.path.end_with?(path) }
        end
      end
    end

    class FormatterColor < Formatter
      DECORATION = {
        'UNKNOWN' => { color: :red, mode: :underline },
        'FATAL' => { color: :red, mode: :underline },
        'ERROR' => :red,
        'WARN' => :yellow,
        'INFO' => :default,
        'DEBUG' => :light_black,
      }.freeze

      def call(severity, time, _progname, msg)
        super.colorize(DECORATION[severity])
      end
    end

    def initialize(context)
      formatter = $stderr.tty? ? FormatterColor : Formatter
      super($stderr, formatter: formatter.new(context))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
uu-0.2.2 lib/uu/logger_stderr.rb