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



89
90
91
# File 'lib/backup/logger.rb', line 89

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) unless quiet?
  to_file loggify(:error, string)
end

.green(string) ⇒ Object

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



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

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



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

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) unless quiet?
  to_file loggify(:message, string)
end

.normal(string) ⇒ Object

Outputs the data as if it were a regular ‘puts’ command, but also logs it to the backup.log



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

def self.normal(string)
  puts    string unless quiet?
  to_file string
end

.quiet?Boolean

Returns ‘true’ (boolean) if the QUIET constant is defined By default it isn’t defined, only when initializing Backup using the ‘–quite’ (or ‘-q’) option in the CLI (e.g. backup perform -t my_backup –quiet)

Returns:

  • (Boolean)


97
98
99
# File 'lib/backup/logger.rb', line 97

def self.quiet?
  const_defined?(:QUIET) && QUIET
end

.red(string) ⇒ Object

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



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

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

.silent(string) ⇒ Object

Silently logs data to the log file



37
38
39
# File 'lib/backup/logger.rb', line 37

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

.timeObject

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



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

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



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

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) unless quiet?
  to_file loggify(:warning, string)
end

.yellow(string) ⇒ Object

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



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

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