invidious-mod-JP/src/invidious/helpers/logger.cr

36 lines
747 B
Crystal
Raw Normal View History

2019-01-23 20:15:19 +00:00
require "logger"
class Invidious::LogHandler < Kemal::BaseLogHandler
def initialize(@io : IO = STDOUT)
end
def call(context : HTTP::Server::Context)
2019-06-08 00:56:41 +00:00
time = Time.utc
2019-01-23 20:15:19 +00:00
call_next(context)
2019-06-08 00:56:41 +00:00
elapsed_text = elapsed_text(Time.utc - time)
2019-01-23 20:15:19 +00:00
@io << time << ' ' << context.response.status_code << ' ' << context.request.method << ' ' << context.request.resource << ' ' << elapsed_text << '\n'
if @io.is_a? File
@io.flush
end
context
end
def write(message : String)
@io << message
if @io.is_a? File
@io.flush
end
end
private def elapsed_text(elapsed)
millis = elapsed.total_milliseconds
return "#{millis.round(2)}ms" if millis >= 1
"#{(millis * 1000).round(2)}µs"
end
end