diff --git a/Gemfile b/Gemfile index ba2744ef2..f34a0a3db 100644 --- a/Gemfile +++ b/Gemfile @@ -222,8 +222,9 @@ gem 'simple-rss', require: false gem 'gctools', require: false, platform: :mri_21 gem 'stackprof', require: false, platform: :mri_21 -# Disabled as it conflicts with our /admin/logs routes -# gem 'logster' +# This silly path comment just makes it easier for me to do dev +# will be removed in a few weeks +gem 'logster'#, path: '/home/sam/Source/logster' # perftools only works on 1.9 atm group :profile do diff --git a/Gemfile.lock b/Gemfile.lock index dd493c269..2029aa03d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -151,6 +151,7 @@ GEM thor (~> 0.15) libv8 (3.16.14.3) listen (0.7.3) + logster (0.0.3) lru_redux (0.8.1) mail (2.5.4) mime-types (~> 1.16) @@ -417,6 +418,7 @@ DEPENDENCIES image_sorcery librarian (>= 0.0.25) listen (= 0.7.3) + logster lru_redux message_bus minitest diff --git a/config/application.rb b/config/application.rb index 25c61f177..7ea2b86f5 100644 --- a/config/application.rb +++ b/config/application.rb @@ -128,8 +128,11 @@ module Discourse config.handlebars.templates_root = 'discourse/templates' require 'discourse_redis' + require 'logster/redis_store' # Use redis for our cache config.cache_store = DiscourseRedis.new_redis_store + $redis = DiscourseRedis.new + Logster.store = Logster::RedisStore.new($redis) # we configure rack cache on demand in an initializer # our setup does not use rack cache and instead defers to nginx diff --git a/config/initializers/01-redis.rb b/config/initializers/01-redis.rb index 7fddf4304..73340cc49 100644 --- a/config/initializers/01-redis.rb +++ b/config/initializers/01-redis.rb @@ -1,7 +1,3 @@ -require "#{Rails.root}/lib/discourse_redis" - -$redis = DiscourseRedis.new - if Rails.env.development? && ENV['DISCOURSE_FLUSH_REDIS'] puts "Flushing redis (development mode)" $redis.flushall @@ -16,3 +12,4 @@ if defined?(PhusionPassenger) end end end + diff --git a/config/initializers/06-mini_profiler.rb b/config/initializers/06-mini_profiler.rb index 8c5f385f7..d35d8e85d 100644 --- a/config/initializers/06-mini_profiler.rb +++ b/config/initializers/06-mini_profiler.rb @@ -16,13 +16,15 @@ if defined?(Rack::MiniProfiler) # For our app, let's just show mini profiler always, polling is chatty so nuke that Rack::MiniProfiler.config.pre_authorize_cb = lambda do |env| + path = env['PATH_INFO'] (env['HTTP_USER_AGENT'] !~ /iPad|iPhone|Nexus 7|Android/) && - (env['PATH_INFO'] !~ /^\/message-bus/) && - (env['PATH_INFO'] !~ /topics\/timings/) && - (env['PATH_INFO'] !~ /assets/) && - (env['PATH_INFO'] !~ /qunit/) && - (env['PATH_INFO'] !~ /srv\/status/) && - (env['PATH_INFO'] !~ /commits-widget/) + (path !~ /^\/message-bus/) && + (path !~ /topics\/timings/) && + (path !~ /assets/) && + (path !~ /qunit/) && + (path !~ /srv\/status/) && + (path !~ /commits-widget/) && + (path !~ /^\/logs\/messages/) end # without a user provider our results will use the ip address for namespacing diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index 0cfdbf90f..82494716b 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -42,3 +42,19 @@ if Sidekiq.server? end Sidekiq.logger.level = Logger::WARN + +class LogsterErrorHandler + def call(ex, hash={}) + text = "exception: #{ex}\ncontext: #{hash.inspect}\n" + if ex.backtrace + text << "backtrace: #{ex.backtrace.join("\n")}" + end + Rails.logger.error(text) + rescue => e + Rails.logger.fatal("Failed to log exception #{ex} #{hash}\nReason: #{e}\n#{e.backtrace.join("\n")}") + end +end + +Sidekiq.error_handlers << LogsterErrorHandler.new + + diff --git a/config/initializers/silence_logger.rb b/config/initializers/silence_logger.rb index 2d42bd18b..0a0accb64 100644 --- a/config/initializers/silence_logger.rb +++ b/config/initializers/silence_logger.rb @@ -1,4 +1,7 @@ class SilenceLogger < Rails::Rack::Logger + PATH_INFO = 'PATH_INFO'.freeze + HTTP_X_SILENCE_LOGGER = 'HTTP_X_SILENCE_LOGGER'.freeze + def initialize(app, opts = {}) @app = app @opts = opts @@ -10,11 +13,13 @@ class SilenceLogger < Rails::Rack::Logger def call(env) prev_level = Rails.logger.level + path_info = env[PATH_INFO] - if env['HTTP_X_SILENCE_LOGGER'] || @opts[:silenced].include?(env['PATH_INFO']) + if env[HTTP_X_SILENCE_LOGGER] || + @opts[:silenced].include?(path_info) || + path_info.start_with?('/logs') Rails.logger.level = Logger::WARN - result = @app.call(env) - result + @app.call(env) else super(env) end @@ -23,5 +28,10 @@ class SilenceLogger < Rails::Rack::Logger end end -silenced = ["/mini-profiler-resources/results", "/mini-profiler-resources/includes.js", "/mini-profiler-resources/includes.css", "/mini-profiler-resources/jquery.tmpl.js"] +silenced = [ + "/mini-profiler-resources/results".freeze, + "/mini-profiler-resources/includes.js".freeze, + "/mini-profiler-resources/includes.css".freeze, + "/mini-profiler-resources/jquery.tmpl.js".freeze +] Rails.configuration.middleware.swap Rails::Rack::Logger, SilenceLogger, silenced: silenced diff --git a/config/routes.rb b/config/routes.rb index 3531fe389..9e92d0847 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,6 +16,11 @@ Discourse::Application.routes.draw do mount Sidekiq::Web => "/sidekiq", constraints: AdminConstraint.new + if Rails.env.production? + require 'logster/middleware/viewer' + mount Logster::Middleware::Viewer.new(nil) => "/logs", constraints: AdminConstraint.new + end + get "site" => "site#index" resources :forums