From 20411219caddb4d346bca037fa07255def91a97e Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 18 Jan 2024 13:37:11 +0400 Subject: [PATCH] logger refactored + TODO --- .vscode/extensions.json | 3 +- .vscode/settings.json | 1 - README.md | 2 ++ app/controllers/scores_controller.rb | 1 + lib/formatters/basic_formatter.rb | 53 +++++++++++++++------------- 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 68f167e..1cc1449 100644 --- a/.vscode/extensions.json +++ b/.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" ] } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 3228fdf..3a55e5f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,5 +11,4 @@ "emmet.includeLanguages": { "erb": "html" }, - "rubyLsp.enableExperimentalFeatures": true } \ No newline at end of file diff --git a/README.md b/README.md index b9b1c80..1648a2a 100644 --- a/README.md +++ b/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 diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index dabb1b4..bd37f54 100644 --- a/app/controllers/scores_controller.rb +++ b/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 }) diff --git a/lib/formatters/basic_formatter.rb b/lib/formatters/basic_formatter.rb index 7daf88e..67cbb79 100644 --- a/lib/formatters/basic_formatter.rb +++ b/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