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

Inherits:
String
  • Object
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

Constructor Details

#initialize(line) ⇒ Line

:nodoc:



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

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

Class Method Details

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

:nodoc:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/aws/s3/logging.rb', line 139

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



156
157
158
159
160
161
162
163
164
# File 'lib/aws/s3/logging.rb', line 156

def typecast_time(datetime) #:nodoc:
  month = datetime[/[a-z]+/i]
  datetime.sub!(%r|^(\w{2})/(\w{3})|, '\2/\1')
  if Date.constants.include?('ABBR_MONTHS')
    datetime.sub!(month, Date::ABBR_MONTHS[month.downcase].to_s) 
  end
  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']


194
195
196
197
198
199
# File 'lib/aws/s3/logging.rb', line 194

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