Class: DataObjects::Logger

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

Overview

Public DataObjects Logger API

Logger taken from Merb :)

To replace an existing logger with a new one:

DataObjects::Logger.set_log(log{String, IO},level{Symbol, String})

Available logging levels are

DataObjects::Logger::{ Fatal, Error, Warn, Info, Debug }

Logging via:

DataObjects.logger.fatal(message<String>)
DataObjects.logger.error(message<String>)
DataObjects.logger.warn(message<String>)
DataObjects.logger.info(message<String>)
DataObjects.logger.debug(message<String>)

Flush the buffer to

DataObjects.logger.flush

Remove the current log object

DataObjects.logger.close

Private DataObjects Logger API

To initialize the logger you create a new object, proxies to set_log.

DataObjects::Logger.new(log{String, IO},level{Symbol, String})

Logger will not create the file until something is actually logged This avoids file creation on DataObjects init when it creates the default logger.

Defined Under Namespace

Classes: Message

Constant Summary collapse

LEVELS =

Ruby (standard) logger levels:

off:   absolutely nothing
fatal: an unhandleable error that results in a program crash
error: a handleable error condition
warn:  a warning
info:  generic (useful) information about system operation
debug: low-level information for developers

DataObjects::Logger::LEVELS[:off, :fatal, :error, :warn, :info, :debug]

{
  :off   => 99999,
  :fatal => 7,
  :error => 6,
  :warn  => 4,
  :info  => 3,
  :debug => 0
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Logger

To initialize the logger you create a new object, proxies to set_log.

DataObjects::Logger.new(log{String, IO},level{Symbol, String})

Parameters:

  • log (IO, String)

    either an IO object or a name of a logfile.

  • log_level (String)

    the message string to be logged

  • delimiter (String)

    delimiter to use between message sections

  • log_creation (Boolean)

    log that the file is being created



167
168
169
# File 'lib/data_objects/logger.rb', line 167

def initialize(*args)
  set_log(*args)
end

Instance Attribute Details

#aioObject

Use asynchronous I/O?



56
57
58
# File 'lib/data_objects/logger.rb', line 56

def aio
  @aio
end

#bufferObject (readonly)

Direct access to the buffer



62
63
64
# File 'lib/data_objects/logger.rb', line 62

def buffer
  @buffer
end

#delimiterObject

delimiter to use between message sections



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

def delimiter
  @delimiter
end

#levelObject

a symbol representing the log level from :fatal, :error, :warn, :info, :debug



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

def level
  @level
end

#logObject (readonly)

The name of the log file



64
65
66
# File 'lib/data_objects/logger.rb', line 64

def log
  @log
end

Instance Method Details

#closeObject

Close and remove the current log object.

DataObjects.logger.close


210
211
212
213
214
# File 'lib/data_objects/logger.rb', line 210

def close
  flush
  @log.close if @log.respond_to?(:close)
  @log = nil
end

#flushObject

Flush the entire buffer to the log object.

DataObjects.logger.flush


202
203
204
205
# File 'lib/data_objects/logger.rb', line 202

def flush
  return unless @buffer.size > 0
  @log.write_method(@buffer.slice!(0..-1).join)
end

#push(string) ⇒ Object Also known as: <<

Note that the string is discarded if the string’s log level less than the logger’s log level.

Note that if the logger is aio capable then the logger will use non-blocking asynchronous writes.

Parameters:

  • level (Fixnum)

    the logging level as an integer

  • string (String)

    the message string to be logged



227
228
229
# File 'lib/data_objects/logger.rb', line 227

def push(string)
  internal_push(string)
end

#set_log(log, log_level = :off, delimiter = " ~ ", log_creation = false) ⇒ Object

To replace an existing logger with a new one:

DataObjects::Logger.set_log(log{String, IO},level{Symbol, String})

Parameters:

  • log (IO, String)

    either an IO object or a name of a logfile.

  • log_level (Symbol) (defaults to: :off)

    a symbol representing the log level from :fatal, :error, :warn, :info, :debug

  • delimiter (String) (defaults to: " ~ ")

    delimiter to use between message sections

  • log_creation (Boolean) (defaults to: false)

    log that the file is being created



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/data_objects/logger.rb', line 180

def set_log(log, log_level = :off, delimiter = " ~ ", log_creation = false)
  delimiter    ||= " ~ "

  if log_level && LEVELS[log_level.to_sym]
    self.level = log_level.to_sym
  else
    self.level = :debug
  end

  @buffer    = []
  @delimiter = delimiter

  initialize_log(log)

  DataObjects.logger = self

  self.info("Logfile created") if log_creation
end