Browse Source

log_subscriber with tags

main
pvincent 2 months ago
parent
commit
16363d9df9
  1. 1
      app/controllers/hot_controller.rb
  2. 23
      lib/semantic/ansi_formatter.rb
  3. 1
      lib/semantic/dev_loader.rb
  4. 21
      lib/semantic/log_subscriber.rb

1
app/controllers/hot_controller.rb

@ -1,5 +1,6 @@
class HotController < ApplicationController class HotController < ApplicationController
def index def index
session[:toto1] = 'TOTO1'
logger.info('callee') logger.info('callee')
logger.info(__callee__) logger.info(__callee__)
logger.info(__method__) logger.info(__method__)

23
lib/semantic/ansi_formatter.rb

@ -28,7 +28,6 @@ module Semantic
end end
def call(log, logger) def call(log, logger)
# puts 'ok2'
log = alter(log) log = alter(log)
self.log = log self.log = log
@ -46,6 +45,17 @@ module Semantic
private private
def tags
first_tag = log.tags.empty? ? 'none' : log.tags.first
first_tag.truncate(CENTER_SIZE).center(CENTER_SIZE)
end
def named_tags
return if log.named_tags.empty?
log.named_tags
end
def build_regex_redirected def build_regex_redirected
dev_port = Rails.application.routes.default_url_options[:port] 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('|') dev_hosts = ['127.0.0.1', 'localhost', Rails.application.routes.default_url_options[:host]].uniq.join('|')
@ -166,13 +176,10 @@ module Semantic
log log
end end
def draw_fatal(char = CHAR_FATAL)
BG_MAGENTA + BOLD + TEXT_WHITE + char + CLEAR
end
def draw_fatal(char = CHAR_FATAL) = BG_MAGENTA + BOLD + TEXT_WHITE + char + CLEAR
def origin = colorize(centerize(log.name), TEXT_CYAN) def origin = colorize(centerize(log.name), TEXT_CYAN)
def build_prefix(char) = "#{origin}#{colorize(char)}"
def build_terminus = "#{origin} #{TERMINUS_STRING} "
def build_prefix(char) = "#{tags} #{origin}#{colorize(char)}"
def build_terminus = "#{tags} #{origin} #{TERMINUS_STRING} "
def centerize(text) = text.truncate(CENTER_SIZE).center(CENTER_SIZE) def centerize(text) = text.truncate(CENTER_SIZE).center(CENTER_SIZE)
def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}" def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}"
@ -184,7 +191,7 @@ module Semantic
end end
def build_dimensions(dimensions) def build_dimensions(dimensions)
"#{origin} #{dimensions.rails} "
"#{tags} #{origin} #{dimensions.rails} "
end end
def compute_useful_length def compute_useful_length

1
lib/semantic/dev_loader.rb

@ -7,6 +7,7 @@ module Semantic
append_subscriber_once append_subscriber_once
end end
append_ansi_formatter append_ansi_formatter
# Rails.logger.level = :error
end end
private private

21
lib/semantic/log_subscriber.rb

@ -1,7 +1,26 @@
module Semantic module Semantic
class LogSubscriber < ActiveSupport::LogSubscriber class LogSubscriber < ActiveSupport::LogSubscriber
include SemanticLogger::Loggable
# def logger = SemanticLogger['Rails']
SESSION_KEY = 'toto1'.freeze
def initialize
end
def start_processing(event) def start_processing(event)
logger.warn("MyStarted #{event.payload[:controller]} - #{event.payload[:action]}", event.payload[:request])
request = event.payload[:request]
rack_session = event.payload[:headers]['rack.session']
session_value = rack_session.fetch(SESSION_KEY, 'EMPTY')
format = event.payload[:format]
format = format.to_s.upcase if format.is_a?(Symbol)
format = '*/*' if format.nil?
SemanticLogger.tagged(session_value) do
logger.info("Started #{request.raw_request_method} #{request.filtered_path}")
logger.debug("Processing by #{event.payload[:controller]}##{event.payload[:action]} as #{format}")
end
end end
end end
end end
Loading…
Cancel
Save