Sha256: c500eda5d306ee75914508de71907f51a2ad521783cd9153657939f7f4f01aa2

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true
require 'logger'
require 'colorized_string'
require 'kubernetes-deploy/deferred_summary_logging'

module KubernetesDeploy
  class FormattedLogger < Logger
    include DeferredSummaryLogging

    def self.indent_four(str)
      "    " + str.to_s.gsub("\n", "\n    ")
    end

    def self.build(namespace = nil, context = nil, stream = $stderr, verbose_prefix: false)
      l = new(stream)
      l.level = level_from_env

      middle = if verbose_prefix
        if namespace.blank?
          raise ArgumentError, 'Must pass a namespace if logging verbosely'
        end
        if context.blank?
          raise ArgumentError, 'Must pass a context if logging verbosely'
        end

        "[#{context}][#{namespace}]"
      end

      l.formatter = proc do |severity, datetime, _progname, msg|
        colorized_line = ColorizedString.new("[#{severity}][#{datetime}]#{middle}\t#{msg}\n")

        case severity
        when "FATAL"
          ColorizedString.new("[#{severity}][#{datetime}]#{middle}\t").red + "#{msg}\n"
        when "ERROR"
          colorized_line.red
        when "WARN"
          colorized_line.yellow
        else
          colorized_line
        end
      end
      l
    end

    def self.level_from_env
      return ::Logger::DEBUG if ENV["DEBUG"]

      if ENV["LEVEL"]
        ::Logger.const_get(ENV["LEVEL"].upcase)
      else
        ::Logger::INFO
      end
    end
    private_class_method :level_from_env
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
kubernetes-deploy-0.30.0 lib/kubernetes-deploy/formatted_logger.rb
kubernetes-deploy-0.29.0 lib/kubernetes-deploy/formatted_logger.rb
kubernetes-deploy-0.28.0 lib/kubernetes-deploy/formatted_logger.rb
kubernetes-deploy-0.27.0 lib/kubernetes-deploy/formatted_logger.rb