Browse Source

no more ansi_filter

main
pvincent 7 months ago
parent
commit
7f22754bf9
  1. 8
      config/environments/development.rb
  2. 53
      lib/formatters/ansi_filter.rb
  3. 52
      lib/formatters/ansi_formatter.rb

8
config/environments/development.rb

@ -82,12 +82,12 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength
config.rails_semantic_logger.add_file_appender = false
require Rails.root.join('lib', 'formatters', 'ansi_formatter')
require Rails.root.join('lib', 'formatters', 'ansi_filter')
formatter = AnsiFormatter.new
config.semantic_logger.add_appender(io: $stdout,
formatter: AnsiFormatter.new,
filter: ->(log) { !AnsiFilter.reject(log) })
formatter:,
filter: ->(log) { !formatter.reject(log) })
# require Rails.root.join('lib', 'formatters', 'basic_formatter')
# require Rails.root.join('lib', 'old', 'basic_formatter')
# config.semantic_logger.add_appender(io: $stdout,
# formatter: BasicFormatter.new,
# filter: BasicFormatter::EXCLUDE_LAMBDA)

53
lib/formatters/ansi_filter.rb

@ -1,53 +0,0 @@
require_relative 'ansi_dimensions'
require_relative 'ansi_colors'
# filter log message
module AnsiFilter
$memory = nil
CHAR_FATAL = '⯶'.freeze
TERMINUS_STRING = '╙─╜'.freeze
def self.reject(log)
# log.name == 'ScoresController' || false
false
end
def self.alter(log)
if log.name == 'Rails'
if log.message
log.message.chomp!('')
if log.message.starts_with?('Started')
rails = '╓─╖'
before = 1
if $memory
rails = "#{$memory}"
before = 0
end
log.dimensions = AnsiDimensions.new(rails:, before:)
$memory = nil
elsif log.message.starts_with?('Completed 2')
log.dimensions = AnsiDimensions.new(rails: TERMINUS_STRING, after: 1)
elsif log.message.starts_with?('Completed 3')
$memory = '║'
log.dimensions = AnsiDimensions.new(rails: "#{$memory}")
elsif log.message.starts_with?('Completed 4')
log.dimensions = AnsiDimensions.new(rails: '╙╨╜')
elsif log.message.starts_with?('Completed 5')
log.dimensions = AnsiDimensions.new(rails: "#{draw_fatal}")
end
elsif log.exception
log.dimensions = AnsiDimensions.new(
rails: "#{draw_fatal(log.level.to_s.chr.upcase)}",
after: 1,
terminus: true
)
end
end
log
end
private_class_method def self.draw_fatal(char = CHAR_FATAL)
AnsiColors::BG_MAGENTA + AnsiColors::BOLD + AnsiColors::TEXT_WHITE + char + AnsiColors::CLEAR
end
end

52
lib/formatters/ansi_formatter.rb

@ -1,5 +1,6 @@
require_relative 'ansi_wrapper'
require_relative 'ansi_colors'
require_relative 'ansi_dimensions'
require 'io/console'
require 'amazing_print'
@ -9,9 +10,13 @@ require 'json'
class AnsiFormatter < SemanticLogger::Formatters::Color
include AnsiColors
$memory = nil
CENTER_SIZE = 20
FOREMAN_PREFIX_LENGTH = 18
FAILOVER_WRAP = 80
CHAR_FATAL = '⯶'.freeze
TERMINUS_STRING = '╙─╜'.freeze
def initialize
super(color_map:
@ -25,7 +30,7 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
end
def call(log, logger)
log = AnsiFilter.alter(log)
log = alter(log)
self.log = log
self.logger = logger
@ -34,11 +39,54 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
wrap_level(compute_useful_length, message, payload, exception)
end
def reject(log)
log.name == 'ScoresController' || false
false
end
private
def alter(log)
if log.name == 'Rails'
if log.message
log.message.chomp!('')
if log.message.starts_with?('Started')
rails = '╓─╖'
before = 1
if $memory
rails = "#{$memory}"
before = 0
end
log.dimensions = AnsiDimensions.new(rails:, before:)
$memory = nil
elsif log.message.starts_with?('Completed 2')
log.dimensions = AnsiDimensions.new(rails: TERMINUS_STRING, after: 1)
elsif log.message.starts_with?('Completed 3')
$memory = '║'
log.dimensions = AnsiDimensions.new(rails: "#{$memory}")
elsif log.message.starts_with?('Completed 4')
log.dimensions = AnsiDimensions.new(rails: '╙╨╜')
elsif log.message.starts_with?('Completed 5')
log.dimensions = AnsiDimensions.new(rails: "#{draw_fatal}")
end
elsif log.exception
log.dimensions = AnsiDimensions.new(
rails: "#{draw_fatal(log.level.to_s.chr.upcase)}",
after: 1,
terminus: true
)
end
end
log
end
def draw_fatal(char = CHAR_FATAL)
AnsiColors::BG_MAGENTA + AnsiColors::BOLD + AnsiColors::TEXT_WHITE + char + AnsiColors::CLEAR
end
def origin = colorize(centerize(log.name), TEXT_CYAN)
def build_prefix(char) = "#{origin}#{colorize(char)}"
def build_terminus = "#{origin} #{AnsiFilter::TERMINUS_STRING} "
def build_terminus = "#{origin} #{TERMINUS_STRING} "
def centerize(text) = text.truncate(CENTER_SIZE).center(CENTER_SIZE)
def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}"
def stackisize(items) = items.length.positive? ? "\n#{items.join("\n")}" : ''

Loading…
Cancel
Save