Class: DataMapper::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-core/logger.rb

Constant Summary collapse

LEVELS =
Note:

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

DataMapper::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.

DataMapper::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



145
146
147
# File 'lib/dm-core/logger.rb', line 145

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

Instance Attribute Details

#aioObject

Returns the value of attribute aio.



41
42
43
# File 'lib/dm-core/logger.rb', line 41

def aio
  @aio
end

#bufferObject (readonly)

Returns the value of attribute buffer.



44
45
46
# File 'lib/dm-core/logger.rb', line 44

def buffer
  @buffer
end

#delimiterObject

Returns the value of attribute delimiter.



42
43
44
# File 'lib/dm-core/logger.rb', line 42

def delimiter
  @delimiter
end

#levelObject

Returns the value of attribute level.



43
44
45
# File 'lib/dm-core/logger.rb', line 43

def level
  @level
end

#logObject (readonly)

Returns the value of attribute log.



45
46
47
# File 'lib/dm-core/logger.rb', line 45

def log
  @log
end

Instance Method Details

#closeObject

Close and remove the current log object.

DataMapper.logger.close


187
188
189
190
191
# File 'lib/dm-core/logger.rb', line 187

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

#flushObject

Flush the entire buffer to the log object.

DataMapper.logger.flush


179
180
181
182
# File 'lib/dm-core/logger.rb', line 179

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

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

Note:

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

Note:

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



204
205
206
# File 'lib/dm-core/logger.rb', line 204

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:

DataMapper.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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/dm-core/logger.rb', line 157

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)

  DataMapper.logger = self

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