Skip to content

A platform for community discussion. Free, open, simple.

DEV: Customize the dev-env rails logger (#39988)

Jarek Radoszf6daea608207
diff --git a/config/environments/development.rb b/config/environments/development.rb
index 03011dc9831..a2a7b7d6707 100644
--- a/config/environments/development.rb
+++ b/config/environments/development.rb
@@ -50,6 +50,9 @@ Discourse::Application.configure do
config.log_level = ENV["DISCOURSE_DEV_LOG_LEVEL"] if ENV["DISCOURSE_DEV_LOG_LEVEL"]
+ require_relative "../../lib/dev_log_formatter"
+ config.log_formatter = DevLogFormatter.new
+
config.active_record.logger = nil if ENV["RAILS_DISABLE_ACTIVERECORD_LOGS"] == "1" ||
ENV["ENABLE_LOGSTASH_LOGGER"] == "1"
config.active_record.verbose_query_logs = true if ENV["RAILS_VERBOSE_QUERY_LOGS"] == "1"
diff --git a/config/initializers/100-logster.rb b/config/initializers/100-logster.rb
index 6f12cb1e54d..c7360abe673 100644
--- a/config/initializers/100-logster.rb
+++ b/config/initializers/100-logster.rb
@@ -16,7 +16,9 @@ if Rails.env.development? && !Sidekiq.server? && ENV["RAILS_LOGS_STDOUT"] == "1"
Rails.application.config.after_initialize do
console = ActiveSupport::Logger.new(STDOUT)
original_logger = Rails.logger.broadcasts.first
- console.formatter = original_logger.formatter
+ formatter = original_logger.formatter
+ console.formatter =
+ formatter.is_a?(DevLogFormatter) && ENV["NO_COLOR"].nil? ? formatter.colored : formatter
console.level = original_logger.level
unless ActiveSupport::Logger.logger_outputs_to?(original_logger, STDOUT)
diff --git a/config/pitchfork.conf.rb b/config/pitchfork.conf.rb
index 51a50b95a2f..675a0c6eb52 100644
--- a/config/pitchfork.conf.rb
+++ b/config/pitchfork.conf.rb
@@ -14,7 +14,16 @@ if enable_logstash_logger
customize_event: lambda { |event| event["@timestamp"] = ::Time.now.utc },
)
else
- logger Logger.new(STDOUT)
+ console_logger = Logger.new(STDOUT)
+ if ENV["RAILS_ENV"] != "production"
+ require_relative "../lib/dev_log_formatter"
+ console_logger.formatter = DevLogFormatter.new(color: ENV["NO_COLOR"].nil?)
+
+ # Drop per-worker spawn logs. Summary below covers that.
+ console_logger.level = Logger::WARN
+ STDOUT.puts "Starting Pitchfork..."
+ end
+ logger console_logger
end
worker_processes (ENV["UNICORN_WORKERS"] || 3).to_i
@@ -123,7 +132,18 @@ before_service_worker_ready do |server, service_worker|
demon_class.start(1, logger: server.logger)
end
- Demon::PluginJsWatcher.start(verbose: true) if Rails.env.development? && !ENV["CI"]
+ if Rails.env.development? && !ENV["CI"]
+ Demon::PluginJsWatcher.start(verbose: false, logger: server.logger)
+ end
+
+ if Rails.env.development?
+ workers = server.worker_processes
+ parts = ["#{workers} worker#{"s" if workers != 1}"]
+ parts << "#{sidekiqs} sidekiq#{"s" if sidekiqs != 1}" if sidekiqs > 0
+ parts.concat(DiscoursePluginRegistry.demon_processes.map(&:prefix))
+ parts << "plugin JS watcher" if !ENV["CI"]
+ STDOUT.puts "Pitchfork ready on http://localhost:#{ENV["UNICORN_PORT"] || 3000} (#{parts.join(", ")})"
+ end
Thread.new do
while true
diff --git a/lib/demon/plugin_js_watcher.rb b/lib/demon/plugin_js_watcher.rb
index ebfd48e3895..f62124e6521 100644
--- a/lib/demon/plugin_js_watcher.rb
+++ b/lib/demon/plugin_js_watcher.rb
@@ -6,7 +6,7 @@ class Demon::PluginJsWatcher < ::Demon::Base
end
def after_fork
- log("[PluginJsWatcher] Loading PluginJsWatcher in process id #{Process.pid}")
+ log("Loading PluginJsWatcher in process id #{Process.pid}")
@queue = Queue.new
diff --git a/lib/dev_log_formatter.rb b/lib/dev_log_formatter.rb
new file mode 100644
index 00000000000..3a156925baa
--- /dev/null
+++ b/lib/dev_log_formatter.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+# Compact dev log formatter: drops timestamp and pid.
+# Prefixes a severity label for file log, drops the prefix
+# applies color to the whole message instead.
+class DevLogFormatter < ::Logger::Formatter
+ SEVERITY_COLORS = {
+ "DEBUG" => "\e[90m",
+ "WARN" => "\e[33m",
+ "ERROR" => "\e[31m",
+ "FATAL" => "\e[1;31m",
+ "UNKNOWN" => "\e[35m",
+ }.freeze
+ RESET = "\e[0m"
+
+ attr_reader :color
+
+ def initialize(color: false)
+ super()
+ @color = color
+ end
+
+ def colored
+ clone.tap { |f| f.instance_variable_set(:@color, true) }
+ end
+
+ def call(severity, _time, _progname, msg)
+ message = msg2str(msg)
+ if @color
+ code = SEVERITY_COLORS[severity]
+ code ? "#{code}#{message}#{RESET}\n" : "#{message}\n"
+ else
+ "#{severity.ljust(5)} #{message}\n"
+ end
+ end
+end
diff --git a/lib/plugin/js_manager.rb b/lib/plugin/js_manager.rb
index 34bea267327..836732290b8 100644
--- a/lib/plugin/js_manager.rb
+++ b/lib/plugin/js_manager.rb
@@ -47,7 +47,7 @@ module Plugin
start = Time.now
if !GlobalSetting.mini_racer_single_threaded && AssetProcessor.booted?
- raise "[Plugin::JSManager] Cannot fork for parallel compilation because AssetProcessor is already booted."
+ raise "Cannot fork Plugin::JsManager for parallel compilation because AssetProcessor is already booted."
end
parallel_count = [Etc.nprocessors, 4].min
@@ -209,7 +209,7 @@ module Plugin
end
def log(message)
- STDERR.puts "[Plugin::JsManager] #{message}"
+ STDERR.puts message
end
private_class_method def self.maybe_cache(key, &blk)