Sha256: c5d4c3cf089fe20ea31bad23a8d79c4b48fceb68b5262f018c160c6f473488ff

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

module Legion
  module Logging
    module Builder
      def log_format(options = {}, log = @log) # rubocop:disable Metrics/AbcSize
        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 = 'info', log = @log)
        case level
        when 'trace'
          log.level = ::Logger::INFO
        when 'debug'
          log.level = ::Logger::DEBUG
        when 'info'
          log.level = ::Logger::INFO
        when 'warn'
          log.level = ::Logger::WARN
        when 'error'
          log.level = ::Logger::ERROR
        when 'fatal'
          log.level = ::Logger::FATAL
        end
        @log = log
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
legion-logging-1.1.2 lib/legion/logging/builder.rb
legion-logging-1.1.1 lib/legion/logging/builder.rb
legion-logging-1.1.0 lib/legion/logging/builder.rb