Skip to content

Commit

Permalink
Remove old methods, use a module for the singleton
Browse files Browse the repository at this point in the history
Fixes #3425
  • Loading branch information
ph authored and jordansissel committed Jun 18, 2015
1 parent 8a78be0 commit 280db59
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 57 deletions.
51 changes: 0 additions & 51 deletions lib/logstash/event.rb
Expand Up @@ -218,58 +218,7 @@ def remove(fieldref)
#
# If a %{name} value is an array, then we will join by ','
# If a %{name} value does not exist, then no substitution occurs.
#
# TODO(sissel): It is not clear what the value of a field that
# is an array (or hash?) should be. Join by comma? Something else?
public
def old_sprintf(format)
if format.is_a?(Float) and
(format < MIN_FLOAT_BEFORE_SCI_NOT or format >= MAX_FLOAT_BEFORE_SCI_NOT) then
format = ("%.15f" % format).sub(/0*$/,"")
else
format = format.to_s
end
if format.index("%").nil?
return format
end

return format.gsub(/%\{[^}]+\}/) do |tok|
# Take the inside of the %{ ... }
key = tok[2 ... -1]

if key[0] == "+" && !@data.has_key?(TIMESTAMP)
raise LogStash::Error, "Unable to format \"#{key}\" in string \"#{format}\", #{TIMESTAMP} field not found"
end

if key == "+%s"
# Got %{+%s}, support for unix epoch time
next @data[TIMESTAMP].to_i
elsif key[0,1] == "+"
t = @data[TIMESTAMP]
formatter = org.joda.time.format.DateTimeFormat.forPattern(key[1 .. -1])\
.withZone(org.joda.time.DateTimeZone::UTC)
#next org.joda.time.Instant.new(t.tv_sec * 1000 + t.tv_usec / 1000).toDateTime.toString(formatter)
# Invoke a specific Instant constructor to avoid this warning in JRuby
# > ambiguous Java methods found, using org.joda.time.Instant(long)
org.joda.time.Instant.java_class.constructor(Java::long).new_instance(
t.tv_sec * 1000 + t.tv_usec / 1000
).to_java.toDateTime.toString(formatter)
else
value = self[key]
case value
when nil
tok # leave the %{foo} if this field does not exist in this event.
when Array
value.join(",") # Join by ',' if value is an array
when Hash
LogStash::Json.dump(value) # Convert hashes to json
else
value # otherwise return the value
end # case value
end # 'key' checking
end # format.gsub...
end # def sprintf

def sprintf(format)
LogStash::StringInterpolation.evaluate(self, format)
end
Expand Down
9 changes: 3 additions & 6 deletions lib/logstash/string_interpolation.rb
Expand Up @@ -2,13 +2,14 @@
require "forwardable"

module LogStash
class StringInterpolation
module StringInterpolation
extend self

# Floats outside of these upper and lower bounds are forcibly converted
# to scientific notation by Float#to_s
MIN_FLOAT_BEFORE_SCI_NOT = 0.0001
MAX_FLOAT_BEFORE_SCI_NOT = 1000000000000000.0

INSTANCE = StringInterpolation.new
CACHE = ThreadSafe::Cache.new
TEMPLATE_TAG_REGEXP = /%\{[^}]+\}/

Expand All @@ -25,10 +26,6 @@ def evaluate(event, template)
compiled.evaluate(event)
end

def self.evaluate(event, format)
INSTANCE.evaluate(event, format)
end

private
def not_cachable?(template)
template.index("%").nil?
Expand Down

0 comments on commit 280db59

Please sign in to comment.