class Sawmill::EntryProcessor::Format

This processor formats log entries and writes them to a destination.

Public Class Methods

new(destination_, opts_={}) click to toggle source

Create a formatter.

The destination can be a ruby IO object, a Sawmill::Rotater, or any object that responds to the “write” and “close” methods as defined by the ruby IO class.

Recognized options include:

:include_id

Include the record ID in every log entry. Default is false.

:fractional_second_digits

Number of digits of fractional seconds to display in timestamps. Default is 2. Accepted values are 0 to 6.

:level_width

Column width of the level field.

:local_time

If true, outputs local time with the timezone offset indicator. If false (the default), outputs UTC.

:iso_8601_time

If true, outputs time in strict ISO 8601 format. If false (the default), outputs a slightly more readable format.

:length_limit

Limit to the entry length. Entries are truncated to this length when written. If not specified, entries are not truncated.

# File lib/sawmill/entry_processor/format.rb, line 72
def initialize(destination_, opts_={})
  if destination_.kind_of?(Rotater)
    @rotater = destination_
    @channels = {}
    @standby_channel = nil
  else
    @rotater = nil
    if destination_.respond_to?(:close) && destination_.respond_to?(:write)
      @io = destination_
    else
      raise ::ArgumentError, "Unknown destination type"
    end
  end
  @include_id = opts_[:include_id]
  @fractional_second_digits = (opts_[:fractional_second_digits] || 2).to_i
  @fractional_second_digits = 0 if @fractional_second_digits < 0
  @fractional_second_digits = 6 if @fractional_second_digits > 6
  @usec_factor = 1
  (6 - @fractional_second_digits).times{ @usec_factor *= 10 }
  @level_width = opts_[:level_width]
  @length_limit = opts_[:length_limit]
  @local_time = opts_[:local_time]
  @iso_8601_time = opts_[:iso_8601_time]
end

Public Instance Methods

attribute(entry_) click to toggle source
# File lib/sawmill/entry_processor/format.rb, line 142
def attribute(entry_)
  return false unless @io || @rotater
  opcode_ = entry_.operation == :append ? '+' : '='
  str_ = _format_entry(entry_, '=', "#{entry_.key} #{opcode_} #{entry_.value}")
  _write_str(str_, entry_.record_id)
  true
end
begin_record(entry_) click to toggle source
# File lib/sawmill/entry_processor/format.rb, line 98
def begin_record(entry_)
  return false unless @io || @rotater
  record_id_ = entry_.record_id
  if @rotater
    if @standby_channel
      @standby_channel.check_rotate
      io_ = @standby_channel
      @standby_channel = nil
    else
      io_ = @rotater.create_channel
    end
    @channels[record_id_] = io_
  else
    io_ = @io
  end
  io_.write(_format_entry(entry_, '^', "BEGIN #{record_id_}"))
  true
end
end_record(entry_) click to toggle source
# File lib/sawmill/entry_processor/format.rb, line 117
def end_record(entry_)
  return false unless @io || @rotater
  record_id_ = entry_.record_id
  str_ = _format_entry(entry_, '$', "END #{record_id_}")
  if @rotater
    if (channel_ = @channels.delete(record_id_))
      @standby_channel.close if @standby_channel
      @standby_channel = channel_
    else
      @standby_channel ||= @rotater.create_channel
    end
    @standby_channel.write(str_)
    @standby_channel.check_rotate
  else
    @io.write(str_)
  end
  true
end
finish() click to toggle source
# File lib/sawmill/entry_processor/format.rb, line 156
def finish
  if @rotater
    @default_channel.close
    @channels.values.each{ |channel_| channel_.close }
    @rotater = nil
  elsif @io
    @io.close
    @io = nil
  end
  nil
end
message(entry_) click to toggle source
# File lib/sawmill/entry_processor/format.rb, line 136
def message(entry_)
  return false unless @io || @rotater
  _write_str(_format_entry(entry_, '.', entry_.message), entry_.record_id)
  true
end
unknown_data(entry_) click to toggle source
# File lib/sawmill/entry_processor/format.rb, line 150
def unknown_data(entry_)
  return false unless @io || @rotater
  _write_str(entry_.line+"\n", nil)
  true
end