From a7a17bcf2b07b475b44b176b32ef002001997521 Mon Sep 17 00:00:00 2001 From: pvincent Date: Sun, 22 Sep 2024 22:23:35 +0400 Subject: [PATCH] basic_formatter tag --- app/controllers/hot_controller.rb | 2 +- app/controllers/scores_controller.rb | 1 + config/initializers/instrumentalizing.rb | 3 +++ lib/semantic/abstract_formatter.rb | 16 ++++++++++++++++ lib/semantic/basic_formatter.rb | 7 +++++-- lib/semantic/fancy_formatter.rb | 14 -------------- lib/semantic/instrumentalizer.rb | 3 ++- lib/semantic/subscribers/action_controller.rb | 5 +++-- 8 files changed, 31 insertions(+), 20 deletions(-) diff --git a/app/controllers/hot_controller.rb b/app/controllers/hot_controller.rb index 7f075e5..0b8718a 100644 --- a/app/controllers/hot_controller.rb +++ b/app/controllers/hot_controller.rb @@ -6,6 +6,6 @@ class HotController < ApplicationController logger.info(__method__) # logger.info(ActiveSupport::LogSubscriber.subscribers.map(&:patterns)) - logger.info(ActiveSupport::Notifications.notifier.listeners_for('.*\.action_controller')) + # logger.info(ActiveSupport::Notifications.notifier.listeners_for('.*\.action_controller')) end end diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index 959b1c6..67f8b24 100644 --- a/app/controllers/scores_controller.rb +++ b/app/controllers/scores_controller.rb @@ -6,6 +6,7 @@ class ScoresController < ApplicationController def index logger.info('inside controller2') + logger.info("multiline1\nmultiline2\nmultiline3") @q = Score.all.ransack(q_params) @pagy, @scores = pagy(@q.result) end diff --git a/config/initializers/instrumentalizing.rb b/config/initializers/instrumentalizing.rb index b7baf66..ac53375 100644 --- a/config/initializers/instrumentalizing.rb +++ b/config/initializers/instrumentalizing.rb @@ -1,5 +1,8 @@ return unless Rails.application.server? +Rails.application.config.x.action_controller.main_session_tag = 'toto1' +Rails.application.config.x.action_controller.main_session_tag_fixed_length = 8 + def build_instrumentation_config { action_controller: true, diff --git a/lib/semantic/abstract_formatter.rb b/lib/semantic/abstract_formatter.rb index 2268764..e40dcaf 100644 --- a/lib/semantic/abstract_formatter.rb +++ b/lib/semantic/abstract_formatter.rb @@ -2,6 +2,9 @@ module Semantic # Abstract colorized formatter class AbstractFormatter < SemanticLogger::Formatters::Color include AnsiColors + TAG_NONE = ''.freeze + CENTER_SIZE = 20 + TAG_FIXED_LENGTH = Rails.application.config.x.action_controller.main_session_tag_fixed_length || 10 private @@ -41,5 +44,18 @@ module Semantic traces = items.map { |item| ansi_trace(item, '➟') } "\n#{traces.join("\n")}" end + + def tags + first_tag, taint = log.tags.empty? ? [TAG_NONE, CLEAR] : [log.tags.first, BG_GRAY + DARK_TEXT_YELLOW] + colorize(centerize(first_tag, TAG_FIXED_LENGTH), taint) + end + + def named_tags + return if log.named_tags.empty? + + log.named_tags + end + + def centerize(text, max_length = CENTER_SIZE) = text.reverse.truncate(max_length).reverse.center(max_length) end end diff --git a/lib/semantic/basic_formatter.rb b/lib/semantic/basic_formatter.rb index 718e0ae..232de2c 100644 --- a/lib/semantic/basic_formatter.rb +++ b/lib/semantic/basic_formatter.rb @@ -15,6 +15,11 @@ module Semantic @time_format = nil if File.exist?(File.join(Rails.root, 'Procfile.dev')) end + def exception + super_exception = super + "EXCEPTION -- #{super_exception}" if super_exception + end + def time "#{color}#{format_time(log.time)}#{color_map.clear}" if time_format end @@ -46,8 +51,6 @@ module Semantic end end - def tags; end - def process_info fname = file_name_and_line "#{color}[#{fname}]#{color_map.clear}" if fname diff --git a/lib/semantic/fancy_formatter.rb b/lib/semantic/fancy_formatter.rb index 04e1995..9fc9923 100644 --- a/lib/semantic/fancy_formatter.rb +++ b/lib/semantic/fancy_formatter.rb @@ -5,8 +5,6 @@ require 'json' module Semantic # wraps meanwhile takes care of ansi colors class FancyFormatter < AbstractFormatter - TAG_NONE = ''.freeze - CENTER_SIZE = 20 FOREMAN_PREFIX_LENGTH = 18 FAILOVER_WRAP = 80 CHAR_FATAL = '⯶'.freeze @@ -52,17 +50,6 @@ module Semantic private - def tags - first_tag, taint = log.tags.empty? ? [TAG_NONE, CLEAR] : [log.tags.first, BG_GRAY + DARK_TEXT_YELLOW] - colorize(centerize(first_tag, 10), taint) - end - - def named_tags - return if log.named_tags.empty? - - log.named_tags - end - def build_regex_redirected dev_port = Rails.application.routes.default_url_options[:port] dev_hosts = ['127.0.0.1', 'localhost', Rails.application.routes.default_url_options[:host]].uniq.join('|') @@ -121,7 +108,6 @@ module Semantic def draw_fatal(char = CHAR_FATAL) = BG_MAGENTA + BOLD + TEXT_WHITE + char + CLEAR def build_prefix(char) = "#{tags} #{origin} ╣#{colorize(char)}╠ " def build_terminus = "#{tags} #{origin} #{TERMINUS_STRING} " - def centerize(text, max_length = CENTER_SIZE) = text.reverse.truncate(max_length).reverse.center(max_length) def origin true_class = log.name == log.name.upcase_first diff --git a/lib/semantic/instrumentalizer.rb b/lib/semantic/instrumentalizer.rb index 6f008bc..4961c3f 100644 --- a/lib/semantic/instrumentalizer.rb +++ b/lib/semantic/instrumentalizer.rb @@ -64,9 +64,10 @@ module Semantic def build_log_subscriber_from(event_group) classname = event_group.to_s.camelize + options = Rails.application.config.x.send(event_group).to_h case classname when 'ActionController' - Semantic::Subscribers.const_get(classname).new('toto1') # FIXME: externalize session_key + Semantic::Subscribers.const_get(classname).new(**options) else Semantic::Subscribers.const_get(classname).new end diff --git a/lib/semantic/subscribers/action_controller.rb b/lib/semantic/subscribers/action_controller.rb index f2d728e..1731c42 100644 --- a/lib/semantic/subscribers/action_controller.rb +++ b/lib/semantic/subscribers/action_controller.rb @@ -8,9 +8,10 @@ module Semantic DEFAULT_DEV_HOSTS = ['127.0.0.1', 'localhost'].freeze TERMINUS_STRING = '╙─╜'.freeze - def initialize(session_key) + # options = { main_session_tag: 'ANY_SESSION_KEY' } + def initialize(**options) super(:controller) - @session_key = session_key + @session_key = options[:main_session_tag] @transactions = {} end