From 0f8ff0e5ff3b111d6ca60e805f961b56a9538c8a Mon Sep 17 00:00:00 2001 From: pvincent Date: Mon, 23 Sep 2024 22:58:53 +0400 Subject: [PATCH] production syslog logger --- .vscode/settings.json | 3 +- app/controllers/scores_controller.rb | 2 +- bin/sprod | 7 ++++ config/environments/development.rb | 2 +- config/environments/production.rb | 11 ++---- config/initializers/instrumentalizing.rb | 39 +++++++++++++------ lib/semantic/abstract_formatter.rb | 2 +- lib/semantic/ansi_colors.rb | 20 +++++----- lib/semantic/basic_formatter.rb | 2 +- lib/semantic/subscribers/action_controller.rb | 2 +- lib/semantic/subscribers/active_record.rb | 2 +- 11 files changed, 57 insertions(+), 35 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index a649fba..28cf01e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -38,7 +38,8 @@ "terminal.integrated.minimumContrastRatio": 1, "terminal.integrated.drawBoldTextInBrightColors": false, "workbench.colorCustomizations": { - "terminal.background": "#1f1f1f" + "terminal.background": "#1f1f1f", + "terminal.ansiYellow": "#b38210" //BROWN UNIX ANSI }, "workbench.colorTheme": "Default Dark Modern", "inlineFold.unfoldOnLineSelect": true, diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index 7e58285..f58876f 100644 --- a/app/controllers/scores_controller.rb +++ b/app/controllers/scores_controller.rb @@ -42,7 +42,7 @@ class ScoresController < ApplicationController end def destroy - zazaz + # zazaz @score.destroy! redirect_to scores_url, notice: 'Score was successfully destroyed.', status: :see_other diff --git a/bin/sprod b/bin/sprod index 05a7907..0c052e3 100644 --- a/bin/sprod +++ b/bin/sprod @@ -1,2 +1,9 @@ #!/bin/bash +SEMANTIC_LOGGER_APP=bichik RAILS_ENV=production SECRET_KEY_BASE='toto est fluo' rails server + +# journalctl --all -o cat --full -t bichik +# journalctl --all -o short --no-hostname -t bichik + +# TO REMOVE PROD assets +# rm -rf tmp/cache/assets diff --git a/config/environments/development.rb b/config/environments/development.rb index c96236c..297c4ed 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -79,5 +79,5 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength routes.default_url_options[:host] = '127.0.0.1' config.x.semantic.formatter = 'Semantic::FancyFormatter' - config.x.semantic.formatter = 'Semantic::BasicFormatter' + # config.x.semantic.formatter = 'Semantic::BasicFormatter' end diff --git a/config/environments/production.rb b/config/environments/production.rb index a9da34c..e3033ca 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -47,14 +47,11 @@ Rails.application.configure do # Assume all access to the app is happening through a SSL-terminating reverse proxy. # Can be used together with config.force_ssl for Strict-Transport-Security and secure cookies. # config.assume_ssl = true + config.assume_ssl = false # FIXME: remove as soon as you prefer using Nginx Proxy ! With this hack, you can start server in production mode # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. config.force_ssl = false - # Semantic Logger in syslog only! - 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] @@ -94,7 +91,7 @@ Rails.application.configure do # Skip DNS rebinding protection for the default health check endpoint. # config.host_authorization = { exclude: ->(request) { request.path == "/up" } } # - config.after_initialize do - config.semantic_logger.add_appender(appender: :syslog, formatter: Semantic::BasicFormatter.new) - end + + # Semantic Logger production ready! + config.x.semantic.formatter = 'Semantic::BasicFormatter' end diff --git a/config/initializers/instrumentalizing.rb b/config/initializers/instrumentalizing.rb index 4b922da..c57a2fe 100644 --- a/config/initializers/instrumentalizing.rb +++ b/config/initializers/instrumentalizing.rb @@ -14,18 +14,34 @@ def build_instrumentation_config end Rails.configuration.after_initialize do - 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) + + SemanticLogger.add_appender( + appender: :syslog, + formatter: Rails.application.config.x.semantic.formatter.constantize.new + ) + + Rails.logger.info('adding instrumentations...') + Semantic::Instrumentalizer.activate(*build_instrumentation_config) + Rails.logger.info('adding instrumentations...DONE') + else - SemanticLogger.add_appender(io: $stdout, formatter: Rails.application.config.x.semantic.formatter.constantize.new) + SemanticLogger.clear_appenders! + RailsSemanticLogger::Rack::Logger.logger.level = :info # useful for remaining log like "[Rack::Log] Started..." + + 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) + 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: '╠█╣') @@ -33,12 +49,11 @@ Rails.configuration.after_initialize do Semantic::Instrumentalizer.activate(*build_instrumentation_config) end - end - - ActiveSupport::Notifications.subscribe('rolling.live_constant') do |event| - constants = event.payload[:changes].map { |change| change[:constant] } - if constants.intersection(%w[ACTIVE_RECORD ACTION_VIEW]).any? - Semantic::Instrumentalizer.activate(*build_instrumentation_config) + ActiveSupport::Notifications.subscribe('rolling.live_constant') do |event| + constants = event.payload[:changes].map { |change| change[:constant] } + if constants.intersection(%w[ACTIVE_RECORD ACTION_VIEW]).any? + Semantic::Instrumentalizer.activate(*build_instrumentation_config) + end end end end diff --git a/lib/semantic/abstract_formatter.rb b/lib/semantic/abstract_formatter.rb index 7a0cbf5..d21ca88 100644 --- a/lib/semantic/abstract_formatter.rb +++ b/lib/semantic/abstract_formatter.rb @@ -52,7 +52,7 @@ module Semantic end def tags - first_tag, taint = log.tags.empty? ? [TAG_NONE, CLEAR] : [log.tags.first, BG_GRAY + DARK_TEXT_YELLOW] + first_tag, taint = log.tags.empty? ? [TAG_NONE, CLEAR] : [log.tags.first, BG_GRAY + TEXT_YELLOW] colorize(centerize(first_tag, TAG_FIXED_LENGTH), taint) end diff --git a/lib/semantic/ansi_colors.rb b/lib/semantic/ansi_colors.rb index 1ff386c..0fa136e 100644 --- a/lib/semantic/ansi_colors.rb +++ b/lib/semantic/ansi_colors.rb @@ -8,17 +8,17 @@ module Semantic BOLD = "\e[1m".freeze UNDERLINE = "\e[4m".freeze - # TEXT + # TEXT 16-colors TEXT_BLACK = "\e[30m".freeze TEXT_RED = "\e[31m".freeze TEXT_GREEN = "\e[32m".freeze - TEXT_YELLOW = "\e[33m".freeze + TEXT_BROWN = "\e[33m".freeze TEXT_BLUE = "\e[34m".freeze TEXT_MAGENTA = "\e[35m".freeze TEXT_CYAN = "\e[36m".freeze TEXT_WHITE = "\e[37m".freeze - # TEXT GRAY SHADES + # TEXT GRAY SHADES 256-colors TEXT_GRAY_800 = "\e[38;5;232m".freeze TEXT_GRAY_700 = "\e[38;5;233m".freeze TEXT_GRAY_600 = "\e[38;5;235m".freeze @@ -28,15 +28,16 @@ module Semantic TEXT_GRAY_200 = "\e[38;5;249m".freeze TEXT_GRAY_100 = "\e[38;5;252m".freeze - # TEXT EXTRA COLOR NAME - TEXT_COLOR_PINK = "\e[38;5;175m".freeze - TEXT_COLOR_ORANGE = "\e[38;5;202m".freeze + # TEXT 256-colors + TEXT_PINK = "\e[38;5;175m".freeze + TEXT_ORANGE = "\e[38;5;202m".freeze + TEXT_YELLOW = "\e[93m".freeze # DARK TEXT DARK_TEXT_BLACK = "\e[90m".freeze DARK_TEXT_RED = "\e[91m".freeze DARK_TEXT_GREEN = "\e[92m".freeze - DARK_TEXT_YELLOW = "\e[93m".freeze + DARK_TEXT_YELLOW = TEXT_YELLOW DARK_TEXT_BLUE = "\e[94m".freeze DARK_TEXT_MAGENTA = "\e[95m".freeze DARK_TEXT_CYAN = "\e[96m".freeze @@ -51,7 +52,8 @@ module Semantic BG_GRAY = "\e[100m".freeze BG_RED = "\e[41m".freeze BG_GREEN = "\e[42m".freeze - BG_YELLOW = "\e[43m".freeze + BG_BROWN = "\e[43m".freeze + BG_YELLOW = "\e[103m".freeze BG_BLUE = "\e[44m".freeze BG_MAGENTA = "\e[45m".freeze BG_CYAN = "\e[46m".freeze @@ -59,7 +61,7 @@ module Semantic # DARK BACKGROUND DARK_BG_RED = "\e[101m".freeze DARK_BG_GREEN = "\e[102m".freeze - DARK_BG_YELLOW = "\e[103m".freeze + DARK_BG_YELLOW = BG_YELLOW DARK_BG_BLUE = "\e[104m".freeze DARK_BG_MAGENTA = "\e[105m".freeze DARK_BG_CYAN = "\e[106m".freeze diff --git a/lib/semantic/basic_formatter.rb b/lib/semantic/basic_formatter.rb index dafb035..7c5270f 100644 --- a/lib/semantic/basic_formatter.rb +++ b/lib/semantic/basic_formatter.rb @@ -11,7 +11,7 @@ module Semantic super(color_map: ColorMap.new( debug: TEXT_GRAY_400, info: TEXT_GREEN, - warn: TEXT_YELLOW, + warn: TEXT_BROWN, error: LIGHT_TEXT_RED, fatal: TEXT_MAGENTA )) diff --git a/lib/semantic/subscribers/action_controller.rb b/lib/semantic/subscribers/action_controller.rb index 1731c42..340c5ac 100644 --- a/lib/semantic/subscribers/action_controller.rb +++ b/lib/semantic/subscribers/action_controller.rb @@ -40,7 +40,7 @@ module Semantic unless params.empty? params = params.ai(ruby19_syntax: true, plain: true, multiline: false) params.gsub!(/(\w+):/, "#{TEXT_CYAN}\\1#{CLEAR}:") - params.gsub!(/"(.*?)"/, "\"#{TEXT_YELLOW}\\1#{CLEAR}\"") + params.gsub!(/"(.*?)"/, "\"#{TEXT_BROWN}\\1#{CLEAR}\"") end logger.debug("Parameters: #{params}") unless params.empty? end diff --git a/lib/semantic/subscribers/active_record.rb b/lib/semantic/subscribers/active_record.rb index c076382..8eca929 100644 --- a/lib/semantic/subscribers/active_record.rb +++ b/lib/semantic/subscribers/active_record.rb @@ -38,7 +38,7 @@ module Semantic name = "Read #{row_count} #{model.pluralize(row_count)}" end when 'Update', 'Create', 'Destroy' - statement_taint = TEXT_COLOR_PINK + statement_taint = TEXT_PINK name = "#{category} #{model}" category_taint = TRANSACTION_TAINT increment_transaction_local(event.payload[:transaction].uuid, category.downcase.to_sym, event.duration)