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

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


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/logging/appender.rb', line 35

def initialize( name, opts = {} )
  ::Logging.init unless ::Logging.const_defined? :MAX_LEVEL_LENGTH

  @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::Appenders[@name] = self
end

Instance Attribute Details

#layoutObject

Returns the value of attribute layout.



19
20
21
# File 'lib/logging/appender.rb', line 19

def layout
  @layout
end

#levelObject

Returns the value of attribute level.



19
20
21
# File 'lib/logging/appender.rb', line 19

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/logging/appender.rb', line 19

def name
  @name
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.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/logging/appender.rb', line 89

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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/logging/appender.rb', line 64

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.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/logging/appender.rb', line 168

def close( footer = true )
  return self if @closed
  ::Logging::Appenders.remove(@name)
  @closed = true

  sync {flush}

  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)


195
196
197
# File 'lib/logging/appender.rb', line 195

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.



205
206
207
# File 'lib/logging/appender.rb', line 205

def flush
  self
end

#inspectObject

call-seq:

inspect    => string

Returns a string representation of the appender.



214
215
216
217
218
219
220
# File 'lib/logging/appender.rb', line 214

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