Class: Logging::Appender

Inherits:
Object
  • Object
show all
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.

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.



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

#layoutObject

class << self



74
75
76
# File 'lib/logging/appender.rb', line 74

def layout
  @layout
end

#levelObject

class << self



74
75
76
# File 'lib/logging/appender.rb', line 74

def level
  @level
end

#nameObject (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

.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


70
# File 'lib/logging/appender.rb', line 70

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


59
# File 'lib/logging/appender.rb', line 59

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.



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



194
195
196
197
198
199
200
201
202
203
# File 'lib/logging/appender.rb', line 194

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

Returns:

  • (Boolean)


212
213
214
# File 'lib/logging/appender.rb', line 212

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.



222
223
224
# File 'lib/logging/appender.rb', line 222

def flush
  self
end