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.

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.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lumberjack/device/writer.rb', line 52

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] || 32 * 1024)
  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.



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

def buffer_size
  @buffer_size
end

Instance Method Details

#closeObject

Close the underlying stream.



77
78
79
80
# File 'lib/lumberjack/device/writer.rb', line 77

def close
  flush
  stream.close
end

#flushObject

Flush the underlying stream.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lumberjack/device/writer.rb', line 83

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.



68
69
70
71
72
73
74
# File 'lib/lumberjack/device/writer.rb', line 68

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