Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify error styling, add human http status #1114

Merged
merged 3 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions spec/lucky/pretty_log_formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe Lucky::PrettyLogFormatter do
io = IO::Memory.new
format(io, {status: 200, duration: "1.4ms"})

io.to_s.chomp.should start_with(" #{"▸".colorize.dim} Sent #{"200".colorize.green} (1.4ms)")
io.to_s.chomp.should start_with(" #{"▸".colorize.dim} Sent #{"200 Ok".colorize.bold} (1.4ms)")
end
end

Expand Down Expand Up @@ -64,8 +64,8 @@ describe Lucky::PrettyLogFormatter do

format(io, severity: Log::Severity::Error, data: nil, exception: ex)

io.to_s.should start_with(" #{"▸".colorize.red} #{ex.class.name.colorize.bold.red}")
io.to_s.should contain("Details:")
io.to_s.should start_with(" #{"▸".colorize.red}")
io.to_s.should contain(" #{ex.class.name} ".colorize.bold.on_red.to_s)
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/lucky/error_handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Lucky::ErrorHandler
end

private def call_error_action(context : HTTP::Server::Context, error : Exception) : HTTP::Server::Context
Lucky::Log.dexter.error { {exception: error.inspect_with_backtrace} }
Lucky::Log.error(exception: error) { "" }
action.new(context).perform_action(error)
context
end
Expand Down
15 changes: 8 additions & 7 deletions src/lucky/logger_helpers.cr
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
module Lucky::LoggerHelpers
def self.colored_status_code(status_code : Int32) : String
def self.colored_http_status(status_code : Int32) : String
status_name = Wordsmith::Inflector.humanize(HTTP::Status.from_value?(status_code) || "")
message = "#{status_code} #{status_name}".colorize.bold

case status_code
when 200..399
Copy link
Member Author

@paulcsmith paulcsmith Apr 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most requests should be successful. So rather than distracting with a bold green status, just make it white. That way when a real errors pops up it stands out.

Note: We log halted pipes in yellow so redirect due to incorrect permissions still stands out

"#{status_code.colorize.green}"
when 400..499
"#{status_code.colorize(:yellow)}"
message.yellow
when 500..599
"#{status_code.colorize(:red)}"
message.red
else
"#{status_code}"
end
message
end.to_s
end

def self.elapsed_text(elapsed : Time::Span) : String
Expand Down
22 changes: 13 additions & 9 deletions src/lucky/pretty_log_formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ struct Lucky::PrettyLogFormatter < Dexter::BaseFormatter

def write : Nil
add_arrow
colored_status_code = Lucky::LoggerHelpers.colored_status_code(local_context["status"].as_i)
io << "Sent #{colored_status_code} (#{local_context["duration"]})"
http_status = Lucky::LoggerHelpers.colored_http_status(local_context["status"].as_i)
io << "Sent #{http_status} (#{local_context["duration"]})"
end
end

Expand All @@ -80,19 +80,23 @@ struct Lucky::PrettyLogFormatter < Dexter::BaseFormatter
def write : Nil
add_arrow
entry.exception.try do |ex|
io << ex.class.name.colorize.bold.red
io << " "
io << " #{ex.class.name} ".colorize.bold.on_red
if ex.message.try(&.lines)
io << "\n\n Details:\n".colorize.bold
io << "\n"
ex.message.try(&.lines).try(&.each do |line|
io << "\n "
io << line
end)
end
io << "\n\n Backtrace:\n".colorize.bold if ex.backtrace?
(ex.backtrace? || [] of String).each do |trace_line|
trace_line = trace_line.colorize.dim unless trace_line.starts_with?(/src|spec/)
io << "\n #{trace_line}"
if backtrace = ex.backtrace?
io << "\n\n "
io << " Backtrace ".colorize.bold.black.on_white
io << "\n"
backtrace.each do |trace_line|
trace_line = trace_line.colorize.dim unless trace_line.starts_with?(/src|spec/)
io << "\n #{trace_line}"
end
io << "\n"
end
end
end
Expand Down