Browse Source

almost ok, payload with blank message not yet pixel perfect

pagy
pvincent 11 months ago
parent
commit
821743eacb
  1. 14
      app/controllers/scores_controller.rb
  2. 5
      lib/formatters/base.rb
  3. 55
      lib/formatters/basic_formatter.rb

14
app/controllers/scores_controller.rb

@ -5,13 +5,15 @@ class ScoresController < ApplicationController
# GET /scores
def index
@scores = Score.all
logger.info 'this is an information', { 'one': 1, 'two': 2 }
logger.info 'this is an object', BigDecimal('0.0001')
# logger.debug 'this is a debug message'
logger.warn 'this is a warning'
logger.error 'this is an error', BigDecimal('0.0002')
logger.fatal 'FATAL'
logger.debug 'score are', @scores
logger.info(' ' + '*' * 200)
logger.debug({ 'one': 1, 'two': 2 })
logger.info 'this is an information', { 'four': 4, 'five': 5 }
# logger.info 'this is an object', BigDecimal('0.0001')
# logger.warn 'this is a warning'
# logger.error 'this is an error', BigDecimal('0.0002')
# logger.fatal 'FATAL'
# logger.debug 'score are', @scores
end
# GET /scores/1

5
lib/formatters/base.rb

@ -9,6 +9,11 @@ module SemanticLogger
elsif exception.nil? && payload && payload.respond_to?(:backtrace) && payload.respond_to?(:message)
exception = payload
payload = nil
elsif message && !message.is_a?(String)
payload = message
message = "\e[0m"
# message = ' '
# message = nil
# elsif payload && !payload.is_a?(Hash)
# message = message.nil? ? payload : "#{message} -- #{payload}"
# payload = nil

55
lib/formatters/basic_formatter.rb

@ -7,8 +7,7 @@ require 'amazing_print'
class BasicFormatter < SemanticLogger::Formatters::Color
NAME_MAX_SIZE = 25
TERMINAL_PREFIX = ENV['TERMINAL_PREFIX'].to_i || 0
CONTENT_PREFIX = ' '.freeze
WRAP_PREFIX = ' … '.freeze
CONTENT_PREFIX = '---'.freeze
ANSI_DEBUG = "\e[90m".freeze
ANSI_INFO = SemanticLogger::AnsiColors::GREEN
@ -57,11 +56,13 @@ class BasicFormatter < SemanticLogger::Formatters::Color
end
wrap_length = compute_useful_length
space_prefix = message.match(/^\s*/)
message = Wrapper.wrap("#{CONTENT_COLOR_MAP[log.level]}#{message}", before_message(true, space_prefix),
wrap_length - space_prefix.length)
md = message.match(/^\s*/)
space_prefix = md.match(0)
message = md.post_match
message = Wrapper.wrap("#{CONTENT_COLOR_MAP[log.level]}#{message}", before_message(true) + space_prefix.to_s,
wrap_length - md.end(0))
"#{CONTENT_COLOR_MAP[log.level]}#{message}#{color_map.clear}"
"#{CONTENT_COLOR_MAP[log.level]}#{space_prefix}#{message}#{color_map.clear}"
end
def payload
@ -69,20 +70,28 @@ class BasicFormatter < SemanticLogger::Formatters::Color
lines = log.payload.ai(ruby19_syntax: true, indent: 2).split("\n")
first_line = lines.shift
space_prefix = first_line.match(/^\s*/)
lines = lines.map do |l|
space_prefix = l.match(/^\s*/)
"#{before_message(true, space_prefix)}#{l}"
"#{before_message(true)}#{space_prefix}#{l}"
end
lines.unshift(first_line).join("\n")
result = lines.unshift(first_line).join("\n")
result.sub!(/\s*/) { |m| '-' * m.length }
result
end
def level
level = case log.level
when :info then ' '
when :debug then 'd'
else log.level.to_s.chr.upcase
end
"#{color}#{level}#{color_map.clear}"
case log.level
when :info, :debug then draw_rails ' '
else draw_rails(log.level.to_s.chr.upcase)
end
end
def draw_rails(char)
"#{color}#{char}#{color_map.clear}"
end
def continuation
draw_rails('┆')
end
def name
@ -109,8 +118,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color
self.log = log
self.logger = logger
self.log.payload =
[before_message, message, payload, exception].compact.join(' ')
before_message + [message, payload, exception].compact.join(' ')
end
private
@ -118,18 +126,11 @@ class BasicFormatter < SemanticLogger::Formatters::Color
def compute_useful_length
wrap_length = IO.console.winsize[1] - TERMINAL_PREFIX - before_message.length + CONTENT_PREFIX.length + 12
rescue StandardError
wrap_length = 100
wrap_length = 100 # FIXME: CONSTANTIZE, only useful in DEBUGGER, no IO.console detected!
end
def before_message(wrapped = false, prefix = '')
result = [name, level, tags, named_tags, duration, CONTENT_PREFIX].compact.join(' ')
if wrapped
[name, level, tags, named_tags, duration,
"#{color}#{WRAP_PREFIX}#{color_map.clear}#{prefix}"].compact.join(' ')
else
[name, level, tags, named_tags, duration,
CONTENT_PREFIX].compact.join(' ')
end
def before_message(wrapped = false)
[name, wrapped ? continuation : level, tags, named_tags, duration].compact.join(' ') + CONTENT_PREFIX
end
def backtrace(stack)

Loading…
Cancel
Save