Class: Console::Serialized::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/console/serialized/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = $stderr, format: JSON, verbose: false, **options) ⇒ Logger

Returns a new instance of Logger.



32
33
34
35
36
37
# File 'lib/console/serialized/logger.rb', line 32

def initialize(io = $stderr, format: JSON, verbose: false, **options)
  @io = io
  @start = Time.now
  @format = format
  @verbose = verbose
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



41
42
43
# File 'lib/console/serialized/logger.rb', line 41

def format
  @format
end

#ioObject (readonly)

Returns the value of attribute io.



39
40
41
# File 'lib/console/serialized/logger.rb', line 39

def io
  @io
end

#startObject (readonly)

Returns the value of attribute start.



40
41
42
# File 'lib/console/serialized/logger.rb', line 40

def start
  @start
end

Instance Method Details

#call(subject = nil, *arguments, severity: UNKNOWN, **options, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/console/serialized/logger.rb', line 51

def call(subject = nil, *arguments, severity: UNKNOWN, **options, &block)
  record = {
    time: Time.now.iso8601,
    severity: severity,
    class: subject.class,
    oid: subject.object_id,
    pid: Process.pid,
  }
  
  if subject
    record[:subject] = subject
  end
  
  message = arguments
  
  if block_given?
    if block.arity.zero?
      message << yield
    else
      buffer = StringIO.new
      yield buffer
      message << buffer.string
    end
  end
  
  if message.size == 1
    record[:message] = message.first
  elsif message.any?
    record[:message] = message
  end
  
  if exception = find_exception(message)
    record[:error] = {
      kind: exception.class,
      message: exception.message,
      stack: format_stack(exception)
    }
  end
  
  record.update(options)
  
  @io.puts(self.dump(record))
end

#dump(record) ⇒ Object



47
48
49
# File 'lib/console/serialized/logger.rb', line 47

def dump(record)
  @format.dump(record)
end

#verbose!(value = true) ⇒ Object



43
44
45
# File 'lib/console/serialized/logger.rb', line 43

def verbose!(value = true)
  @verbose = true
end