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.



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

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.



124
125
126
# File 'lib/backup/logger.rb', line 124

def messages
  @messages
end

Class Method Details

.clear!Object

Called after each backup model/trigger has been performed.



99
100
101
102
# File 'lib/backup/logger.rb', line 99

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.



93
94
95
# File 'lib/backup/logger.rb', line 93

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.



177
178
179
180
# File 'lib/backup/logger.rb', line 177

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)


150
151
152
# File 'lib/backup/logger.rb', line 150

def has_errors?
  @has_errors
end

#has_warnings?Boolean

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

Returns:

  • (Boolean)


144
145
146
# File 'lib/backup/logger.rb', line 144

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)



136
137
138
139
140
# File 'lib/backup/logger.rb', line 136

[:info, :warn, :error].each do |level|
  define_method level do |obj|
    MUTEX.synchronize { log(obj, level) }
  end
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.



164
165
166
167
168
169
170
171
# File 'lib/backup/logger.rb', line 164

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