Class: Merb::Logger
- Defined in:
- lib/merb-core/logger.rb,
lib/merb-core/logger.rb
Overview
Merb::Logger = Extlib::Logger
Constant Summary collapse
- 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
Mash.new({ :fatal => 7, :error => 6, :warn => 4, :info => 3, :debug => 0 })
- @@mutex =
{}
Instance Attribute Summary collapse
-
#auto_flush ⇒ Object
Returns the value of attribute auto_flush.
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#delimiter ⇒ Object
Returns the value of attribute delimiter.
-
#init_args ⇒ Object
readonly
Returns the value of attribute init_args.
-
#level ⇒ Object
Returns the value of attribute level.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
Instance Method Summary collapse
-
#<<(string = nil) ⇒ Object
(also: #push)
Appends a message to the log.
-
#close ⇒ Object
Close and remove the current log object.
-
#flush ⇒ Object
Flush the entire buffer to the log object.
-
#initialize(*args) ⇒ Logger
constructor
To initialize the logger you create a new object, proxies to set_log.
-
#set_log(stream = Merb::Config[:log_stream], log_level = Merb::Config[:log_level], delimiter = Merb::Config[:log_delimiter], auto_flush = Merb::Config[:log_auto_flush]) ⇒ Object
Replaces an existing logger with a new one.
-
#verbose(message, level = :warn) ⇒ Object
:api: public.
-
#verbose!(message, level = :warn) ⇒ Object
:api: public.
Constructor Details
#initialize(*args) ⇒ Logger
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.
84 85 86 |
# File 'lib/merb-core/logger.rb', line 84 def initialize(*args) set_log(*args) end |
Instance Attribute Details
#auto_flush ⇒ Object
Returns the value of attribute auto_flush.
56 57 58 |
# File 'lib/merb-core/logger.rb', line 56 def auto_flush @auto_flush end |
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
57 58 59 |
# File 'lib/merb-core/logger.rb', line 57 def buffer @buffer end |
#delimiter ⇒ Object
Returns the value of attribute delimiter.
55 56 57 |
# File 'lib/merb-core/logger.rb', line 55 def delimiter @delimiter end |
#init_args ⇒ Object (readonly)
Returns the value of attribute init_args.
59 60 61 |
# File 'lib/merb-core/logger.rb', line 59 def init_args @init_args end |
#level ⇒ Object
Returns the value of attribute level.
54 55 56 |
# File 'lib/merb-core/logger.rb', line 54 def level @level end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
58 59 60 |
# File 'lib/merb-core/logger.rb', line 58 def log @log end |
Instance Method Details
#<<(string = nil) ⇒ Object 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.
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/merb-core/logger.rb', line 143 def <<(string = nil) = "" << delimiter << string if string << "\n" unless [-1] == ?\n @buffer << flush if @auto_flush end |
#close ⇒ Object
Close and remove the current log object.
129 130 131 132 133 |
# File 'lib/merb-core/logger.rb', line 129 def close flush @log.close if @log.respond_to?(:close) && !@log.tty? @log = nil end |
#flush ⇒ Object
Flush the entire buffer to the log object.
121 122 123 124 125 126 |
# File 'lib/merb-core/logger.rb', line 121 def flush return unless @buffer.size > 0 @mutex.synchronize do @log.write(@buffer.slice!(0..-1).join('')) end end |
#set_log(stream = Merb::Config[:log_stream], log_level = Merb::Config[:log_level], delimiter = Merb::Config[:log_delimiter], auto_flush = Merb::Config[:log_auto_flush]) ⇒ Object
Replaces an existing logger with a new one.
Parameters
- stream<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.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/merb-core/logger.rb', line 100 def set_log(stream = Merb::Config[:log_stream], log_level = Merb::Config[:log_level], delimiter = Merb::Config[:log_delimiter], auto_flush = Merb::Config[:log_auto_flush]) @buffer = [] @delimiter = delimiter @auto_flush = auto_flush if Levels[log_level] @level = Levels[log_level] else @level = log_level end @log = stream @log.sync = true @mutex = (@@mutex[@log] ||= Mutex.new) end |