Module: Backup::Logger

Defined in:
lib/backup/logger.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.quietObject

Returns the value of attribute quiet.



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

def quiet
  @quiet
end

Class Method Details

.clear!Object



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

def clear!
  messages.clear
  @has_warnings = false
end

.error(string) ⇒ Object

Outputs an error to the console and writes it to the backup.log Called when an Exception has caused the backup process to abort.



19
20
21
22
# File 'lib/backup/logger.rb', line 19

def error(string)
  to_console  loggify(string, :error,   :red), true
  to_file     loggify(string, :error)
end

.has_warnings?Boolean

Returns true if any warnings have been issued

Returns:

  • (Boolean)


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

def has_warnings?
  @has_warnings ||= false
end

.message(string) ⇒ Object

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



11
12
13
14
# File 'lib/backup/logger.rb', line 11

def message(string)
  to_console  loggify(string, :message, :green)
  to_file     loggify(string, :message)
end

.messagesObject

Returns an Array of all messages written to the log file for this session



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

def messages
  @messages ||= []
end

.normal(string) ⇒ Object

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



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

def normal(string)
  to_console  loggify(string)
  to_file     loggify(string)
end

.silent(string) ⇒ Object

Silently logs data to the log file



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

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

.truncate!(max_bytes = 500_000) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/backup/logger.rb', line 63

def truncate!(max_bytes = 500_000)
  log_file = File.join(Config.log_path, 'backup.log')
  return unless File.exist?(log_file)

  if File.stat(log_file).size > max_bytes
    FileUtils.mv(log_file, log_file + '~')
    File.open(log_file + '~', 'r') do |io_in|
      File.open(log_file, 'w') do |io_out|
        io_in.seek(-max_bytes, IO::SEEK_END) && io_in.gets
        while line = io_in.gets
          io_out.puts line
        end
      end
    end
    FileUtils.rm_f(log_file + '~')
  end
end

.warn(string) ⇒ Object

Outputs a notice to the console and writes it to the backup.log Sets #has_warnings? true so :on_warning notifications will be sent



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

def warn(string)
  @has_warnings = true
  to_console  loggify(string, :warning, :yellow), true
  to_file     loggify(string, :warning)
end