Class: Backup::Logger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/backup/logger.rb,
lib/backup/logger/syslog.rb,
lib/backup/logger/console.rb,
lib/backup/logger/logfile.rb,
lib/backup/logger/fog_adapter.rb

Defined Under Namespace

Modules: FogAdapter Classes: Config, Console, Logfile, Message, Syslog

Constant Summary collapse

MUTEX =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Logger

Returns a new instance of Logger.



129
130
131
132
133
134
# File 'lib/backup/logger.rb', line 129

def initialize(config)
  @config = config
  @messages = []
  @loggers = []
  @has_warnings = @has_errors = false
end

Instance Attribute Details

#messagesObject (readonly)

Returns an Array of Message objects for all logged messages received. These are used to attach log files to Mail notifications.



127
128
129
# File 'lib/backup/logger.rb', line 127

def messages
  @messages
end

Class Method Details

.clear!Object

Called after each backup model/trigger has been performed.



102
103
104
105
# File 'lib/backup/logger.rb', line 102

def clear!
  @logger = nil
  logger.start!
end

.configure(&block) ⇒ Object

Allows the Logger to be configured.

# shown with their default values
Backup::Logger.configure do
  # Console options:
  console.quiet = false

  # Logfile options:
  logfile.enabled   = true
  logfile.log_path  = 'log'
  logfile.max_bytes = 500_000

  # Syslog options:
  syslog.enabled  = false
  syslog.ident    = 'backup'
  syslog.options  = Syslog::LOG_PID
  syslog.facility = Syslog::LOG_LOCAL0
  syslog.info     = Syslog::LOG_INFO
  syslog.warn     = Syslog::LOG_WARNING
  syslog.error    = Syslog::LOG_ERR

  # Ignore Warnings:
  # Converts :warn level messages to level :info
  ignore_warning 'that contains this string'
  ignore_warning /that matches this regexp/
end

See each Logger’s Option class for details.



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

def configure(&block)
  config.dsl.instance_eval(&block)
end

Instance Method Details

#abort!Object

If errors are encountered by Backup::CLI while preparing to perform the backup jobs, this method is called to dump all messages to the console before Backup exits.



180
181
182
183
# File 'lib/backup/logger.rb', line 180

def abort!
  console = Console.new
  console.log(messages.shift) until messages.empty?
end

#has_errors?Boolean

Returns true if any :error level messages have been received.

Returns:

  • (Boolean)


153
154
155
# File 'lib/backup/logger.rb', line 153

def has_errors?
  @has_errors
end

#has_warnings?Boolean

Returns true if any :warn level messages have been received.

Returns:

  • (Boolean)


147
148
149
# File 'lib/backup/logger.rb', line 147

def has_warnings?
  @has_warnings
end

#levelObject

Sends a message to the Logger using the specified log level. obj may be any Object that responds to #to_s (i.e. an Exception)



139
140
141
142
143
# File 'lib/backup/logger.rb', line 139

[:info, :warn, :error].each do |level|
  define_method level, lambda {|obj|
    MUTEX.synchronize { log(obj, level) }
  }
end

#start!Object

The Logger is available as soon as Backup is loaded, and stores all messages it receives. Since the Logger may be configured via the command line and/or the user’s config.rb, no messages are sent until configuration can be completed. (see CLI#perform)

Once configuration is completed, this method is called to activate all enabled loggers and send them any messages that have been received up to this point. From this point onward, these loggers will be sent all messages as soon as they’re received.



167
168
169
170
171
172
173
174
# File 'lib/backup/logger.rb', line 167

def start!
  @config.loggers.each do |logger|
    @loggers << logger.class.new(logger.options) if logger.enabled?
  end
  messages.each do |message|
    @loggers.each {|logger| logger.log(message) }
  end
end