Class: TruncateLogsFormatter

Inherits:
ActiveSupport::Logger::SimpleFormatter
  • Object
show all
Includes:
ActiveSupport::TaggedLogging::Formatter
Defined in:
lib/truncate_logs_formatter.rb

Overview

This log formatter limits the number of characters in each log message to prevent malicious requests from filling up the disk in a short amount of time. The number of characters is determined by the ‘log_line_max_chars` global setting which can be configured via the `DISCOURSE_MAX_LOG_LINES` environment variable or via the `discourse_defaults.conf` file.

Instance Method Summary collapse

Constructor Details

#initialize(log_line_max_chars:) ⇒ TruncateLogsFormatter

Returns a new instance of TruncateLogsFormatter.



9
10
11
# File 'lib/truncate_logs_formatter.rb', line 9

def initialize(log_line_max_chars:)
  @log_line_max_chars = log_line_max_chars
end

Instance Method Details

#call(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/truncate_logs_formatter.rb', line 13

def call(*args)
  # Lograge formatters are only called with a single argument instead of the usual 4 arguments of `severity`, `datetime`, `progname` and `message`.
  message =
    if args.length == 1
      args[0]
    else
      args[3]
    end

  if message.length > @log_line_max_chars
    newlines = message.length - message.chomp.length
    "#{message[0, @log_line_max_chars]}...(truncated)#{"\n" * newlines}"
  else
    message
  end
end