Class: DataMapper::Logger
- Inherits:
-
Object
- Object
- DataMapper::Logger
- Defined in:
- lib/dm-core/support/logger.rb
Constant Summary
- Levels =
Notes
Ruby (standard) logger levels:
: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
{ :fatal => 7, :error => 6, :warn => 4, :info => 3, :debug => 0 }
Instance Attribute Summary (collapse)
-
- (Object) auto_flush
Returns the value of attribute auto_flush.
-
- (Object) buffer
readonly
Returns the value of attribute buffer.
-
- (Object) delimiter
Returns the value of attribute delimiter.
-
- (Object) init_args
readonly
Returns the value of attribute init_args.
-
- (Object) level
Returns the value of attribute level.
-
- (Object) log
readonly
Returns the value of attribute log.
Instance Method Summary (collapse)
-
- (Object) <<(string = nil)
(also: #push)
Appends a message to the log.
-
- (Object) close
Close and remove the current log object.
-
- (Object) flush
Flush the entire buffer to the log object.
-
- (Logger) initialize(*args)
constructor
To initialize the logger you create a new object, proxies to set_log.
-
- (Object) set_log(log, log_level = nil, delimiter = " ~ ", auto_flush = false)
Replaces an existing logger with a new one.
Constructor Details
- (Logger) initialize(*args)
To initialize the logger you create a new object, proxies to set_log.
Parameters
*args |
Arguments to create the log from. See set_logs for specifics. |
92 93 94 95 96 97 |
# File 'lib/dm-core/support/logger.rb', line 92 def initialize(*args) @init_args = args set_log(*args) self.auto_flush = true DataMapper.logger = self end |
Instance Attribute Details
- (Object) auto_flush
Returns the value of attribute auto_flush
43 44 45 |
# File 'lib/dm-core/support/logger.rb', line 43 def auto_flush @auto_flush end |
- (Object) buffer (readonly)
Returns the value of attribute buffer
44 45 46 |
# File 'lib/dm-core/support/logger.rb', line 44 def buffer @buffer end |
- (Object) delimiter
Returns the value of attribute delimiter
42 43 44 |
# File 'lib/dm-core/support/logger.rb', line 42 def delimiter @delimiter end |
- (Object) init_args (readonly)
Returns the value of attribute init_args
46 47 48 |
# File 'lib/dm-core/support/logger.rb', line 46 def init_args @init_args end |
- (Object) level
Returns the value of attribute level
41 42 43 |
# File 'lib/dm-core/support/logger.rb', line 41 def level @level end |
- (Object) log (readonly)
Returns the value of attribute log
45 46 47 |
# File 'lib/dm-core/support/logger.rb', line 45 def log @log end |
Instance Method Details
- (Object) <<(string = nil) Also known as: push
Appends a message to the log. The methods yield to an optional block and the output of this block will be appended to the message.
Parameters
string<String> |
The message to be logged. Defaults to nil. |
Returns
String |
The resulting message added to the log file. |
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/dm-core/support/logger.rb', line 147 def <<(string = nil) = "" << delimiter << string if string << "\n" unless [-1] == ?\n @buffer << flush if @auto_flush end |
- (Object) close
Close and remove the current log object.
133 134 135 136 137 |
# File 'lib/dm-core/support/logger.rb', line 133 def close flush @log.close if @log.respond_to?(:close) && !@log.tty? @log = nil end |
- (Object) flush
Flush the entire buffer to the log object.
125 126 127 128 129 130 |
# File 'lib/dm-core/support/logger.rb', line 125 def flush return unless @buffer.size > 0 to_flush = @buffer @buffer = [] @log.write(to_flush.join) end |
- (Object) set_log(log, log_level = nil, delimiter = " ~ ", auto_flush = false)
Replaces an existing logger with a new one.
Parameters
log<IO, String> |
Either an IO object or a name of a logfile. |
log_level<~to_sym> |
The log level from, e.g. :fatal or :info. Defaults to :error in the production environment and :debug otherwise. |
delimiter<String> |
Delimiter to use between message sections. Defaults to “ ~ ”. |
auto_flush<Boolean> |
Whether the log should automatically flush after new messages are added. Defaults to false. |
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/dm-core/support/logger.rb', line 111 def set_log(log, log_level = nil, delimiter = " ~ ", auto_flush = false) if log_level && Levels[log_level.to_sym] @level = Levels[log_level.to_sym] else @level = Levels[:debug] end @buffer = [] @delimiter = delimiter @auto_flush = auto_flush initialize_log(log) end |