Sha256: 53cce92ab6198466cc32365a05e243711d2b863faaba5be722e62c11a612b5ff

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

module Legion
  module Logging
    module Builder
      def log_format(options = {}, log = @log) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity
        log.formatter = proc do |severity, datetime, _progname, msg|
          options[:lex_name] = options.key?(:lex) ? "[lex-#{options[:lex]}]" : nil
          unless options[:lex_name].nil?
            data = caller_locations[4].to_s.split('/').last(2)
            runner_trace = {
              type:        data[0],
              file:        data[1].split('.')[0],
              function:    data[1].split('`')[1].delete_suffix('\''),
              line_number: data[1].split(':')[1]
            }
          end
          string = "[#{datetime}][#{::Process.pid}]"
          string.concat(options[:lex_name]) unless options[:lex_name].nil?
          if runner_trace.is_a?(Hash) && (options[:extended] || severity == 'debug')
            string.concat("[#{runner_trace[:type]}:#{runner_trace[:file]}:#{runner_trace[:line_number]}]")
          end
          string.concat(" #{severity} #{msg}\n")
          string
        end
      end

      def output(options)
        @log = ::Logger.new($stdout) if options[:log_file].nil?
        @log = ::Logger.new(options[:log_file]) unless options[:log_file].nil?
      end

      def log_level(level = 'debug', log = @log) # rubocop:disable  Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
        if level == 'trace'
          log.level = ::Logger::INFO
        elsif level == 'debug'
          log.level = ::Logger::DEBUG
        elsif level == 'info'
          log.level = ::Logger::INFO
        elsif level == 'warn'
          log.level = ::Logger::WARN
        elsif level == 'error'
          log.level = ::Logger::ERROR
        elsif level == 'fatal'
          log.level = ::Logger::FATAL
        end
        @log = log
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
legion-logging-1.0.0 lib/legion/logging/builder.rb