Browse Source

basic_formatter multiline

main
pvincent 3 months ago
parent
commit
35efb62e61
  1. 4
      app/controllers/scores_controller.rb
  2. 6
      lib/semantic/abstract_formatter.rb
  3. 8
      lib/semantic/ansi_colors.rb
  4. 85
      lib/semantic/basic_formatter.rb
  5. 6
      lib/semantic/fancy_formatter.rb
  6. 2
      lib/semantic/subscribers/active_record.rb

4
app/controllers/scores_controller.rb

@ -7,6 +7,10 @@ class ScoresController < ApplicationController
def index
logger.info('inside controller2')
logger.info("multiline1\nmultiline2\nmultiline3")
logger.warn('this is a warning')
logger.error('this is an error')
# logger.fatal('this is a fatal')
@q = Score.all.ransack(q_params)
@pagy, @scores = pagy(@q.result)
end

6
lib/semantic/abstract_formatter.rb

@ -30,6 +30,12 @@ module Semantic
"#{clazz}#{message}#{backtrace}"
end
def origin
true_class = log.name == log.name.upcase_first
taint = true_class ? TEXT_CYAN : TEXT_GRAY_400
colorize(centerize(log.name), taint)
end
def ansi_trace(trace, symbol)
match = trace.match(/(↳ )?(.*:\d+)(:in `)?(.*'?)/) # only m2(=file) and m4(=optional function) are useful
return trace unless match

8
lib/semantic/ansi_colors.rb

@ -18,10 +18,6 @@ module Semantic
TEXT_CYAN = "\e[36m".freeze
TEXT_WHITE = "\e[37m".freeze
# TEXT EXTRA COLOR NAME
TEXT__PINK = "\e[38;5;175m".freeze
TEXT__ORANGE = "\e[38;5;202m".freeze
# TEXT GRAY SHADES
TEXT_GRAY_800 = "\e[38;5;232m".freeze
TEXT_GRAY_700 = "\e[38;5;233m".freeze
@ -32,6 +28,10 @@ module Semantic
TEXT_GRAY_200 = "\e[38;5;249m".freeze
TEXT_GRAY_100 = "\e[38;5;252m".freeze
# TEXT EXTRA COLOR NAME
TEXT_COLOR_PINK = "\e[38;5;175m".freeze
TEXT_COLOR_ORANGE = "\e[38;5;202m".freeze
# DARK TEXT
DARK_TEXT_BLACK = "\e[90m".freeze
DARK_TEXT_RED = "\e[91m".freeze

85
lib/semantic/basic_formatter.rb

@ -3,73 +3,56 @@ module Semantic
class BasicFormatter < AbstractFormatter
ANSI_REVERSED_WARNING = "\e[0;30;43m".freeze
ANSI_REVERSED_ERROR = "\e[1;30;41m".freeze
ANSI_GRAY = "\e[90m".freeze
MAX_LENGTH_LINE = 255
PRIME = '▍'.freeze
CONTINATION = '▕'.freeze
def initialize
super(time_format: '%H:%M:%S',
color_map: ColorMap.new(
debug: ANSI_GRAY,
info: SemanticLogger::AnsiColors::GREEN,
warn: SemanticLogger::AnsiColors::YELLOW
))
@time_format = nil if File.exist?(File.join(Rails.root, 'Procfile.dev'))
super(color_map: ColorMap.new(
debug: TEXT_GRAY_400,
info: TEXT_GREEN,
warn: TEXT_YELLOW,
error: TEXT_COLOR_ORANGE,
fatal: TEXT_RED
))
@time_format = File.exist?(File.join(Rails.root, 'Procfile.dev')) ? nil : '%H:%M:%S'
end
def exception
super_exception = super
"EXCEPTION -- #{super_exception}" if super_exception
def call(log, logger)
self.color = color_map[log.level]
self.log = log
self.logger = logger
wrap_level(MAX_LENGTH_LINE, duration, named_tags, message, payload, exception)
rescue StandardError => e
puts "Error during formatting: #{e.message}"
puts e.backtrace.join("\n")
end
def time
"#{color}#{format_time(log.time)}#{color_map.clear}" if time_format
private
def wrap_level(length, *items)
prime = "#{build_prime} "
continuation = "#{build_continuation} "
items
.map { |item| Semantic::AnsiWrapper.wrap(item, length, prime, continuation) }
.compact.join("\n")
end
def message
return unless log.message
prefix = "#{color}--#{color_map.clear}"
case log.level
when :info
message = log.message
message = message&.rstrip if log.name == 'Rails' && message.starts_with?('Completed')
if log.name == 'Rails' && message.starts_with?('Started')
message = message.split('for')[0]
puts '' if Rails.env.development?
end
if log.name == 'Rails' || log.name == 'ActionView::Base'
"#{prefix} #{ANSI_GRAY}#{message}#{color_map.clear}"
else
"#{prefix} #{SemanticLogger::AnsiColors::WHITE}#{message}#{color_map.clear}"
end
when :warn
"#{prefix} #{ANSI_REVERSED_WARNING}#{log.message}#{color_map.clear}"
when :error, :fatal
"#{prefix} #{ANSI_REVERSED_ERROR}#{log.message}#{color_map.clear}"
else
"#{prefix} #{color}#{log.message}#{color_map.clear}"
end
log.level == :info ? colorize(log.message, TEXT_WHITE) : colorize(log.message)
end
def process_info
fname = file_name_and_line
"#{color}[#{fname}]#{color_map.clear}" if fname
colorize fname if fname
end
def call(log, logger)
self.color = color_map[log.level]
self.log = log
self.logger = logger
if @time_format
[time, level, process_info, tags, named_tags, duration, name, message, payload,
exception].compact.join(' ')
else
[tags, named_tags, duration, name, message, payload, exception].compact.join(' ')
end
rescue StandardError => e
puts "Error during formatting: #{e.message}"
puts e.backtrace.join("\n")
end
def time = @time_format ? colorize(format_time(log.time), color) : nil
def build_prefix(char) = [time, tags, origin, colorize(char)].compact.join ' '
def build_prime = build_prefix(PRIME)
def build_continuation = build_prefix(CONTINATION)
end
end

6
lib/semantic/fancy_formatter.rb

@ -109,12 +109,6 @@ module Semantic
def build_prefix(char) = "#{tags} #{origin}#{colorize(char)}"
def build_terminus = "#{tags} #{origin} #{TERMINUS_STRING} "
def origin
true_class = log.name == log.name.upcase_first
taint = true_class ? TEXT_CYAN : TEXT_GRAY_400
colorize(centerize(log.name), taint)
end
def build_dimensions(dimensions)
"#{tags} #{origin} #{dimensions.rails} "
end

2
lib/semantic/subscribers/active_record.rb

@ -38,7 +38,7 @@ module Semantic
name = "Read #{row_count} #{model.pluralize(row_count)}"
end
when 'Update', 'Create', 'Destroy'
statement_taint = TEXT__PINK
statement_taint = TEXT_COLOR_PINK
name = "#{category} #{model}"
category_taint = TRANSACTION_TAINT
increment_transaction_local(event.payload[:transaction].uuid, category.downcase.to_sym, event.duration)

Loading…
Cancel
Save