Browse Source

production syslog logger

main
pvincent 2 months ago
parent
commit
0f8ff0e5ff
  1. 3
      .vscode/settings.json
  2. 2
      app/controllers/scores_controller.rb
  3. 7
      bin/sprod
  4. 2
      config/environments/development.rb
  5. 11
      config/environments/production.rb
  6. 31
      config/initializers/instrumentalizing.rb
  7. 2
      lib/semantic/abstract_formatter.rb
  8. 20
      lib/semantic/ansi_colors.rb
  9. 2
      lib/semantic/basic_formatter.rb
  10. 2
      lib/semantic/subscribers/action_controller.rb
  11. 2
      lib/semantic/subscribers/active_record.rb

3
.vscode/settings.json

@ -38,7 +38,8 @@
"terminal.integrated.minimumContrastRatio": 1, "terminal.integrated.minimumContrastRatio": 1,
"terminal.integrated.drawBoldTextInBrightColors": false, "terminal.integrated.drawBoldTextInBrightColors": false,
"workbench.colorCustomizations": { "workbench.colorCustomizations": {
"terminal.background": "#1f1f1f"
"terminal.background": "#1f1f1f",
"terminal.ansiYellow": "#b38210" //BROWN UNIX ANSI
}, },
"workbench.colorTheme": "Default Dark Modern", "workbench.colorTheme": "Default Dark Modern",
"inlineFold.unfoldOnLineSelect": true, "inlineFold.unfoldOnLineSelect": true,

2
app/controllers/scores_controller.rb

@ -42,7 +42,7 @@ class ScoresController < ApplicationController
end end
def destroy def destroy
zazaz
# zazaz
@score.destroy! @score.destroy!
redirect_to scores_url, notice: 'Score was successfully destroyed.', status: :see_other redirect_to scores_url, notice: 'Score was successfully destroyed.', status: :see_other

7
bin/sprod

@ -1,2 +1,9 @@
#!/bin/bash #!/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

2
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' routes.default_url_options[:host] = '127.0.0.1'
config.x.semantic.formatter = 'Semantic::FancyFormatter' config.x.semantic.formatter = 'Semantic::FancyFormatter'
config.x.semantic.formatter = 'Semantic::BasicFormatter'
# config.x.semantic.formatter = 'Semantic::BasicFormatter'
end end

11
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. # 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. # Can be used together with config.force_ssl for Strict-Transport-Security and secure cookies.
# config.assume_ssl = true # 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. # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = false 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. # Prepend all log lines with the following tags.
# config.log_tags = [:request_id] # config.log_tags = [:request_id]
@ -94,7 +91,7 @@ Rails.application.configure do
# Skip DNS rebinding protection for the default health check endpoint. # Skip DNS rebinding protection for the default health check endpoint.
# config.host_authorization = { exclude: ->(request) { request.path == "/up" } } # 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 end

31
config/initializers/instrumentalizing.rb

@ -14,18 +14,34 @@ def build_instrumentation_config
end end
Rails.configuration.after_initialize do 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' Rails.logger.name = 'rails'
if Rails.env.production? 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 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 Rails.autoloaders.main.on_load('ApplicationController') do
SemanticLogger.clear_appenders! 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.info ' ', dimensions: Semantic::FancyDimensions.new(rails: '╔═╗', before: 1)
Rails.logger.fatal 'Zeitwerk RELOAD!', dimensions: Semantic::FancyDimensions.new(rails: '╠█╣') Rails.logger.fatal 'Zeitwerk RELOAD!', dimensions: Semantic::FancyDimensions.new(rails: '╠█╣')
@ -33,8 +49,6 @@ Rails.configuration.after_initialize do
Semantic::Instrumentalizer.activate(*build_instrumentation_config) Semantic::Instrumentalizer.activate(*build_instrumentation_config)
end end
end
ActiveSupport::Notifications.subscribe('rolling.live_constant') do |event| ActiveSupport::Notifications.subscribe('rolling.live_constant') do |event|
constants = event.payload[:changes].map { |change| change[:constant] } constants = event.payload[:changes].map { |change| change[:constant] }
if constants.intersection(%w[ACTIVE_RECORD ACTION_VIEW]).any? if constants.intersection(%w[ACTIVE_RECORD ACTION_VIEW]).any?
@ -42,3 +56,4 @@ Rails.configuration.after_initialize do
end end
end end
end end
end

2
lib/semantic/abstract_formatter.rb

@ -52,7 +52,7 @@ module Semantic
end end
def tags 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) colorize(centerize(first_tag, TAG_FIXED_LENGTH), taint)
end end

20
lib/semantic/ansi_colors.rb

