Class: Logging::Appender
- Inherits:
-
Object
- Object
- Logging::Appender
- Defined in:
- lib/logging/appender.rb
Overview
The Appender
class is provides methods for appending log events to a logging destination. The log events are formatted into strings using a Layout.
All other Appenders inherit from this class which provides stub methods. Each subclass should provide a write
method that will write log messages to the logging destination.
A private sync
method is provided for use by subclasses. It is used to synchronize writes to the logging destination, and can be used by subclasses to synchronize the closing or flushing of the logging destination.
Direct Known Subclasses
Logging::Appenders::Email, Logging::Appenders::Growl, Logging::Appenders::IO, Logging::Appenders::Syslog
Instance Attribute Summary collapse
-
#layout ⇒ Object
class << self.
-
#level ⇒ Object
class << self.
-
#name ⇒ Object
readonly
class << self.
Class Method Summary collapse
-
.[](name) ⇒ Object
call-seq: Appender.
-
.[]=(name, val) ⇒ Object
call-seq: Appender = appender.
-
.remove(name) ⇒ Object
call-seq: Appenders.remove( name ).
-
.stderr ⇒ Object
call-seq: Appender.stderr.
-
.stdout ⇒ Object
call-seq: Appender.stdout.
Instance Method Summary collapse
-
#<<(str) ⇒ Object
call-seq: appender << string.
-
#append(event) ⇒ Object
call-seq: append( event ).
-
#close(footer = true) ⇒ Object
call-seq: close( footer = true ).
-
#closed? ⇒ Boolean
call-seq: closed?.
-
#flush ⇒ Object
call-seq: flush.
-
#initialize(name, opts = {}) ⇒ Appender
constructor
call-seq: Appender.new( name ) Appender.new( name, :layout => layout ).
Constructor Details
#initialize(name, opts = {}) ⇒ Appender
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/logging/appender.rb', line 85 def initialize( name, opts = {} ) @name = name.to_s @closed = false self.layout = opts.getopt(:layout, ::Logging::Layouts::Basic.new) self.level = opts.getopt(:level) @mutex = Mutex.new header = @layout.header sync {write(header)} unless header.nil? || header.empty? ::Logging::Appender[@name] = self end |
Instance Attribute Details
#layout ⇒ Object
class << self
74 75 76 |
# File 'lib/logging/appender.rb', line 74 def layout @layout end |
#level ⇒ Object
class << self
74 75 76 |
# File 'lib/logging/appender.rb', line 74 def level @level end |
#name ⇒ Object (readonly)
class << self
74 75 76 |
# File 'lib/logging/appender.rb', line 74 def name @name end |
Class Method Details
.[](name) ⇒ Object
call-seq:
Appender[name]
Returns the appender instance stroed in the Appender hash under the key name, or nil
if no appender has been created using that name.
32 |
# File 'lib/logging/appender.rb', line 32 def []( name ) @appenders[name] end |
.[]=(name, val) ⇒ Object
call-seq:
Appender[name] = appender
Stores the given appender instance in the Appender hash under the key name.
40 |
# File 'lib/logging/appender.rb', line 40 def []=( name, val ) @appenders[name] = val end |
.remove(name) ⇒ Object
call-seq:
Appenders.remove( name )
Removes the appender instance stored in the Appender hash under the key name.
48 |
# File 'lib/logging/appender.rb', line 48 def remove( name ) @appenders.delete(name) end |
.stderr ⇒ Object
70 |
# File 'lib/logging/appender.rb', line 70 def stderr( ) self['stderr'] || ::Logging::Appenders::Stderr.new end |
Instance Method Details
#<<(str) ⇒ Object
call-seq:
appender << string
Write the given string to the logging destination “as is” – no layout formatting will be performed.
121 122 123 124 125 126 127 128 129 |
# File 'lib/logging/appender.rb', line 121 def <<( str ) if @closed raise RuntimeError, "appender '<#{self.class.name}: #{@name}>' is closed" end sync {write(str)} unless @level >= ::Logging::LEVELS.length self end |
#append(event) ⇒ Object
call-seq:
append( event )
Write the given event to the logging destination. The log event will be processed through the Layout associated with the Appender.
105 106 107 108 109 110 111 112 113 |
# File 'lib/logging/appender.rb', line 105 def append( event ) if @closed raise RuntimeError, "appender '<#{self.class.name}: #{@name}>' is closed" end sync {write(event)} unless @level > event.level self end |
#close(footer = true) ⇒ Object
call-seq:
close( = true )
Close the appender and writes the layout footer to the logging destination if the footer flag is set to true
. Log events will no longer be written to the logging destination after the appender is closed.
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/logging/appender.rb', line 194 def close( = true ) return self if @closed ::Logging::Appender.remove(@name) @closed = true if = @layout. sync {write()} unless .nil? || .empty? end self end |
#closed? ⇒ Boolean
call-seq:
closed?
Returns true
if the appender has been closed; returns false
otherwise. When an appender is closed, no more log events can be written to the logging destination.
212 213 214 |
# File 'lib/logging/appender.rb', line 212 def closed? @closed end |
#flush ⇒ Object
call-seq:
flush
Call flush
to force an appender to write out any buffered log events. Similar to IO#flush, so use in a similar fashion.
222 223 224 |
# File 'lib/logging/appender.rb', line 222 def flush self end |