From a30303949c602211cdb807a2a8e2889a773c0dd8 Mon Sep 17 00:00:00 2001 From: Nick Ethier Date: Tue, 4 Jun 2013 16:24:00 -0600 Subject: [PATCH] codec support for outputs. converted stdout output to use codecs. --- lib/logstash/outputs/base.rb | 8 ++++++ lib/logstash/outputs/stdout.rb | 48 ++++++++++++++-------------------- logstash.gemspec | 1 + 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/lib/logstash/outputs/base.rb b/lib/logstash/outputs/base.rb index 0a6fcd292..4a087a0b0 100644 --- a/lib/logstash/outputs/base.rb +++ b/lib/logstash/outputs/base.rb @@ -48,6 +48,9 @@ class LogStash::Outputs::Base < LogStash::Plugin # Don't send events that have @timestamp older than specified number of seconds. config :ignore_older_than, :validate => :number, :default => 0 + # The codec used for output data + config :codec, :validate => :string, :default => 'plain' + public def initialize(params) super @@ -72,6 +75,11 @@ class LogStash::Outputs::Base < LogStash::Plugin raise "#{self.class}#receive must be overidden" end # def receive + protected + def enable_codecs + @codec = LogStash::Codecs.for(@codec).new + end + public def handle(event) if event == LogStash::SHUTDOWN diff --git a/lib/logstash/outputs/stdout.rb b/lib/logstash/outputs/stdout.rb index 7f2a44e98..4a0246cf7 100644 --- a/lib/logstash/outputs/stdout.rb +++ b/lib/logstash/outputs/stdout.rb @@ -14,56 +14,46 @@ class LogStash::Outputs::Stdout < LogStash::Outputs::Base config :debug, :validate => :boolean # Debug output format: ruby (default), json - config :debug_format, :default => "ruby", :validate => ["ruby", "json", "dots"] + config :debug_format, :default => "ruby", :validate => ["ruby", "dots"] # The message to emit to stdout. config :message, :validate => :string, :default => "%{+yyyy-MM-dd'T'HH:mm:ss.SSSZ} %{host}: %{message}" public def register + enable_codecs + begin + @codec.format = @message + rescue NoMethodError + end @print_method = method(:ap) rescue method(:p) if @debug case @debug_format when "ruby" - define_singleton_method(:receive) do |event| - return unless output?(event) - if event == LogStash::SHUTDOWN - finished - return - end + @codec.on_event do |event| @print_method.call(event) end - when "json" - define_singleton_method(:receive) do |event| - return unless output?(event) - if event == LogStash::SHUTDOWN - finished - return - end - puts event.to_json - end when "dots" - define_singleton_method(:receive) do |event| - return unless output?(event) - if event == LogStash::SHUTDOWN - finished - return - end + @codec.on_event do |event| $stdout.write(".") end else raise "unknown debug_format #{@debug_format}, this should never happen" end else - define_singleton_method(:receive) do |event| - return unless output?(event) - if event == LogStash::SHUTDOWN - finished - return - end - puts event.sprintf(@message) + @codec.on_event do |event| + puts event end end end + def receive(event) + return unless output?(event) + if event == LogStash::SHUTDOWN + finished + return + end + @codec.encode(event) + end + end # class LogStash::Outputs::Stdout diff --git a/logstash.gemspec b/logstash.gemspec index 355727256..ba0469f7d 100644 --- a/logstash.gemspec +++ b/logstash.gemspec @@ -40,6 +40,7 @@ Gem::Specification.new do |gem| gem.add_runtime_dependency "heroku" #(MIT license) gem.add_runtime_dependency "addressable" #(Apache 2.0 license) gem.add_runtime_dependency "bunny", ["0.8.0"] #(MIT license) + gem.add_runtime_dependency "extlib", ["0.9.16"] #(MIT license) gem.add_runtime_dependency "ffi" #(LGPL-3 license) gem.add_runtime_dependency "ffi-rzmq", ["1.0.0"] #(MIT license) gem.add_runtime_dependency "filewatch", ["0.5.1"] #(BSD license)