Class: Hyla::Logger

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

Constant Summary collapse

DEBUG =
0
INFO =
1
WARN =
2
ERROR =
3
FATAL =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = INFO) ⇒ Logger

Public: Create a new instance of Hyla’s logger

level - (optional, integer) the log level

Returns nothing



16
17
18
# File 'lib/hyla/logger.rb', line 16

def initialize(level = INFO)
  @log_level = level
end

Instance Attribute Details

#log_levelObject

Returns the value of attribute log_level.



3
4
5
# File 'lib/hyla/logger.rb', line 3

def log_level
  @log_level
end

Instance Method Details

#abort_with(topic, message = nil) ⇒ Object

Public: Print a hyla error message to stderr and immediately abort the process

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail (can be omitted)

Returns nothing



66
67
68
69
# File 'lib/hyla/logger.rb', line 66

def abort_with(topic, message = nil)
  error(topic, message)
  abort
end

#debug(topic, message = nil) ⇒ Object

Public: Print a Hyla debug message to stdout

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail

Returns nothing



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

def debug(topic, message = nil)
  $stdout.puts(message(topic, message)) if log_level <= DEBUG
end

#error(topic, message = nil) ⇒ Object

Public: Print a hyla error message to stderr

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail

Returns nothing



56
57
58
# File 'lib/hyla/logger.rb', line 56

def error(topic, message = nil)
  $stderr.puts(message(topic, message).red) if log_level <= ERROR
end

#formatted_topic(topic) ⇒ Object

Public: Format the topic

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc.

Returns the formatted topic statement



86
87
88
# File 'lib/hyla/logger.rb', line 86

def formatted_topic(topic)
  "#{topic} ".rjust(20)
end

#info(topic, message = nil) ⇒ Object

Public: Print a hyla message to stdout

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail

Returns nothing



36
37
38
# File 'lib/hyla/logger.rb', line 36

def info(topic, message = nil)
  $stdout.puts(message(topic, message)) if log_level <= INFO
end

#message(topic, message) ⇒ Object

Public: Build a hyla topic method

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail

Returns the formatted message



77
78
79
# File 'lib/hyla/logger.rb', line 77

def message(topic, message)
  formatted_topic(topic) + message.to_s.gsub(/\s+/, ' ')
end

#warn(topic, message = nil) ⇒ Object

Public: Print a hyla message to stderr

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail

Returns nothing



46
47
48
# File 'lib/hyla/logger.rb', line 46

def warn(topic, message = nil)
  $stderr.puts(message(topic, message).yellow) if log_level <= WARN
end