Class: Rakali::Logger

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = INFO) ⇒ Logger

Public: Create a new logger instance

level - (optional, integer) the log level

Returns nothing



18
19
20
# File 'lib/rakali/logger.rb', line 18

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

Instance Attribute Details

#log_levelObject

Returns the value of attribute log_level.



6
7
8
# File 'lib/rakali/logger.rb', line 6

def log_level
  @log_level
end

Instance Method Details

#abort_with(topic, message = nil) ⇒ Object

Public: Print a 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



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

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

#debug(topic, message = nil) ⇒ Object

Public: Print a debug message to stdout

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

Returns nothing



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

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

#error(topic, message = nil) ⇒ Object

Public: Print a error message to stderr

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

Returns nothing



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

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



88
89
90
# File 'lib/rakali/logger.rb', line 88

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

#info(topic, message = nil) ⇒ Object

Public: Print a message to stdout

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

Returns nothing



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

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

#message(topic, message) ⇒ Object

Public: Build a topic method

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

Returns the formatted message



79
80
81
# File 'lib/rakali/logger.rb', line 79

def message(topic, message)
  formatted_topic(topic) + message.to_s
end

#warn(topic, message = nil) ⇒ Object

Public: Print a message to stderr

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

Returns nothing



48
49
50
# File 'lib/rakali/logger.rb', line 48

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