Sha256: b80328e2af69c37cd17e8a01ad17eb90f95b412ea65e8776fc6b5ab730ba6794

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

require 'logger'

module Telemetry
  module Logger
    module Builder
      def opts
        @opts ||= {}
      end

      def format(include_pid: false, **)
        log.formatter = proc do |severity, datetime, _progname, msg|
          string = "[#{datetime}]"
          string.concat("[#{::Process.pid}]") if include_pid
          string.concat(" #{severity} #{msg}\n")
          string
        end
      end

      def log
        @log ||= output(**opts)
      end

      def output(**options)
        return @log unless @log.nil?

        @log = ::Logger.new(options[:log_file] || $stdout)
        self.log_level = options[:level] if options.key? :level
        self.format

        @log
      end

      def level
        log.level
      end

      def log_level=(level)
        log.level = case level
                    when 'trace', 'debug'
                      ::Logger::DEBUG
                    when 'info'
                      ::Logger::INFO
                    when 'warn'
                      ::Logger::WARN
                    when 'error'
                      ::Logger::ERROR
                    when 'fatal'
                      ::Logger::FATAL
                    when nil
                      42
                    else
                      if level.is_a? Integer
                        level
                      else
                        0
                      end
                    end
        @log = log
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
telemetry-logger-0.2.0 lib/telemetry/logger/builder.rb
telemetry-logger-0.1.1 lib/telemetry/logger/builder.rb
telemetry-logger-0.1.0 lib/telemetry/logger/builder.rb