pvincent 2 months ago
parent
commit
498554e70f
  1. 2
      bin/sprod
  2. 4
      config/environments/production.rb
  3. 34
      config/initializers/instrumentalizing.rb
  4. 4
      lib/live/definable.rb
  5. 64
      test/formatters/wrapper2_test.rb

2
bin/sprod

@ -0,0 +1,2 @@
#!/bin/bash

4
config/environments/production.rb

@ -52,8 +52,8 @@ Rails.application.configure do
config.force_ssl = false
# Semantic Logger in syslog only!
require Rails.root.join('lib', 'formatters', 'basic_formatter')
config.semantic_logger.add_appender(appender: :syslog, formatter: BasicFormatter.new)
config.x.semantic.formatter = 'Semantic::BasicFormatter'
# config.semantic_logger.add_appender(appender: :syslog, formatter: Semantic::BasicFormatter.new)
# Prepend all log lines with the following tags.
# config.log_tags = [:request_id]

34
config/initializers/instrumentalizing.rb

@ -14,10 +14,25 @@ def build_instrumentation_config
end
Rails.configuration.after_initialize do
if Rails.env.development?
RailsSemanticLogger::Rack::Logger.logger.level = :info # useful for remaining log like "[Rack::Log] Started..."
SemanticLogger.clear_appenders!
Rails.logger.name = 'rails'
SemanticLogger.clear_appenders!
RailsSemanticLogger::Rack::Logger.logger.level = :info # useful for remaining log like "[Rack::Log] Started..."
Rails.logger.name = 'rails'
if Rails.env.production?
SemanticLogger.add_appender(appender: :syslog,
formatter: Rails.application.config.x.semantic.formatter.constantize.new)
else
SemanticLogger.add_appender(io: $stdout, formatter: Rails.application.config.x.semantic.formatter.constantize.new)
Rails.autoloaders.main.on_load('ApplicationController') do
SemanticLogger.clear_appenders!
SemanticLogger.add_appender(io: $stdout, formatter: Rails.application.config.x.semantic.formatter.constantize.new)
Rails.logger.info ' ', dimensions: Semantic::FancyDimensions.new(rails: '╔═╗', before: 1)
Rails.logger.fatal 'Zeitwerk RELOAD!', dimensions: Semantic::FancyDimensions.new(rails: '╠█╣')
Rails.logger.info ' ', dimensions: Semantic::FancyDimensions.new(rails: '╚═╝')
Semantic::Instrumentalizer.activate(*build_instrumentation_config)
end
end
ActiveSupport::Notifications.subscribe('rolling.live_constant') do |event|
@ -27,14 +42,3 @@ Rails.configuration.after_initialize do
end
end
end
Rails.autoloaders.main.on_load('ApplicationController') do
SemanticLogger.clear_appenders!
SemanticLogger.add_appender(io: $stdout, formatter: Rails.application.config.x.semantic.formatter.constantize.new)
Rails.logger.info ' ', dimensions: Semantic::FancyDimensions.new(rails: '╔═╗', before: 1)
Rails.logger.fatal 'Zeitwerk RELOAD!', dimensions: Semantic::FancyDimensions.new(rails: '╠█╣')
Rails.logger.info ' ', dimensions: Semantic::FancyDimensions.new(rails: '╚═╝')
Semantic::Instrumentalizer.activate(*build_instrumentation_config)
end

4
lib/live/definable.rb

@ -84,7 +84,7 @@ module Live
# origin (or caller[0]) helps fetching the constant name from source code introspection
def define_type_from_callee(origin, type, default)
@@class_origin ||= self # rubocop:disable Style/ClassVars
@listener ||= start_listener
@listener ||= start_listener if defined?(Listen)
file, line = origin.split(':')
constant = introspect_constant_from_file(file, line.to_i - 1)
@ -97,7 +97,7 @@ module Live
end
def start_listener
DEFINABLE_LISTENERS.each(&:stop)
DEFINABLE_LISTENERS.each(&:stop) # FIXME: issue on stop after multiple reload!!! Learn mode about listener: #close
DEFINABLE_LISTENERS.clear
DEFINABLE_THREAD_GROUP.list.each(&:kill)

64
test/formatters/wrapper2_test.rb

@ -1,64 +0,0 @@
require_relative '../../lib/formatters/ansi_wrapper'
require 'minitest/autorun'
class Wrapper2Test < Minitest::Test
def test_wrap_score
assert_wrap("\
********************\e[0m\n\
********************\e[0m\n\
**********\e[0m",
('*' * 50), 20)
assert_wrap("\
> ******************\e[0m\n\
> ******************\e[0m\n\
> **************\e[0m",
('*' * 50), 20, '> ')
end
def test_wrap_sql
assert_wrap("\
\e[0m\e[0;36mELECT \"scores\".* FRO\e[0m
+\e[0m\e[0;36mM \"scores\" WHERE \"sc\e[0m
+\e[0m\e[0;36mores\".\"id\" = $1 LIMI\e[0m
+\e[0m\e[0;36mT $2\e[0m [[\"id\", 2], [\"L
IMIT\", 1]]
\e[0;35mScore Load (0.6ms)\e[0m \e[0;36mS\e[0m\n\
\e[0;36mELECT \"scores\".* FRO\e[0m\n\
\e[0;36mM \"scores\" WHERE \"sc\e[0m\n\
\e[0;36mores\".\"id\" = $1 LIMI\e[0m\n\
\e[0;36mT $2\e[0m [[\"id\", 2], [\"L\n\
IMIT\", 1]]", "\
\e[0;35mScore Load (0.6ms)\e[0m \e[0;36mSELECT \"scores\".* FROM \"scores\" WHERE \"scores\".\"id\" = $1 LIMIT $2\e[0m [[\"id\", 2], [\"LIMIT\", 1]]",
20)
end
# def test_carriage_return
# assert_wrap('text', "text\n\n", 10)
# assert_wrap('first ', "first \n", 10)
# assert_wrap('first ', "first \n", 10)
# assert_wrap('first ', "first \n", 10)
# assert_wrap("first \n second ", "first\n second\n", 10)
# assert_wrap("\e[0;35mhello\e[0m \n\e[0;35m\e[0m\n\e[0;35myop", "\e[0;35mhello\n\nyop", 10)
# end
private
def assert_wrap(expectation, text, length, prefix = '')
show_with_style(text, length, prefix)
assert_equal(expectation, prefix + AnsiWrapper.wrap(text, length - prefix.length, prefix))
end
def show_with_style(text, length, prefix = '')
expectation = AnsiWrapper.wrap(text, length - prefix.length, prefix)
puts "original_o is:\n#{prefix}[#{text}]\e[0m"
puts "original_i is:\n#{prefix}[#{text.inspect}]\e[0m"
puts
puts "final_o is:\n#{prefix}[#{expectation}]\e[0m"
puts "final_i is:\n#{prefix}[#{expectation.inspect}]\e[0m"
puts
end
end
Loading…
Cancel
Save