Skip to content

Commit

Permalink
Use standard Log handler for server logs (#303)
Browse files Browse the repository at this point in the history
- Use `HTTP::LogHandler`
- Fix the logger initialization. Use `Kemal.config.logger = APILogHandler.new`
  instead of `logger APILogHandler.new` (See issue:
  kemalcr/kemal#653)

Signed-off-by: Aravinda Vishwanathapura <[email protected]>
  • Loading branch information
aravindavk authored Feb 1, 2023
1 parent cf20a8e commit 9f62483
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions mgr/src/server/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,17 @@ end
module StorageMgr
class StorageManagerAPILogHandler < Kemal::BaseLogHandler
def initialize
@handler = HTTP::LogHandler.new
end

def call(context : HTTP::Server::Context)
elapsed_time = Time.measure { call_next(context) }
elapsed_text = elapsed_text(elapsed_time)
Log.info &.emit("#{context.request.method} #{context.request.resource}", status_code: "#{context.response.status_code}", duration: "#{elapsed_text}")
context
@handler.next = @next
@handler.call(context)
end

def write(message : String)
Log.info { message.strip }
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

def self.start_services
Expand Down Expand Up @@ -166,7 +158,7 @@ module StorageMgr

Log.info &.emit("Starting the Storage manager ReST API server", port: "#{Kemal.config.port}")

logger StorageManagerAPILogHandler.new
Kemal.config.logger = StorageManagerAPILogHandler.new
add_handler ApiExceptionHandler.new
add_handler MgrRequestsProxyHandler.new
add_handler AuthHandler.new
Expand Down

0 comments on commit 9f62483

Please sign in to comment.