forked from choonkeat/heroku-log-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
40 lines (32 loc) · 981 Bytes
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'logger'
require 'heroku-log-parser'
require_relative './queue_io.rb'
require_relative ENV.fetch("WRITER_LIB", "./writer/s3.rb") # provider of `Writer < WriterBase` singleton
class App
PREFIX = ENV.fetch("FILTER_PREFIX", "")
PREFIX_LENGTH = PREFIX.length
LOG_REQUEST_URI = ENV['LOG_REQUEST_URI']
def initialize
@logger = Logger.new(STDOUT)
@logger.formatter = proc do |severity, datetime, progname, msg|
"[app #{$$} #{Thread.current.object_id}] #{msg}\n"
end
@logger.info "initialized"
end
def call(env)
lines = if LOG_REQUEST_URI
[env['REQUEST_URI']]
else
HerokuLogParser.parse(env['rack.input'].read).collect {|m| m[:message] }
end
lines.each do |line|
next unless line.start_with?(PREFIX)
Writer.instance.write(line[PREFIX_LENGTH..-1]) # WRITER_LIB
end
rescue Exception
@logger.error $!
@logger.error $@
ensure
return [200, { 'Content-Length' => '0' }, []]
end
end