Browse Source

rails redirect

main
pvincent 1 month ago
parent
commit
78ac1cae1b
  1. 59
      lib/semantic/ansi_formatter.rb
  2. 28
      lib/semantic/log_subscriber.rb

59
lib/semantic/ansi_formatter.rb

@ -83,64 +83,7 @@ module Semantic
end end
def alter(log) def alter(log)
if log.name == 'Rails'
if log.message
log.message.lstrip!
log.message.chomp!('')
if log.message.starts_with?('Started')
rails = '╓─╖'
before = 1
if @memory
rails = "#{@memory}"
before = 0
end
log.dimensions = Semantic::AnsiDimensions.new(rails:, before:)
@memory = nil
log.message = two_captures_last_as_bold(log.message, /(^Started \w* )"(.*?)"/)
elsif log.message.starts_with?('Parameters')
parameters = log.message.match(/Parameters: ({.*}$)/).match(1)
parameters = JSON.parse(parameters.gsub('=>', ':'), symbolize_names: true)
parameters = parameters.ai(ruby19_syntax: true, plain: true, multiline: false)
parameters = parameters.gsub(/\w*:/) { |key| "#{TEXT_GRAY_200}#{key.chop}#{color_map[:debug]}:" }
parameters = parameters.gsub(/: ".*?"/) do |value|
": \"#{TEXT_YELLOW}#{value[3..-2]}#{color_map[:debug]}\""
end
log.message = "Parameters: #{parameters}"
log.level = :debug
elsif log.message.starts_with?('Completed')
m1, m2, m3, m4 = log.message.match(/^Completed (\d+) (.*) in (\d+\.?\d*)ms (.*)$/).captures
http_code = m1.to_i
duration = m3.to_f
duration = duration > TOTAL_RENDERED_VIEW_DURATION ? " in #{m3}ms #{m4}" : ''
log.message = "Completed #{BOLD}#{http_code}#{CLEAR} #{m2}#{duration}"
if http_code / 100 == 2
log.dimensions = Semantic::AnsiDimensions.new(rails: TERMINUS_STRING, after: 1)
elsif http_code / 100 == 3
@memory = '║'
log.dimensions = Semantic::AnsiDimensions.new(rails: "#{@memory}")
elsif http_code / 100 == 4
log.dimensions = Semantic::AnsiDimensions.new(rails: '╙╨╜')
elsif http_code / 100 == 5
log.dimensions = Semantic::AnsiDimensions.new(rails: "#{draw_fatal}")
end
elsif log.message =~ /^(Processing|Parameters)/
log.level = :debug
if log.message =~ /^Processing/
log.message = two_captures_last_as_bold(log.message, /(^Processing by \w*#\w* as )(.*)/)
end
elsif log.message =~ /Redirected/
log.level = :debug
@regex_redirected ||= build_regex_redirected # lazy building
log.message = two_captures_last_as_bold(log.message, @regex_redirected)
end
elsif log.exception
log.dimensions = Semantic::AnsiDimensions.new(
rails: "#{draw_fatal(log.level.to_s.chr.upcase)}",
after: 1,
terminus: true
)
end
elsif log.name =~ /^(ActionView|ActiveRecord)::Base/
if log.name =~ /^(ActionView|ActiveRecord)::Base/
log.level = :debug log.level = :debug
log.message.lstrip! log.message.lstrip!
if log.name == 'ActiveRecord::Base' if log.name == 'ActiveRecord::Base'

28
lib/semantic/log_subscriber.rb

@ -2,10 +2,11 @@ module Semantic
class LogSubscriber < ActiveSupport::LogSubscriber class LogSubscriber < ActiveSupport::LogSubscriber
include SemanticLogger::Loggable include SemanticLogger::Loggable
include AnsiColors include AnsiColors
def logger = SemanticLogger['IRails']
def logger = SemanticLogger['Rails']
INTERNAL_PARAMS = %i[controller action format _method only_path].freeze INTERNAL_PARAMS = %i[controller action format _method only_path].freeze
DEFAULT_DEV_HOSTS = ['127.0.0.1', 'localhost'].freeze DEFAULT_DEV_HOSTS = ['127.0.0.1', 'localhost'].freeze
TERMINUS_STRING = '╙─╜'.freeze
def initialize(session_key) def initialize(session_key)
@session_key = session_key @session_key = session_key
@ -20,7 +21,13 @@ module Semantic
SemanticLogger.tagged(session_value) do SemanticLogger.tagged(session_value) do
request = event.payload[:request] request = event.payload[:request]
path = colorize(request.filtered_path, BOLD) path = colorize(request.filtered_path, BOLD)
logger.info("Started #{request.raw_request_method} #{path}")
dimensions = Semantic::AnsiDimensions.new(rails: '╓─╖', before: 1)
if defined?(@previously_redirect) && @previously_redirect
dimensions = Semantic::AnsiDimensions.new(rails: '╓║╖', before: 0)
@previously_redirect = false
end
logger.info("Started #{request.raw_request_method} #{path}", dimensions:)
format = event.payload[:format] format = event.payload[:format]
format = format.to_s.upcase if format.is_a?(Symbol) format = format.to_s.upcase if format.is_a?(Symbol)
@ -60,7 +67,21 @@ module Semantic
logger.debug process_duration(event, additions) logger.debug process_duration(event, additions)
end end
logger.info("Completed #{colorize(status, BOLD)} #{Rack::Utils::HTTP_STATUS_CODES[status]}")
status_family = status / 100
dimensions = case status_family
when 2
Semantic::AnsiDimensions.new(rails: TERMINUS_STRING, after: 1)
when 3
Semantic::AnsiDimensions.new(rails: '╙║╜')
when 4
Semantic::AnsiDimensions.new(rails: '╙╨╜')
when 5
Semantic::AnsiDimensions.new(rails: '╙║╜')
end
logger.info("Completed #{colorize(status, BOLD)} #{Rack::Utils::HTTP_STATUS_CODES[status]}", dimensions:)
logger.info(' ', dimensions: Semantic::AnsiDimensions.new(rails: ' ║ ')) if status_family == 3
logger.info(' ', dimensions: Semantic::AnsiDimensions.new(rails: '╓║╖')) if status_family == 5
end end
end end
@ -69,6 +90,7 @@ module Semantic
location = capture_path(event.payload[:location]) location = capture_path(event.payload[:location])
logger.debug("Redirected to #{colorize(location, BOLD)}") logger.debug("Redirected to #{colorize(location, BOLD)}")
end end
@previously_redirect = true
end end
def any_hook(event) def any_hook(event)

Loading…
Cancel
Save