Browse Source

almost ok, payload with blank message not yet pixel perfect

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

14
app/controllers/scores_controller.rb

@ -5,13 +5,15 @@ class ScoresController < ApplicationController
# GET /scores # GET /scores
def index def index
@scores = Score.all @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.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 end
# GET /scores/1 # 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) elsif exception.nil? && payload && payload.respond_to?(:backtrace) && payload.respond_to?(:message)
exception = payload exception = payload
payload = nil payload = nil
elsif message && !message.is_a?(String)
payload = message
message = "\e[0m"
# message = ' '
# message = nil
# elsif payload && !payload.is_a?(Hash) # elsif payload && !payload.is_a?(Hash)
# message = message.nil? ? payload : "#{message} -- #{payload}" # message = message.nil? ? payload : "#{message} -- #{payload}"
# payload = nil # payload = nil

53
lib/formatters/basic_formatter.rb

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

Loading…
Cancel
Save