diff --git a/config/environments/development.rb b/config/environments/development.rb index 3cfc06f02..0c60b4f36 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -14,6 +14,9 @@ Rails.application.configure do # Do not eager load code on boot. config.eager_load = false + require 'custom_log_formatter' + config.log_formatter = CustomLogFormatter.new + # Show full error reports and disable caching. config.consider_all_requests_local = true config.action_controller.perform_caching = false diff --git a/config/environments/production.rb b/config/environments/production.rb index 5f32d7df6..3a4d4b763 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -10,7 +10,12 @@ Rails.application.configure do # require 'syslog/logger' # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') - config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, 50 * 1024 * 1024) + MAX_LOG_MEGABYTES = 50 + config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, MAX_LOG_MEGABYTES * 1024 * 1024) + + require 'custom_log_formatter' + config.log_formatter = CustomLogFormatter.new + config.logger.formatter = config.log_formatter if ENV["RAILS_LOG_TO_STDOUT"].present? logger = ActiveSupport::Logger.new(STDOUT) diff --git a/lib/custom_log_formatter.rb b/lib/custom_log_formatter.rb new file mode 100644 index 000000000..20fa29017 --- /dev/null +++ b/lib/custom_log_formatter.rb @@ -0,0 +1,18 @@ +class CustomLogFormatter < ActiveSupport::Logger::SimpleFormatter + SEVERITY_TO_COLOR_MAP = {'DEBUG'=>'0;37', 'INFO'=>'32', 'WARN'=>'33', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37'} + + IPRegexp = /\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/ + FilteredString = '**FILTERED**' + + def call(severity, time, progname, msg) + formatted_severity = sprintf("%-5s","#{severity}") + formatted_time = time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..2].rjust(3) + color = SEVERITY_TO_COLOR_MAP[severity] + + "\033[0;37m#{formatted_time}\033[0m [\033[#{color}m#{formatted_severity}\033[0m] #{filter_ip(msg.strip)} (pid:#{$$})\n" + end + + def filter_ip(msg) + msg.gsub(IPRegexp, FilteredString) + end +end