mirror of
https://github.com/samsonjs/grape_logging.git
synced 2026-04-15 12:25:46 +00:00
* Add rubocop with a todo list * Run rubocop as part of the CI workflow * Autofix for RuboCop: Layout/ElseAlignment * Regen autogen, removing all claude-scratchpad/* * Autofix RuboCop Layout/EmptyLine * AutoFix RuboCop Style/ZeroLengthPredicate * AutoFix RuboCop Layout/EmptyLinesAroundAccessModifier * AutoFix RuboCop Layout/EmptyLinesAroundMethodBody * AutoFix RuboCop Layout/EmptyLinesAroundBlockBody * AutoFix RuboCop Layout/EmptyLinesAroundClassBody * AutoFix RuboCop Layout/EndAlignment * AutoFix RuboCop Layout/ExtraSpacing * AutoFix RuboCop Layout/FirstHashElementIndentation * AutoFix RuboCop Layout/HashAlignment * AutoFix RuboCop Layout/IndentationWidth * Regenerate todo * AutoFix RuboCop Layout/SpaceBeforeBlockBraces * AutoFix RuboCop Layout/SpaceBeforeComma * AutoFix RuboCop Layout/SpaceInsideBlockBraces * AutoFix RuboCop Layout/SpaceInsideHashLiteralBraces * AutoFix RuboCop Layout/TrailingEmptyLines * Update ci.yml Co-authored-by: Pieter Oliver <68863060+pieterocp@users.noreply.github.com> * WIP: Remove claude-scratchpad cruft from rubocop todo * Specify files explicity in rubocop rake task * Pin rubocop version to 1.77.0 * AutoFix RuboCop Style/TrailingCommaInHashLiteral * AutoFix RuboCop Style/StringLiteralsInInterpolation * AutoFix RuboCop Style/StringLiterals * AutoFix RuboCop Style/RescueStandardError * AutoFix RuboCop Style/RedundantPercentQ * AutoFix RuboCop Style/RedundantConstantBase * AutoFix RuboCop Style/QuotedSymbols * AutoFix RuboCop Style/PercentLiteralDelimiters * AutoFix RuboCop Style/ParallelAssignment * AutoFix RuboCop Style/MultilineIfModifier * AutoFix RuboCop Style/Lambda * AutoFix RuboCop Style/IfUnlessModifier * AutoFix RuboCop Style/HashSyntax * AutoFix RuboCop Style/GuardClause * AutoFix RuboCop Style/ExpandPathArguments * AutoFix RuboCop Style/BlockDelimiters * AutoFix RuboCop Style/BlockComments * AutoFix RuboCop Lint/UnusedMethodArgument * AutoFix RuboCop Lint/SymbolConversion * Fix auto-gen formatting + RuboCop AutoFix Style/ModuleFunction * Code style --------- Co-authored-by: Pieter Oliver <pieter.oliver@nourishcare.com> Co-authored-by: Pieter Oliver <68863060+pieterocp@users.noreply.github.com>
57 lines
1.9 KiB
Ruby
57 lines
1.9 KiB
Ruby
require 'rack/utils'
|
|
|
|
module GrapeLogging
|
|
module Formatters
|
|
class Rails
|
|
def call(severity, datetime, _, data)
|
|
if data.is_a?(String)
|
|
"#{severity[0..0]} [#{datetime}] #{severity} -- : #{data}\n"
|
|
elsif data.is_a?(Exception)
|
|
"#{severity[0..0]} [#{datetime}] #{severity} -- : #{format_exception(data)}\n"
|
|
elsif data.is_a?(Hash)
|
|
format_hash(data)
|
|
else
|
|
"#{data.inspect}\n"
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def format_exception(exception)
|
|
backtrace_array = (exception.backtrace || []).map { |line| "\t#{line}" }
|
|
|
|
[
|
|
"#{exception.message} (#{exception.class})",
|
|
backtrace_array.join("\n")
|
|
].reject { |line| line == '' }.join("\n")
|
|
end
|
|
|
|
def format_hash(hash)
|
|
# Create Rails' single summary line at the end of every request, formatted like:
|
|
# Completed 200 OK in 958ms (Views: 951.1ms | ActiveRecord: 3.8ms)
|
|
# See: actionpack/lib/action_controller/log_subscriber.rb
|
|
|
|
message = ''
|
|
additions = []
|
|
status = hash.delete(:status)
|
|
params = hash.delete(:params)
|
|
|
|
total_time = hash[:time] && hash[:time][:total] && hash[:time][:total].round(2)
|
|
view_time = hash[:time] && hash[:time][:view] && hash[:time][:view].round(2)
|
|
db_time = hash[:time] && hash[:time][:db] && hash[:time][:db].round(2)
|
|
|
|
additions << "Views: #{view_time}ms" if view_time
|
|
additions << "DB: #{db_time}ms" if db_time
|
|
|
|
message << " Parameters: #{params.inspect}\n" if params
|
|
|
|
message << "Completed #{status} #{::Rack::Utils::HTTP_STATUS_CODES[status]} in #{total_time}ms"
|
|
message << " (#{additions.join(' | '.freeze)})" unless additions.empty?
|
|
message << "\n"
|
|
message << "\n" if defined?(::Rails.env) && ::Rails.env.development?
|
|
|
|
message
|
|
end
|
|
end
|
|
end
|
|
end
|