Class: Backup::Logger

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

Class Method Summary collapse

Class Method Details

.colorize(string, code) ⇒ Object

Wraps the provided string in colorizing tags to provide easier to view output to the client



81
82
83
# File 'lib/backup/logger.rb', line 81

def self.colorize(string, code)
  "\e[#{code}m#{string}\e[0m"
end

.error(string) ⇒ Object

Outputs an error to the console and writes it to the backup.log



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

def self.error(string)
  puts    loggify(:error, string, :red)
  to_file loggify(:error, string)
end

.green(string) ⇒ Object

Invokes the #colorize method with the provided string and the color code “32” (for green)



60
61
62
# File 'lib/backup/logger.rb', line 60

def self.green(string)
  colorize(string, 32)
end

.loggify(type, string, color = false) ⇒ Object

Builds the string in a log format with the date/time, the type (colorized) based on whether it’s a message, notice or error, and the message itself. ANSI color codes are only used in the console, and are not written to the log since it doesn’t do anything and just adds more unnecessary bloat to the log file



44
45
46
47
# File 'lib/backup/logger.rb', line 44

def self.loggify(type, string, color = false)
  return "[#{time}][#{type}] #{string}" unless color
  "[#{time}][#{send(color, type)}] #{string}"
end

.message(string) ⇒ Object

Outputs a messages to the console and writes it to the backup.log



8
9
10
11
# File 'lib/backup/logger.rb', line 8

def self.message(string)
  puts    loggify(:message, string, :green)
  to_file loggify(:message, string)
end

.red(string) ⇒ Object

Invokes the #colorize method the with provided string and the color code “31” (for red)



74
75
76
# File 'lib/backup/logger.rb', line 74

def self.red(string)
  colorize(string, 31)
end

.silent(string) ⇒ Object

Silently logs data to the log file



29
30
31
# File 'lib/backup/logger.rb', line 29

def self.silent(string)
  to_file loggify(:silent, string)
end

.timeObject

Returns the time in [YYYY/MM/DD HH:MM:SS] format



35
36
37
# File 'lib/backup/logger.rb', line 35

def self.time
  Time.now.strftime("%Y/%m/%d %H:%M:%S")
end

.to_file(string) ⇒ Object

Writes (appends) a string to the backup.log file



51
52
53
54
55
# File 'lib/backup/logger.rb', line 51

def self.to_file(string)
  File.open(File.join(LOG_PATH, 'backup.log'), 'a') do |file|
    file.write("#{string}\n")
  end
end

.warn(string) ⇒ Object

Outputs a notice to the console and writes it to the backup.log



22
23
24
25
# File 'lib/backup/logger.rb', line 22

def self.warn(string)
  puts    loggify(:warning, string, :yellow)
  to_file loggify(:warning, string)
end

.yellow(string) ⇒ Object

Invokes the #colorize method with the provided string and the color code “33” (for yellow)



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

def self.yellow(string)
  colorize(string, 33)
end