Class: Sawmill::EntryProcessor::Format

Inherits:
Base
  • Object
show all
Defined in:
lib/sawmill/entry_processor/format.rb

Overview

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

Instance Method Summary collapse

Methods inherited from Base

add_dsl_method, inherited

Constructor Details

#initialize(destination_, opts_ = {}) ⇒ Format

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.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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

Instance Method Details

#attribute(entry_) ⇒ Object



142
143
144
145
146
147
148
# 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_) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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_) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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

#finishObject



156
157
158
159
160
161
162
163
164
165
166
# 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_) ⇒ Object



136
137
138
139
140
# 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_) ⇒ Object



150
151
152
153
154
# 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