Class: Lumberjack::Device::Writer

Inherits:
Lumberjack::Device show all
Defined in:
lib/lumberjack/device/writer.rb

Overview

This logging device writes log entries as strings to an IO stream. By default, messages will be buffered and written to the stream in a batch when the buffer is full or when flush is called.

Direct Known Subclasses

LogFile

Defined Under Namespace

Classes: Buffer

Constant Summary collapse

DEFAULT_FIRST_LINE_TEMPLATE =
"[:time :severity :progname(:pid) #:unit_of_work_id] :message".freeze
DEFAULT_ADDITIONAL_LINES_TEMPLATE =
"#{Lumberjack::LINE_SEPARATOR}> [#:unit_of_work_id] :message".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Lumberjack::Device

#cleanup_files!, #do_once

Constructor Details

#initialize(stream, options = {}) ⇒ Writer

Create a new device to write log entries to a stream. Entries are converted to strings using a Template. The template can be specified using the :template option. This can either be a Proc or a string that will compile into a Template object.

If the template is a Proc, it should accept an LogEntry as its only argument and output a string.

If the template is a template string, it will be used to create a Template. The :additional_lines and :time_format options will be passed through to the Template constuctor.

The default template is "[:time :severity :progname(:pid) #:unit_of_work_id] :message" with additional lines formatted as "\n [#:unit_of_work_id] :message". The unit of work id will only appear if it is present.

The size of the internal buffer in bytes can be set by providing :buffer_size (defaults to 32K).



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lumberjack/device/writer.rb', line 55

def initialize(stream, options = {})
  @lock = Mutex.new
  @stream = stream
  @stream.sync = true if @stream.respond_to?(:sync=)
  @buffer = Buffer.new
  @buffer_size = (options[:buffer_size] || 0)
  template = (options[:template] || DEFAULT_FIRST_LINE_TEMPLATE)
  if template.respond_to?(:call)
    @template = template
  else
    additional_lines = (options[:additional_lines] || DEFAULT_ADDITIONAL_LINES_TEMPLATE)
    @template = Template.new(template, :additional_lines => additional_lines, :time_format => options[:time_format])
  end
end

Instance Attribute Details

#buffer_sizeObject

The size of the internal buffer. Defaults to 32K.



10
11
12
# File 'lib/lumberjack/device/writer.rb', line 10

def buffer_size
  @buffer_size
end

Instance Method Details

#closeObject

Close the underlying stream.



87
88
89
90
# File 'lib/lumberjack/device/writer.rb', line 87

def close
  flush
  stream.close
end

#flushObject

Flush the underlying stream.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/lumberjack/device/writer.rb', line 93

def flush
  @lock.synchronize do
    before_flush
    unless @buffer.empty?
      out = @buffer.join(Lumberjack::LINE_SEPARATOR) << Lumberjack::LINE_SEPARATOR
      begin
        stream.write(out)
        stream.flush
      rescue => e
        $stderr.write("#{e.class.name}: #{e.message}#{' at ' + e.backtrace.first if e.backtrace}")
        $stderr.write(out)
        $stderr.flush
      end
      @buffer.clear
    end
  end
end

#write(entry) ⇒ Object

Write an entry to the stream. The entry will be converted into a string using the defined template.



78
79
80
81
82
83
84
# File 'lib/lumberjack/device/writer.rb', line 78

def write(entry)
  string = @template.call(entry)
  @lock.synchronize do
    @buffer << string
  end
  flush if @buffer.size >= buffer_size
end