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

Inherits:
String show all
Defined in:
lib/aws/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

Methods inherited from String

#previous, #previous!, #remove_extended, #remove_extended!, #tap, #to_header, #underscore, #valid_utf8?

Constructor Details

#initialize(line) ⇒ Line

:nodoc:



173
174
175
176
# File 'lib/aws/s3/logging.rb', line 173

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

Class Method Details

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

:nodoc:



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/aws/s3/logging.rb', line 149

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



166
167
168
169
170
# File 'lib/aws/s3/logging.rb', line 166

def typecast_time(datetime) #:nodoc:
  datetime.sub!(%r|^(\w{2})/(\w{3})/(\w{4})|, '\2 \1 \3')
  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']


200
201
202
203
204
205
# File 'lib/aws/s3/logging.rb', line 200

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