@ -8,17 +8,17 @@ module Semantic
BOLD = "\e[1m".freeze BOLD = "\e[1m".freeze
UNDERLINE = "\e[4m".freeze UNDERLINE = "\e[4m".freeze
# TEXT
# TEXT 16-colors
TEXT_BLACK = "\e[30m".freeze TEXT_BLACK = "\e[30m".freeze
TEXT_RED = "\e[31m".freeze TEXT_RED = "\e[31m".freeze
TEXT_GREEN = "\e[32m".freeze TEXT_GREEN = "\e[32m".freeze
TEXT_YELLOW = "\e[33m".freeze
TEXT_BROWN = "\e[33m".freeze
TEXT_BLUE = "\e[34m".freeze TEXT_BLUE = "\e[34m".freeze
TEXT_MAGENTA = "\e[35m".freeze TEXT_MAGENTA = "\e[35m".freeze
TEXT_CYAN = "\e[36m".freeze TEXT_CYAN = "\e[36m".freeze
TEXT_WHITE = "\e[37m".freeze TEXT_WHITE = "\e[37m".freeze
# TEXT GRAY SHADES
# TEXT GRAY SHADES 256-colors
TEXT_GRAY_800 = "\e[38;5;232m".freeze TEXT_GRAY_800 = "\e[38;5;232m".freeze
TEXT_GRAY_700 = "\e[38;5;233m".freeze TEXT_GRAY_700 = "\e[38;5;233m".freeze
TEXT_GRAY_600 = "\e[38;5;235m".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_200 = "\e[38;5;249m".freeze
TEXT_GRAY_100 = "\e[38;5;252m".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
DARK_TEXT_BLACK = "\e[90m".freeze DARK_TEXT_BLACK = "\e[90m".freeze
DARK_TEXT_RED = "\e[91m".freeze DARK_TEXT_RED = "\e[91m".freeze
DARK_TEXT_GREEN = "\e[92m".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_BLUE = "\e[94m".freeze
DARK_TEXT_MAGENTA = "\e[95m".freeze DARK_TEXT_MAGENTA = "\e[95m".freeze
DARK_TEXT_CYAN = "\e[96m".freeze DARK_TEXT_CYAN = "\e[96m".freeze
@ -51,7 +52,8 @@ module Semantic
BG_GRAY = "\e[100m".freeze BG_GRAY = "\e[100m".freeze
BG_RED = "\e[41m".freeze BG_RED = "\e[41m".freeze
BG_GREEN = "\e[42m".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_BLUE = "\e[44m".freeze
BG_MAGENTA = "\e[45m".freeze BG_MAGENTA = "\e[45m".freeze
BG_CYAN = "\e[46m".freeze BG_CYAN = "\e[46m".freeze
@ -59,7 +61,7 @@ module Semantic
# DARK BACKGROUND # DARK BACKGROUND
DARK_BG_RED = "\e[101m".freeze DARK_BG_RED = "\e[101m".freeze
DARK_BG_GREEN = "\e[102m".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_BLUE = "\e[104m".freeze
DARK_BG_MAGENTA = "\e[105m".freeze DARK_BG_MAGENTA = "\e[105m".freeze
DARK_BG_CYAN = "\e[106m".freeze DARK_BG_CYAN = "\e[106m".freeze

2
lib/semantic/basic_formatter.rb

@ -11,7 +11,7 @@ module Semantic
super(color_map: ColorMap.new( super(color_map: ColorMap.new(
debug: TEXT_GRAY_400, debug: TEXT_GRAY_400,
info: TEXT_GREEN, info: TEXT_GREEN,
warn: TEXT_YELLOW,
warn: TEXT_BROWN,
error: LIGHT_TEXT_RED, error: LIGHT_TEXT_RED,
fatal: TEXT_MAGENTA fatal: TEXT_MAGENTA
)) ))

2
lib/semantic/subscribers/action_controller.rb

@ -40,7 +40,7 @@ module Semantic
unless params.empty? unless params.empty?
params = params.ai(ruby19_syntax: true, plain: true, multiline: false) params = params.ai(ruby19_syntax: true, plain: true, multiline: false)
params.gsub!(/(\w+):/, "#{TEXT_CYAN}\\1#{CLEAR}:") params.gsub!(/(\w+):/, "#{TEXT_CYAN}\\1#{CLEAR}:")
params.gsub!(/"(.*?)"/, "\"#{TEXT_YELLOW}\\1#{CLEAR}\"")
params.gsub!(/"(.*?)"/, "\"#{TEXT_BROWN}\\1#{CLEAR}\"")
end end
logger.debug("Parameters: #{params}") unless params.empty? logger.debug("Parameters: #{params}") unless params.empty?
end end

2
lib/semantic/subscribers/active_record.rb

@ -38,7 +38,7 @@ module Semantic
name = "Read #{row_count} #{model.pluralize(row_count)}" name = "Read #{row_count} #{model.pluralize(row_count)}"
end end
when 'Update', 'Create', 'Destroy' when 'Update', 'Create', 'Destroy'
statement_taint = TEXT_COLOR_PINK
statement_taint = TEXT_PINK
name = "#{category} #{model}" name = "#{category} #{model}"
category_taint = TRANSACTION_TAINT category_taint = TRANSACTION_TAINT
increment_transaction_local(event.payload[:transaction].uuid, category.downcase.to_sym, event.duration) increment_transaction_local(event.payload[:transaction].uuid, category.downcase.to_sym, event.duration)

Loading…
Cancel
Save