"Fossies" - the Fresh Open Source Software Archive

Member "discourse-3.2.0/config/initializers/100-logster.rb" (30 Jan 2024, 4564 Bytes) of package /linux/www/discourse-3.2.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "100-logster.rb": 3.1.5_vs_3.2.0.

    1 # frozen_string_literal: true
    2 
    3 if GlobalSetting.skip_redis?
    4   Rails.application.reloader.to_prepare do
    5     Rails.logger = Rails.logger.chained.first if Rails.logger.respond_to? :chained
    6   end
    7   return
    8 end
    9 
   10 if Rails.env.development? && !Sidekiq.server? && ENV["RAILS_LOGS_STDOUT"] == "1"
   11   Rails.application.config.after_initialize do
   12     console = ActiveSupport::Logger.new(STDOUT)
   13     original_logger = Rails.logger.chained.first
   14     console.formatter = original_logger.formatter
   15     console.level = original_logger.level
   16 
   17     unless ActiveSupport::Logger.logger_outputs_to?(original_logger, STDOUT)
   18       original_logger.extend(ActiveSupport::Logger.broadcast(console))
   19     end
   20   end
   21 end
   22 
   23 if Rails.env.production?
   24   Logster.store.ignore = [
   25     # These errors are caused by client requests. No need to log them.
   26     # Rails itself defines these as 'silent exceptions', but this does
   27     # not entirely prevent them from being logged
   28     # https://github.com/rails/rails/blob/f2caed1e/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb#L39-L42
   29     /^ActionController::RoutingError \(No route matches/,
   30     /^ActionDispatch::Http::MimeNegotiation::InvalidType/,
   31     /^PG::Error: ERROR:\s+duplicate key/,
   32     /^ActionController::UnknownFormat/,
   33     /^ActionController::UnknownHttpMethod/,
   34     /^AbstractController::ActionNotFound/,
   35     # ignore any empty JS errors that contain blanks or zeros for line and column fields
   36     #
   37     # Line:
   38     # Column:
   39     #
   40     /(?m).*?Line: (?:\D|0).*?Column: (?:\D|0)/,
   41     # suppress empty JS errors (covers MSIE 9, etc)
   42     /^(Syntax|Script) error.*Line: (0|1)\b/m,
   43     # CSRF errors are not providing enough data
   44     # suppress unconditionally for now
   45     /^Can't verify CSRF token authenticity.$/,
   46     # Yandex bot triggers this JS error a lot
   47     /^Uncaught ReferenceError: I18n is not defined/,
   48     # related to browser plugins somehow, we don't care
   49     /Error calling method on NPObject/,
   50     # 404s can be dealt with elsewhere
   51     /^ActiveRecord::RecordNotFound/,
   52     # bad asset requested, no need to log
   53     /^ActionController::BadRequest/,
   54     # we can't do anything about invalid parameters
   55     /Rack::QueryParser::InvalidParameterError/,
   56     # we handle this cleanly in the message bus middleware
   57     # no point logging to logster
   58     /RateLimiter::LimitExceeded.*/m,
   59   ]
   60   Logster.config.env_expandable_keys.push(:hostname, :problem_db)
   61 end
   62 
   63 Logster.store.max_backlog = GlobalSetting.max_logster_logs
   64 
   65 # TODO logster should be able to do this automatically
   66 Logster.config.subdirectory = "#{GlobalSetting.relative_url_root}/logs"
   67 
   68 Logster.config.application_version = Discourse.git_version
   69 Logster.config.enable_custom_patterns_via_ui = true
   70 Logster.config.use_full_hostname = true
   71 Logster.config.enable_js_error_reporting = GlobalSetting.enable_js_error_reporting
   72 
   73 store = Logster.store
   74 redis = Logster.store.redis
   75 store.redis_prefix = Proc.new { redis.namespace }
   76 store.redis_raw_connection = redis.without_namespace
   77 severities = [Logger::WARN, Logger::ERROR, Logger::FATAL, Logger::UNKNOWN]
   78 
   79 RailsMultisite::ConnectionManagement.each_connection do
   80   error_rate_per_minute =
   81     begin
   82       SiteSetting.alert_admins_if_errors_per_minute
   83     rescue StandardError
   84       0
   85     end
   86 
   87   if (error_rate_per_minute || 0) > 0
   88     store.register_rate_limit_per_minute(severities, error_rate_per_minute) do |rate|
   89       MessageBus.publish(
   90         "/logs_error_rate_exceeded",
   91         { rate: rate, duration: "minute", publish_at: Time.current.to_i },
   92         group_ids: [Group::AUTO_GROUPS[:admins]],
   93       )
   94     end
   95   end
   96 
   97   error_rate_per_hour =
   98     begin
   99       SiteSetting.alert_admins_if_errors_per_hour
  100     rescue StandardError
  101       0
  102     end
  103 
  104   if (error_rate_per_hour || 0) > 0
  105     store.register_rate_limit_per_hour(severities, error_rate_per_hour) do |rate|
  106       MessageBus.publish(
  107         "/logs_error_rate_exceeded",
  108         { rate: rate, duration: "hour", publish_at: Time.current.to_i },
  109         group_ids: [Group::AUTO_GROUPS[:admins]],
  110       )
  111     end
  112   end
  113 end
  114 
  115 if Rails.configuration.multisite
  116   if Rails.logger.respond_to? :chained
  117     chained = Rails.logger.chained
  118     chained && chained.first.formatter = RailsMultisite::Formatter.new
  119   end
  120 end
  121 
  122 Logster.config.project_directories = [
  123   { path: Rails.root.to_s, url: "https://github.com/discourse/discourse", main_app: true },
  124 ]
  125 Discourse.plugins.each do |plugin|
  126   next if !plugin.metadata.url
  127 
  128   Logster.config.project_directories << {
  129     path: "#{Rails.root}/plugins/#{plugin.directory_name}",
  130     url: plugin.metadata.url,
  131   }
  132 end