Browse Source

drawRails

main
pvincent 6 months ago
parent
commit
0f554ad684
  1. 9
      config/environments/development.rb
  2. 22
      lib/formatters/ansi_formatter.rb

9
config/environments/development.rb

@ -81,12 +81,13 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength
config.rails_semantic_logger.rendered = true
config.rails_semantic_logger.add_file_appender = false
# config.semantic_logger.add_appender(io: $stdout,
# formatter: BasicFormatter.new,
# filter: BasicFormatter::EXCLUDE_LAMBDA)
require Rails.root.join('lib', 'formatters', 'basic_formatter')
require Rails.root.join('lib', 'formatters', 'ansi_formatter')
config.semantic_logger.add_appender(io: $stdout,
formatter: AnsiFormatter.new)
# config.semantic_logger.add_appender(io: $stdout,
# formatter: BasicFormatter.new,
# filter: BasicFormatter::EXCLUDE_LAMBDA)
end

22
lib/formatters/ansi_formatter.rb

@ -33,7 +33,7 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
self.logger = logger
self.color = color_map[log.level]
wrap_length = compute_useful_length
wrap_level(level, wrap_length, message, payload, exception)
wrap_level(wrap_length, message, payload, exception)
end
def origin
@ -59,6 +59,11 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
private
def colorize(text, tint = color) = "#{tint}#{text}#{AnsiColors::CLEAR}"
def build_prefix = "#{origin} #{colorize(draw_rails(level_char))} "
def build_continuation = "#{origin} #{colorize(draw_rails('┆'))} "
def draw_rails(char) = "#{char}"
def compute_useful_length
IO.console.winsize[1] - FOREMAN_PREFIX_LENGTH
rescue StandardError
@ -72,15 +77,18 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
backtrace.compact.join("\n")
end
def colorize(text, tint = color)
"#{tint}#{text}#{AnsiColors::CLEAR}"
def level_char
case log.level
when :info, :debug then ' '
else log.level.to_s.chr.upcase
end
end
def wrap_level(level, length, *items)
prefix = "#{origin} #{colorize('>')} "
continuation = "#{origin} #{colorize('#')} "
def wrap_level(length, *items)
prefix = build_prefix
continuation = build_continuation
items.map do |item|
AnsiWrapper.wrap(item, length, prefix, continuation)
end.compact.join("\n") # FIXME: previously was: join(' ')
end.compact.join("\n")
end
end
Loading…
Cancel
Save