Class: U::Log::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/u-log.rb

Overview

A very simple logger and log context

Constant Summary collapse

NL =
"\n".freeze

Instance Method Summary collapse

Constructor Details

#initialize(out, format, data) ⇒ Logger

out is the log destination. Has a #<< method that takes a string. format is what transforms the data into a string using the #dump method data is context data for the logger. Responds to #to_h.



36
37
38
39
40
# File 'lib/u-log.rb', line 36

def initialize(out, format, data)
  @out = out
  @format = format
  @data = data.to_h
end

Instance Method Details

#compatObject

Returns a ::Logger-compatible object.

Make sure to require ‘u-log/compat’ before invoking this method.



66
# File 'lib/u-log.rb', line 66

def compat; Compat.new(self) end

#context(data = {}) ⇒ Object Also known as: merge

Creates a derivative context so that ‘context(a: 1).context(b: 2)` is equivalent to `contect(a: 1, b: 2)`



49
50
51
52
# File 'lib/u-log.rb', line 49

def context(data = {})
  return self unless data.to_h.any?
  with_data @data.merge(data.to_h)
end

#log(*args) ⇒ Object

Outputs the given arguments merged with the context.



43
44
45
# File 'lib/u-log.rb', line 43

def log(*args)
  @out << with_data(args_to_hash args).to_s + NL
end

#to_hObject



60
# File 'lib/u-log.rb', line 60

def to_h; @data; end

#to_sObject



56
57
58
# File 'lib/u-log.rb', line 56

def to_s
  @format.dump(evaluate_procs @data)
end