lib/hedgelog.rb in hedgelog-0.1.9 vs lib/hedgelog.rb in hedgelog-0.1.11

- old
+ new

@@ -1,27 +1,28 @@ # frozen_string_literal: true + require 'hedgelog/version' require 'hedgelog/context' require 'hedgelog/scrubber' require 'hedgelog/normalizer' require 'logger' require 'yajl' class Hedgelog - LEVELS = %w(DEBUG INFO WARN ERROR FATAL UNKNOWN).each_with_object({}).with_index do |(v, h), i| + LEVELS = %w[DEBUG INFO WARN ERROR FATAL UNKNOWN].each_with_object({}).with_index do |(v, h), i| h[v] = i h[v.downcase] = i h[v.to_sym] = i h[v.downcase.to_sym] = i h[i] = v.downcase.to_sym end.freeze - TOP_LEVEL_KEYS = [:app, :channel, :level, :level_name, :message, :request_id, :timestamp].freeze - RESERVED_KEYS = [:app, :level, :level_name, :timestamp, :context, :caller].freeze + TOP_LEVEL_KEYS = %i[app channel level level_name message request_id timestamp].freeze + RESERVED_KEYS = %i[app level level_name timestamp context caller].freeze - TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S.%6N%z'.freeze - BACKTRACE_RE = /([^:]+):([0-9]+)(?::in `(.*)')?/ + TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S.%6N%z' + BACKTRACE_RE = Regexp.new("([^:]+):([0-9]+)(?::in `(.*)')?") attr_reader :level attr_writer :app def initialize(logdev = STDOUT, shift_age = nil, shift_size = nil) @@ -41,10 +42,11 @@ end def level=(level) int_level = level_to_int(level) raise ::ArgumentError, "#{self.class}#level= , #{level} is not a valid level." if int_level.nil? + @level = int_level end def add(severity = LEVELS[:unknown], message = nil, progname = nil, context = {}, &block) return true if (@logdev.nil? && @channel.nil?) || severity < @level @@ -56,11 +58,11 @@ context.merge!(@channel_context) context[:message] ||= message return write(severity, context) if @logdev - @channel.add(severity, nil, progname, context) if @channel + @channel&.add(severity, nil, progname, context) end def []=(key, val) @channel_context[key] = val end @@ -84,11 +86,11 @@ channel_name = "#{self[:channel]} => #{name}" if self[:channel] sc[:channel] = channel_name sc end - %w(fatal error warn info debug unknown).each do |level| + %w[fatal error warn info debug unknown].each do |level| predicate = "#{level}?".to_sym level = level.to_sym define_method(level) do |message = nil, context = {}, &block| raise ::ArgumentError, "#{self.class}##{level} requires at least 1 argument or a block" if !message && !block @@ -116,60 +118,61 @@ def formatter ::Logger::Formatter.new end - def formatter=(_) + def formatter=(_value) formatter end - private - - def level_to_int(level) + private def level_to_int(level) return level if level.is_a?(Integer) + LEVELS[level] end - def level_from_int(level) + private def level_from_int(level) return LEVELS[level] if level.is_a?(Integer) + level.to_sym end - def write(severity, context) + private def write(severity, context) return true if @logdev.nil? context.normalize! context.scrub! data = context.merge(default_data(severity)) data[:app] = @app if @app - data[:caller] = debugharder(caller[3]) if debug? + data[:caller] = debugharder(caller(4, 1).first) if debug? data = extract_top_level_keys(data) @logdev.write(Yajl::Encoder.encode(data) + "\n") true end - def default_data(severity) + private def default_data(severity) { timestamp: Time.now.strftime(TIMESTAMP_FORMAT), level_name: level_from_int(severity), level: severity } end - def extract_top_level_keys(context) + private def extract_top_level_keys(context) data = {} TOP_LEVEL_KEYS.each do |key| data[key] = context.delete(key) if context.key? key end data[:context] = context data end - def debugharder(callinfo) + private def debugharder(callinfo) m = BACKTRACE_RE.match(callinfo) return unless m + path, line, method = m[1..3] whence = $LOAD_PATH.find { |p| path.start_with?(p) } file = if whence # Remove the RUBYLIB path portion of the full file name path[whence.length + 1..-1]