Sha256: 721dabb438383ad1e5eb2c0a6cff4b2da93ea55e2e0f55c578a679bfcd143c8c

Contents?: true

Size: 1.82 KB

Versions: 14

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

##
# Usage example:
#
#   ConvenientService::Logger.instance
#   ConvenientService::Logger.instance.level = ::Logger::WARN
#   ConvenientService::Logger.instance.warn { "log message" }
#
# Docs:
#
# - https://ruby-doc.org/stdlib-3.1.0/libdoc/logger/rdoc/Logger.html
# - https://newbedev.com/ruby-create-singleton-with-parameters
#
module ConvenientService
  ##
  # TODO: Custom matcher to track log messages.
  #
  class Logger < ::Logger
    include ::Singleton

    class << self
      private

      ##
      # NOTE: `super` is `Logger.new`.
      # https://ruby-doc.org/stdlib-3.1.0/libdoc/logger/rdoc/Logger.html#method-c-new
      #
      # rubocop:disable Style/GlobalStdStream
      def new
        super(STDOUT).tap do |logger|
          logger.level = ENV["ACTIVE_SERVICE_LOGGER_LEVEL"] || "INFO"

          logger.formatter = colored_formatter if ENV["ACTIVE_SERVICE_LOGGER_ENABLE_COLORS"] == "true"
        end
      end
      # rubocop:enable Style/GlobalStdStream

      def original_formatter
        ::Logger::Formatter.new
      end

      ##
      # https://ruby-doc.org/stdlib-2.4.0/libdoc/logger/rdoc/Logger.html#method-i-add
      #
      def colored_formatter
        return original_formatter unless defined? ::Paint

        proc do |severity, datetime, progname, message|
          log = original_formatter.call(severity, datetime, progname, message)

          case severity.to_s.downcase
          when "info" then ::Paint[log, :cyan, :bold]
          when "warn" then ::Paint[log, :yellow, :bold]
          when "error" then ::Paint[log, :red, :bold]
          when "fatal" then ::Paint[log, :red, :underline]
          when "debug" then ::Paint[log, :magenta, :bold]
          else log
          end
        end
      end
    end
  end

  class << self
    def logger
      Logger.instance
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
convenient_service-0.11.0 lib/convenient_service/logger.rb
convenient_service-0.10.1 lib/convenient_service/logger.rb
convenient_service-0.10.0 lib/convenient_service/logger.rb
convenient_service-0.9.0 lib/convenient_service/logger.rb
convenient_service-0.8.0 lib/convenient_service/logger.rb
convenient_service-0.7.0 lib/convenient_service/logger.rb
convenient_service-0.6.0 lib/convenient_service/logger.rb
convenient_service-0.5.0 lib/convenient_service/logger.rb
convenient_service-0.4.0 lib/convenient_service/logger.rb
convenient_service-0.3.1 lib/convenient_service/logger.rb
convenient_service-0.3.0 lib/convenient_service/logger.rb
convenient_service-0.2.1 lib/convenient_service/logger.rb
convenient_service-0.2.0 lib/convenient_service/logger.rb
convenient_service-0.1.0 lib/convenient_service/logger.rb