Class: LogStash::Codecs::JSON
- Inherits:
-
Base
- Object
- Base
- LogStash::Codecs::JSON
- Defined in:
- lib/logstash/codecs/json.rb
Overview
This codec may be used to decode (via inputs) and encode (via outputs) full JSON messages. If the data being sent is a JSON array at its root multiple events will be created (one per element).
If you are streaming JSON messages delimited by ‘n’ then see the ‘json_lines` codec.
Encoding will result in a compact JSON representation (no line terminators or indentation)
If this codec recieves a payload from an input that is not valid JSON, then it will fall back to plain text and add a tag ‘_jsonparsefailure`. Upon a JSON failure, the payload will be stored in the `message` field.
Instance Method Summary collapse
Instance Method Details
#decode(data) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/logstash/codecs/json.rb', line 39 def decode(data) data = @converter.convert(data) begin decoded = LogStash::Json.load(data) if decoded.is_a?(Array) decoded.each {|item| yield(LogStash::Event.new(item)) } elsif decoded.is_a?(Hash) yield LogStash::Event.new(decoded) else @logger.info? && @logger.info("JSON codec received a scalar instead of an Arary or Object!", :data => data) yield LogStash::Event.new("message" => data, "tags" => ["_jsonparsefailure"]) end rescue LogStash::Json::ParserError => e @logger.info("JSON parse failure. Falling back to plain-text", :error => e, :data => data) yield LogStash::Event.new("message" => data, "tags" => ["_jsonparsefailure"]) rescue StandardError => e # This should NEVER happen. But hubris has been the cause of many pipeline breaking things # If something bad should happen we just don't want to crash logstash here. @logger.warn("An unexpected error occurred parsing input to JSON", :input => data, :message => e., :class => e.class.name, :backtrace => e.backtrace) end end |
#encode(event) ⇒ Object
67 68 69 |
# File 'lib/logstash/codecs/json.rb', line 67 def encode(event) @on_event.call(event, event.to_json) end |
#register ⇒ Object
33 34 35 36 |
# File 'lib/logstash/codecs/json.rb', line 33 def register @converter = LogStash::Util::Charset.new(@charset) @converter.logger = @logger end |