Class: Async::Logger

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

Constant Summary collapse

LEVELS =
{debug: 0, info: 1, warn: 2, error: 3, fatal: 4}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, level: 1) ⇒ Logger

Returns a new instance of Logger.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/async/logger.rb', line 43

def initialize(output, level: 1)
	@output = output
	@level = level
	@start = Time.now
	
	@terminal = Terminal.new(output)
	@reset_style = @terminal.reset
	@prefix_style = @terminal.color(Terminal::Colors::CYAN)
	@subject_style = @terminal.color(nil, nil, Terminal::Attributes::BOLD)
	@exception_title_style = @terminal.color(Terminal::Colors::RED, nil, Terminal::Attributes::BOLD)
	@exception_line_style = @terminal.color(Terminal::Colors::RED)
	
	@subjects = {}
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



58
59
60
# File 'lib/async/logger.rb', line 58

def level
  @level
end

Instance Method Details

#disable(subject) ⇒ Object



76
77
78
# File 'lib/async/logger.rb', line 76

def disable(subject)
	@subjects[subject.class] = false
end

#enable(subject) ⇒ Object



72
73
74
# File 'lib/async/logger.rb', line 72

def enable(subject)
	@subjects[subject.class] = true
end

#enabled?(subject) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/async/logger.rb', line 68

def enabled?(subject)
	@subjects[subject.class] == true
end

#format(subject = nil, *arguments, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/async/logger.rb', line 88

def format(subject = nil, *arguments, &block)
	prefix = time_offset_prefix
	indent = " " * prefix.size
	
	if block_given?
		arguments << yield
	end
	
	if subject
		format_subject(prefix, subject)
	end
	
	arguments.each do |argument|
		format_argument(indent, argument)
	end
end

#format_argument(prefix, argument) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/async/logger.rb', line 105

def format_argument(prefix, argument)
	if argument.is_a? Exception
		format_exception(prefix, argument)
	else
		format_value(prefix, argument)
	end
end

#format_exception(indent, exception, prefix = nil, pwd: Dir.pwd) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/async/logger.rb', line 113

def format_exception(indent, exception, prefix = nil, pwd: Dir.pwd)
	@output.puts "#{indent}|  #{prefix}#{@exception_title_style}#{exception.class}#{@reset_style}: #{exception}"
	
	exception.backtrace.each_with_index do |line, index|
		path, offset, message = line.split(":")
		
		# Make the path a bit more readable
		path.gsub!(/^#{pwd}\//, "./")
		
		@output.puts "#{indent}|  #{index == 0 ? "" : " "} #{@exception_line_style}#{path}:#{offset}#{@reset_style} #{message}"
	end
	
	if exception.cause
		@output.puts "#{indent}|"
		
		format_exception(indent, exception.cause, "Caused by ", pwd: pwd)
	end
end

#format_subject(prefix, subject) ⇒ Object



132
133
134
# File 'lib/async/logger.rb', line 132

def format_subject(prefix, subject)
	@output.puts "#{@prefix_style}#{prefix}: #{@subject_style}#{subject}#{@reset_style}"
end

#format_value(indent, value) ⇒ Object



136
137
138
# File 'lib/async/logger.rb', line 136

def format_value(indent, value)
	@output.puts "#{indent}: #{value}"
end

#log(level, *args, &block) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/async/logger.rb', line 80

def log(level, *args, &block)
	unless level.is_a? Symbol
		level = LEVELS[level]
	end
	
	self.send(level, *args, &block)
end

#time_offset_prefixObject



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/async/logger.rb', line 140

def time_offset_prefix
	offset = Time.now - @start
	minutes = (offset/60).floor
	seconds = (offset - (minutes*60))
	
	if minutes > 0
		"#{minutes}m#{seconds.floor}s"
	else
		"#{seconds.round(2)}s"
	end.rjust(6)
end