forked from choonkeat/heroku-log-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
41 lines (33 loc) · 1.09 KB
/
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
41
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
[{ msg: env['REQUEST_URI'], ts: '' }]
else
HerokuLogParser.parse(env['rack.input'].read).collect { |m| { msg: m[:message], ts: m[:emitted_at].strftime('%Y-%m-%dT%H:%M:%S.%L%z') } }
end
lines.each do |line|
msg = line[:msg]
next unless msg.start_with?(PREFIX)
Writer.instance.write([line[:ts], msg[PREFIX_LENGTH..-1]].join(' ').strip) # WRITER_LIB
end
rescue Exception
@logger.error $!
@logger.error $@
ensure
return [200, { 'Content-Length' => '0' }, []]
end
end