File: quiet_logger.rb

package info (click to toggle)
ruby-sinatra 4.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,944 kB
  • sloc: ruby: 17,702; sh: 25; makefile: 8
file content (55 lines) | stat: -rw-r--r-- 1,553 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

module Sinatra
  # = Sinatra::QuietLogger
  #
  # QuietLogger extension allows you to define paths excluded
  # from logging using the +quiet_logger_prefixes+ setting.
  # It is inspired from rails quiet_logger, but handles multiple paths.
  #
  # == Usage
  #
  # === Classic Application
  #
  # You have to require the quiet_logger, set the prefixes
  # and register the extension in your application.
  #
  #     require 'sinatra'
  #     require 'sinatra/quiet_logger'
  #
  #     set :quiet_logger_prefixes, %w(css js images fonts)
  #     register Sinatra::QuietLogger
  #
  # === Modular Application
  #
  # The same for modular application:
  #
  #     require 'sinatra/base'
  #     require 'sinatra/quiet_logger'
  #
  #     set :quiet_logger_prefixes, %w(css js images fonts)
  #
  #     class App < Sinatra::Base
  #       register Sinatra::QuietLogger
  #     end
  #
  module QuietLogger
    def self.registered(app)
      quiet_logger_prefixes = begin
        app.settings.quiet_logger_prefixes.join('|')
      rescue StandardError
        ''
      end
      return warn('You need to specify the paths you wish to exclude from logging via `set :quiet_logger_prefixes, %w(images css fonts)`') if quiet_logger_prefixes.empty?

      const_set('QUIET_LOGGER_REGEX', %r(\A/{0,2}(?:#{quiet_logger_prefixes})))
      ::Rack::CommonLogger.prepend(
        ::Module.new do
          def log(env, *)
            super unless env['PATH_INFO'] =~ QUIET_LOGGER_REGEX
          end
        end
      )
    end
  end
end