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: Format.default, verbose: false, **options) ⇒ Logger

Returns a new instance of Logger.



17
18
19
20
21
22
# File 'lib/console/serialized/logger.rb', line 17

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

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



26
27
28
# File 'lib/console/serialized/logger.rb', line 26

def format
  @format
end

#ioObject (readonly)

Returns the value of attribute io.



24
25
26
# File 'lib/console/serialized/logger.rb', line 24

def io
  @io
end

#startObject (readonly)

Returns the value of attribute start.



25
26
27
# File 'lib/console/serialized/logger.rb', line 25

def start
  @start
end

Instance Method Details

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



36
37
38
39
40
41
42
43
44
45
46
47
48
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
82
83
84
85
86
# File 'lib/console/serialized/logger.rb', line 36

def call(subject = nil, *arguments, severity: UNKNOWN, **options, &block)
	record = {
		time: Time.now.iso8601,
		severity: severity,
		oid: subject.object_id,
		pid: Process.pid,
	}
	
	# We want to log just a brief subject:
	if subject.is_a?(String)
		record[:subject] = subject
	elsif subject.is_a?(Module)
		record[:subject] = subject.name
	else
		record[:subject] = subject.class.name
	end
	
	if annotation = Fiber.current.annotation
		record[:annotation] = annotation
	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



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

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

#verbose!(value = true) ⇒ Object



28
29
30
# File 'lib/console/serialized/logger.rb', line 28

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