Class: Logging::Appender

Inherits:
Object show all
Defined in:
lib/gems/logging-0.9.4/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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Appender

call-seq:

Appender.new( name )
Appender.new( name, :layout => layout )

Creates a new appender using the given name. If no Layout is specified, then a Basic layout will be used. Any logging header supplied by the layout will be written to the logging destination when the Appender is created.

Options:

:layout   => the layout to use when formatting log events
:level    => the level at which to log


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 87

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

  unless header.nil? || header.empty?
    begin 
      sync {write(header)}
    rescue StandardError => err
      ::Logging.log_internal(-2) {err}
    end
  end

  ::Logging::Appender[@name] = self
end

Instance Attribute Details

#layoutObject

class << self



71
72
73
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 71

def layout
  @layout
end

#levelObject

class << self



71
72
73
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 71

def level
  @level
end

#nameObject (readonly)

class << self



71
72
73
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 71

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.



29
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 29

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.



37
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 37

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.



45
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 45

def remove( name ) @appenders.delete(name) end

.stderrObject

call-seq:

Appender.stderr

Returns an instance of the Stderr Appender. Unless the user explicitly creates a new Stderr Appender, the instance returned by this method will always be the same:

Appender.stderr.object_id == Appender.stderr.object_id    #=> true


67
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 67

def stderr( ) self['stderr'] || ::Logging::Appenders::Stderr.new end

.stdoutObject

call-seq:

Appender.stdout

Returns an instance of the Stdout Appender. Unless the user explicitly creates a new Stdout Appender, the instance returned by this method will always be the same:

Appender.stdout.object_id == Appender.stdout.object_id    #=> true


56
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 56

def stdout( ) self['stdout'] || ::Logging::Appenders::Stdout.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.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 139

def <<( str )
  if @closed
    raise RuntimeError,
          "appender '<#{self.class.name}: #{@name}>' is closed"
  end

  unless @level >= ::Logging::LEVELS.length
    begin
      sync {write(str)}
    rescue StandardError => err
      ::Logging.log_internal(-2) {err}
    end
  end
  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.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 114

def append( event )
  if @closed
    raise RuntimeError,
          "appender '<#{self.class.name}: #{@name}>' is closed"
  end

  # only append if the event level is less than or equal to the configured
  # appender level
  unless @level > event.level
    begin
      sync {write(event)}
    rescue StandardError => err
      ::Logging.log_internal(-2) {err}
    end
  end

  self
end

#close(footer = true) ⇒ Object

call-seq:

close( footer = 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.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 218

def close( footer = true )
  return self if @closed
  ::Logging::Appender.remove(@name)
  @closed = true
  if footer
    footer = @layout.footer
    unless footer.nil? || footer.empty?
      begin
        sync {write(footer)}
      rescue StandardError => err
        ::Logging.log_internal(-2) {err}
      end
    end
  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.

Returns:

  • (Boolean)


242
243
244
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 242

def closed?
  @closed
end

#flushObject

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.



252
253
254
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 252

def flush
  self
end

#inspectObject

call-seq:

inspect    => string

Returns a string representation of the appender.



261
262
263
264
265
266
267
# File 'lib/gems/logging-0.9.4/lib/logging/appender.rb', line 261

def inspect
  "<%s:0x%x name=\"%s\">" % [
      self.class.name.sub(%r/^Logging::/, ''),
      self.object_id,
      self.name
  ]
end