Class: AWS::S3::Logging::Log::Line

Inherits:
String
  • Object
show all
Defined in:
lib/aws-matt/s3/logging.rb

Overview

Each line of a log exposes the raw line, but it also has method accessors for all the fields of the logged request.

The list of supported log line fields are listed in the S3 documentation: docs.amazonwebservices.com/AmazonS3/2006-03-01/LogFormat.html

line = log.lines.first
line.remote_ip
# => '72.21.206.5'

If a certain field does not apply to a given request (for example, the key field does not apply to a bucket request), or if it was unknown or unavailable, it will return nil.

line.operation
# => 'REST.GET.BUCKET'
line.key
# => nil

Constant Summary collapse

DATE =
/\[([^\]]+)\]/
QUOTED_STRING =
/"([^"]+)"/
REST =
/(\S+)/
LINE_SCANNER =
/#{DATE}|#{QUOTED_STRING}|#{REST}/
@@decorators =
Hash.new {|hash, key| hash[key] = lambda {|entry| CoercibleString.coerce(entry)}}
@@fields =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ Line

:nodoc:



170
171
172
173
# File 'lib/aws-matt/s3/logging.rb', line 170

def initialize(line) #:nodoc:
  super(line)
  @parts = parse
end

Class Method Details

.field(name, offset, type = nil, &block) ⇒ Object

:nodoc:



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/aws-matt/s3/logging.rb', line 143

def field(name, offset, type = nil, &block) #:nodoc:
  decorators[name] = block if block_given?
  fields << name
  class_eval(<<-EVAL, __FILE__, __LINE__)
    def #{name}
      value = parts[#{offset} - 1]
      if value == '-'
        nil
      else
        self.class.decorators[:#{name}].call(value)
      end
    end
    memoized :#{name}
  EVAL
end

.typecast_time(datetime) ⇒ Object

Time.parse doesn’t like %d/%B/%Y:%H:%M:%S %z so we have to transform it unfortunately



160
161
162
163
164
165
166
167
# File 'lib/aws-matt/s3/logging.rb', line 160

def typecast_time(datetime) #:nodoc:
  month       = datetime[/[a-z]+/i]
  month_names = [nil, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
  datetime.sub!(%r|^(\w{2})/(\w{3})|, '\2/\1')
  datetime.sub!(month, month_names.index(month).to_s)
  datetime.sub!(':', ' ')
  Time.parse(datetime)
end

Instance Method Details

#attributesObject

Returns all fields of the line in a hash of the form :field_name => :field_value.

line.attributes.values_at(:bucket, :key)
# => ['marcel', 'kiss.jpg']


197
198
199
200
201
202
# File 'lib/aws-matt/s3/logging.rb', line 197

def attributes
  self.class.fields.inject({}) do |attribute_hash, field|
    attribute_hash[field] = send(field)
    attribute_hash
  end
end