Skip to content

Commit

Permalink
refactor: adjusted to handle all request methods
Browse files Browse the repository at this point in the history
Reports on `GET, POST, PUT, DELETE, PATCH` request methods instead of
only `GET`
  • Loading branch information
jonrandahl committed May 25, 2023
1 parent e214c37 commit efb2b12
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/json_rails_logger/json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class JsonFormatter < ::Logger::Formatter
method path status duration user_agent accept request_id
].freeze

REQUEST_METHODS = %w[GET, POST, PUT, DELETE, PATCH].freeze

def call(severity, timestamp, _progname, raw_msg)
sev = process_severity(severity)
timestp = process_timestamp(timestamp)
Expand Down Expand Up @@ -45,7 +47,7 @@ def process_message(raw_msg)
return msg unless msg.is_a?(String)

return status_message(msg) if status_message?(msg)
return get_message(msg) if get_message?(msg)
return request_type(msg) if request_type?(msg)
return user_agent_message(msg) if user_agent_message?(msg)

{ message: msg.squish }
Expand Down Expand Up @@ -95,12 +97,12 @@ def status_message(msg)
{ status: status }
end

def get_message?(msg)
def request_type?(msg)
msg.is_a?(String) &&
msg.match(/GET http\S+/)
REQUEST_METHODS.any? { |method| msg.match(/#{method} http\S+/) }
end

def get_message(msg)
def request_type(msg)
splitted_msg = msg.split
method = splitted_msg[0]
path = splitted_msg[1]
Expand Down

0 comments on commit efb2b12

Please sign in to comment.