Class: VCAP::Logging::Sink::BaseSink

Inherits:
Object
  • Object
show all
Defined in:
lib/vcap/logging/sink/base_sink.rb

Overview

Sinks serve as the final destination for log records. Usually they are lightweight wrappers around other objects that perform IO (files and sockets come to mind).

Direct Known Subclasses

FileSink, StdioSink, StringSink, SyslogSink

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formatter = nil) ⇒ BaseSink

Returns a new instance of BaseSink.



21
22
23
24
25
26
# File 'lib/vcap/logging/sink/base_sink.rb', line 21

def initialize(formatter=nil)
  @formatter = formatter
  @opened    = false
  @mutex     = Mutex.new
  @autoflush = false
end

Instance Attribute Details

#autoflushObject

Returns the value of attribute autoflush.



19
20
21
# File 'lib/vcap/logging/sink/base_sink.rb', line 19

def autoflush
  @autoflush
end

#formatterObject

Returns the value of attribute formatter.



18
19
20
# File 'lib/vcap/logging/sink/base_sink.rb', line 18

def formatter
  @formatter
end

#openedObject (readonly)

Returns the value of attribute opened.



17
18
19
# File 'lib/vcap/logging/sink/base_sink.rb', line 17

def opened
  @opened
end

Instance Method Details

#add_record(log_record) ⇒ Object

Formats the log record using the configured formatter and NB: Depending on the implementation of write(), this may buffer the record in memory.

Parameters:

  • log_record

    VCAP::Logging::LogRecord Record to add

Raises:



51
52
53
54
55
56
57
58
# File 'lib/vcap/logging/sink/base_sink.rb', line 51

def add_record(log_record)
  raise UsageError, "You cannot add a record until the sink has been opened" unless @opened
  raise UsageError, "You must supply a formatter" unless @formatter

  message = @formatter.format_record(log_record)
  write(message)
  flush if @autoflush
end

#closeObject

Closes any underlying file descriptors and ensures that any log records buffered in memory are flushed.



38
39
40
# File 'lib/vcap/logging/sink/base_sink.rb', line 38

def close
  @mutex.synchronize { @opened = false }
end

#flushObject

Flushes any log records that may have been buffered in memory



61
62
63
# File 'lib/vcap/logging/sink/base_sink.rb', line 61

def flush
  nil
end

#openObject

Opens any underlying file descriptors, etc. and ensures that the sink is capable of receiving records.

This MUST be called before any calls to add_record().



32
33
34
# File 'lib/vcap/logging/sink/base_sink.rb', line 32

def open
  @mutex.synchronize { @opened = true }
end