Class: YamlBot::LoggingBot

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml_bot/logging_bot.rb

Defined Under Namespace

Classes: LoggingError

Constant Summary collapse

ESCAPES =
{ green: "\033[32m",
yellow: "\033[33m",
red: "\033[31m",
reset: "\033[0m" }.freeze
LOGLEVEL =
{ info: [:info, :warn, :error],
warn: [:warn, :error],
error: [:error] }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_file, level: :info, no_color: false) ⇒ LoggingBot

Returns a new instance of LoggingBot.



19
20
21
22
23
# File 'lib/yaml_bot/logging_bot.rb', line 19

def initialize(log_file, level: :info, no_color: false)
  @log_file = log_file
  @log_level = level.to_sym unless valid_log_level(level)
  @no_color = no_color
end

Instance Attribute Details

#log_fileObject

Returns the value of attribute log_file.



17
18
19
# File 'lib/yaml_bot/logging_bot.rb', line 17

def log_file
  @log_file
end

#log_levelObject

Returns the value of attribute log_level.



17
18
19
# File 'lib/yaml_bot/logging_bot.rb', line 17

def log_level
  @log_level
end

Instance Method Details

#close_logObject



45
46
47
# File 'lib/yaml_bot/logging_bot.rb', line 45

def close_log
  log_file.close
end

#error(message) ⇒ Object



35
36
37
38
# File 'lib/yaml_bot/logging_bot.rb', line 35

def error(message)
  log(message, :error)
  emit(message: message, color: :red)
end

#info(message) ⇒ Object



25
26
27
28
# File 'lib/yaml_bot/logging_bot.rb', line 25

def info(message)
  log(message, :info)
  emit(message: message, color: :green)
end

#log(message, level) ⇒ Object



40
41
42
43
# File 'lib/yaml_bot/logging_bot.rb', line 40

def log(message, level)
  message = level.to_s.upcase + ': ' + message + "\n"
  log_file.write(message) if LOGLEVEL[log_level].include?(level)
end

#warn(message) ⇒ Object



30
31
32
33
# File 'lib/yaml_bot/logging_bot.rb', line 30

def warn(message)
  log(message, :warn)
  emit(message: message, color: :yellow)
end