Class: BufferingLogger::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/buffering_logger/railtie.rb

Class Method Summary collapse

Class Method Details

.install(transform: nil, device: nil, sync: true, request_id: true, warn_log_tags: true, simple_formatter: true) ⇒ Object



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
56
57
58
59
60
61
62
63
64
# File 'lib/buffering_logger/railtie.rb', line 8

def self.install(
  transform: nil, device: nil, sync: true, request_id: true,
  warn_log_tags: true, simple_formatter: true
)
  initializer :buffering_logger, :before => :initialize_logger do |app|
    device ||= begin
      # Does mostly the same things that Rails does. See http://git.io/2v9FxQ

      path = app.paths["log"].first

      unless File.exist? File.dirname path
        FileUtils.mkdir_p File.dirname path
      end

      file = File.open(path, 'a')
      file.binmode
      file
    end

    device.sync = true if sync && device.respond_to?(:sync=)

    logger = BufferingLogger::Logger.new(device)
    logger.formatter = if simple_formatter
      ActiveSupport::Logger::SimpleFormatter.new
    else
      app.config.log_formatter
    end
    logger = ActiveSupport::TaggedLogging.new(logger)

    app.config.logger = logger

    # We insert this at the very beginning so that all logs, even from other
    # middleware, get buffered together.
    app.config.middleware.insert(
      0,
      BufferingLogger::RackBuffer,
      logger,
      transform: transform,
    )

    # Log the request_id
    if request_id
      app.config.middleware.insert_after(
        Rails::Rack::Logger,
        BufferingLogger::RailsRackLogRequestId,
      )
    end

    if warn_log_tags && app.config.log_tags.present?
      puts(<<~TEXT.squish)
        NOTE: You're using `Rails.application.config.log_tags` with
        BufferingLogger. We recommend disabling these when using
        BufferingLogger. See the README for more info.
      TEXT
    end
  end
end