Browse Source

logger refactored + TODO

pagy
pvincent 11 months ago
parent
commit
20411219ca
  1. 3
      .vscode/extensions.json
  2. 1
      .vscode/settings.json
  3. 2
      README.md
  4. 1
      app/controllers/scores_controller.rb
  5. 53
      lib/formatters/basic_formatter.rb

3
.vscode/extensions.json

@ -2,6 +2,7 @@
"recommendations": [
"shopify.ruby-lsp",
"aki77.rails-db-schema",
"yzhang.markdown-all-in-one"
"yzhang.markdown-all-in-one",
"jgclark.vscode-todo-highlight"
]
}

1
.vscode/settings.json

@ -11,5 +11,4 @@
"emmet.includeLanguages": {
"erb": "html"
},
"rubyLsp.enableExperimentalFeatures": true
}

2
README.md

@ -44,4 +44,6 @@ end
* [x] Ruby-lsp-rails
* [x] Ruby Db Schema
* [x] Markdown All in One
* [ ] TODO Highliter
* [ ] run ` rails notes` to show up any TODO or FIXME

1
app/controllers/scores_controller.rb

@ -4,6 +4,7 @@ class ScoresController < ApplicationController
# GET /scores
def index
# TODO: my todo list is beginning
@scores = Score.all
logger.debug 'this is a debug message'
logger.debug({ one: 1, two: 2 })

53
lib/formatters/basic_formatter.rb

@ -4,7 +4,7 @@ require 'io/console'
require 'amazing_print'
# Opinioned Rails custom formatter
class BasicFormatter < SemanticLogger::Formatters::Color
class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metrics/ClassLength
NAME_MAX_SIZE = 25
TERMINAL_PREFIX = ENV['TERMINAL_PREFIX'].to_i || 0
CONTENT_PREFIX = ' '.freeze
@ -51,20 +51,9 @@ class BasicFormatter < SemanticLogger::Formatters::Color
def message
return unless log.message
message = log.message
if log.name == 'Rails' && message.starts_with?('Completed')
message.rstrip!
message += "\n" unless message.starts_with?('Completed 5')
end
wrap_length = compute_useful_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]}#{space_prefix}#{message}#{color_map.clear}"
message = transform_message(log)
message = wrap_message(message)
"#{CONTENT_COLOR_MAP[log.level]}#{message}#{color_map.clear}"
end
def payload
@ -74,7 +63,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color
first_line = lines.shift
space_prefix = first_line.match(/^\s*/)
lines = lines.map do |l|
"#{before_message(true)}#{space_prefix}#{l}"
"#{before_message(wrapped: true)}#{space_prefix}#{l}"
end
result = lines.unshift(first_line).join("\n")
result.sub!(/\s*/) { |m| '-' * m.length }
@ -111,11 +100,6 @@ class BasicFormatter < SemanticLogger::Formatters::Color
end
def call(log, logger)
unless log.message.is_a?(String)
log.payload = log.message
log.message = nil
end
self.color = color_map[log.level]
self.log = log
self.logger = logger
@ -125,13 +109,34 @@ class BasicFormatter < SemanticLogger::Formatters::Color
private
def wrap_message(message)
message, space_prefix = split_spaces_in_front(message)
message = Wrapper.wrap("#{CONTENT_COLOR_MAP[log.level]}#{message}",
before_message(wrapped: true) + space_prefix.to_s,
compute_useful_length - space_prefix.length)
"#{space_prefix}#{message}"
end
def split_spaces_in_front(message)
md = message.match(/^\s*/)
[md.post_match, md.match(0)]
end
def transform_message(log)
return log.message unless log.name == 'Rails' && log.message.starts_with?('Completed')
message = log.message.rstrip!
message += "\n" unless message.starts_with?('Completed 5')
message
end
def compute_useful_length
wrap_length = IO.console.winsize[1] - TERMINAL_PREFIX - before_message.length + CONTENT_PREFIX.length + 12
IO.console.winsize[1] - TERMINAL_PREFIX - before_message.length + CONTENT_PREFIX.length + 12
rescue StandardError
wrap_length = 100 # FIXME: CONSTANTIZE, only useful in DEBUGGER, no IO.console detected!
100 # FIXME: CONSTANTIZE, only useful in DEBUGGER, no IO.console detected!
end
def before_message(wrapped = false)
def before_message(wrapped: false)
[name, wrapped ? continuation : level, tags, named_tags, duration].compact.join(' ') + CONTENT_PREFIX
end

Loading…
Cancel
Save