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.



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

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.



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

def format
  @format
end

#ioObject (readonly)

Returns the value of attribute io.



37
38
39
# File 'lib/console/serialized/logger.rb', line 37

def io
  @io
end

#startObject (readonly)

Returns the value of attribute start.



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

def start
  @start
end

Instance Method Details

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



49
50
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
# File 'lib/console/serialized/logger.rb', line 49

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
  
  if arguments.any?
    record[:arguments] = arguments
  end
  
  if options.any?
    record[:options] = options
  end
  
  if block_given?
    if block.arity.zero?
      record[:message] = yield
    else
      buffer = StringIO.new
      yield buffer
      record[:message] = buffer.string
    end
  end
  
  @io.puts(self.dump(record))
end

#dump(record) ⇒ Object



45
46
47
# File 'lib/console/serialized/logger.rb', line 45

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

#verbose!(value = true) ⇒ Object



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

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