Permalink
Browse files

Max number of loglines stored can now get configured by setting Metri…

…csAgent#max_lines_per_logger (defaults to 50)
  • Loading branch information...
paukul committed Nov 15, 2010
1 parent f1f11c5 commit 27e759bd4f5de858a1b9ae29cae94a63cf073773
Showing with 34 additions and 2 deletions.
  1. +17 −2 lib/amqp_logging/metrics_agent.rb
  2. +17 −0 test/metrics_agent_test.rb
@@ -3,7 +3,9 @@
module AMQPLogging
class MetricsAgent
+ DEFAULT_MAX_LINES_PER_LOGGER = 50
attr_reader :fields
+ attr_accessor :max_lines_per_logger
def initialize
@default_fields = {
@@ -13,6 +15,7 @@ def initialize
:default => []
}
}
+ @max_lines_per_logger = DEFAULT_MAX_LINES_PER_LOGGER
@logger_types = {}
reset_fields
end
@@ -42,8 +45,19 @@ def []=(fieldname, value)
def add_logline(severity, message, progname, logger)
t = Time.now
- msg = (message || progname).strip
- @fields[:loglines][@logger_types[logger]] << [severity, t.strftime("%Y-%m-%dT%H:%M:%S.#{t.usec}"), msg]
+ timestring = t.strftime("%Y-%m-%dT%H:%M:%S.#{t.usec}")
+ logtype = @logger_types[logger]
+ lines = @fields[:loglines][logtype]
+ if !@truncated_status[logtype] && lines.size < @max_lines_per_logger
+ msg = (message || progname).strip
+ lines << [severity, timestring, msg]
+ true
+ else
+ msg = "Loglines truncated to #{@max_lines_per_logger} lines (MetricsAgent#max_lines_per_logger)"
+ lines[-1] = [Logger::INFO, timestring, msg]
+ @truncated_status[logtype] = true
+ false
+ end
end
def wrap_logger(logger, type = :default)
@@ -72,6 +86,7 @@ def reset_fields
@fields = {
}.merge!(@default_fields)
@logger_types.values.each {|logtype| @fields[:loglines][logtype] = []}
+ @truncated_status = {}
end
module MetricsAgentSupport
View
@@ -119,5 +119,22 @@ def setup
@proxy.debug "\n\nfoo\n\n"
assert_equal "foo", @agent[:loglines][:default][-1][2]
end
+
+ test "should have a limit of loglines per logger after which they will get ignored" do
+ @agent.max_lines_per_logger = 2
+ @logger.debug "foo"
+ @logger.debug "bar"
+ no_lines_before = @agent[:loglines][:default].size
+ @logger.debug "baz"
+ assert_equal no_lines_before, @agent[:loglines][:default].size
+ end
+
+ test "should replace the last logged line with a truncation note if the limit of loglines is exceeded" do
+ @agent.max_lines_per_logger = 1
+ @logger.debug "foo"
+ assert_equal "foo", @agent[:loglines][:default].last[2]
+ @logger.debug "bar"
+ assert_match /truncated/, @agent[:loglines][:default].last[2]
+ end
end
end

0 comments on commit 27e759b

Please sign in to comment.