简体   繁体   中英

Logstash TCP input w/ JSON codec treats each line as a separate event

I'm trying to read log4j v2.3 JSON output via a Logstash TCP socket using the logstash JSON codec, but Logstash is treating each line as a separate event to be indexed rather than reading each JSON object as an event.

log4j config

<Appenders>
    <Console name="console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d %p [%c] - &lt;%m&gt;%n"/>
    </Console>
    ... removed for brevity ...
    <Socket name="logstash" host="localhost" port="4560">
      <JSONLayout />
    </Socket>
</Appenders>
<Loggers>
    <Logger name="org.jasig" level="info" additivity="false">
        <AppenderRef ref="console"/>
        <AppenderRef ref="file"/>
        <AppenderRef ref="logstash"/>
    </Logger>
    ... removed for brevity ...
    <Root level="error">
        <AppenderRef ref="console"/>
        <AppenderRef ref="logstash"/>
    </Root>
</Loggers>

Logstash Config

input {
  tcp {
      port => 4560
      codec => json
  }
}
output {
  elasticsearch {}
  stdout {}
}

Logstash Output

each line is parsed as an individual even rather than treating the entire JSON object as a single event.

2016-03-22T01:24:27.213Z 127.0.0.1 {
2016-03-22T01:24:27.215Z 127.0.0.1   "timeMillis" : 1458609867060,
2016-03-22T01:24:27.216Z 127.0.0.1   "thread" : "localhost-startStop-1",
2016-03-22T01:24:27.217Z 127.0.0.1   "level" : "INFO",
2016-03-22T01:24:27.218Z 127.0.0.1   "loggerName" : "com.hazelcast.instance.DefaultAddressPicker",
2016-03-22T01:24:27.219Z 127.0.0.1   "message" : "[LOCAL] [dev] [3.5] Resolving domain name 'wozniak.local' to address(es): [192.168.0.16, fe80:0:0:0:6203:8ff:fe89:6d3a%4]\n",
2016-03-22T01:24:27.220Z 127.0.0.1   "endOfBatch" : false,
2016-03-22T01:24:27.221Z 127.0.0.1   "loggerFqcn" : "org.apache.logging.slf4j.Log4jLogger"
2016-03-22T01:24:27.222Z 127.0.0.1 }
2016-03-22T01:24:32.281Z 127.0.0.1 {
2016-03-22T01:24:32.283Z 127.0.0.1   "timeMillis" : 1458609872279,
2016-03-22T01:24:32.286Z 127.0.0.1   "thread" : "localhost-startStop-1",
2016-03-22T01:24:32.287Z 127.0.0.1   "level" : "WARN",
2016-03-22T01:24:32.289Z 127.0.0.1   "loggerName" : "com.hazelcast.instance.DefaultAddressPicker",
2016-03-22T01:24:32.294Z 127.0.0.1   "message" : "[LOCAL] [dev] [3.5] Cannot resolve hostname: 'Jons-MacBook-Pro-2.local'\n",
2016-03-22T01:24:32.299Z 127.0.0.1   "endOfBatch" : false,
2016-03-22T01:24:32.302Z 127.0.0.1   "loggerFqcn" : "org.apache.logging.slf4j.Log4jLogger"
2016-03-22T01:24:32.307Z 127.0.0.1 }

Thank you in advance for your help.

Well, I got this to work. It isn't how I wanted to solve it, but it does work.

Rather than using the json codec, I used the multiline codec for input and the json filter.

logstash config

input {
  tcp {
      port => 4560
      codec => multiline {
        pattern => "^\{$"
        negate => true
        what => previous
      }  
  }
}

filter {
  json { source => message }
}

output {
  elasticsearch {}
  stdout {}
}

and here's the properly indexed output

2016-03-22T09:42:26.880Z 127.0.0.1 0 expired tickets found to be removed.
2016-03-22T09:43:26.992Z 127.0.0.1 Finished ticket cleanup.
2016-03-22T09:43:47.120Z 127.0.0.1 Setting path for cookies to: /cas/ 
2016-03-22T09:43:47.122Z 127.0.0.1 AcceptUsersAuthenticationHandler successfully authenticated hashbrowns+password
2016-03-22T09:43:47.131Z 127.0.0.1 Authenticated hashbrowns with credentials [hashbrowns+password].
2016-03-22T09:43:47.186Z 127.0.0.1 Audit trail record BEGIN
=============================================================
WHO: hashbrowns+password
WHAT: supplied credentials: [hashbrowns+password]
ACTION: AUTHENTICATION_SUCCESS
APPLICATION: CAS
WHEN: Tue Mar 22 05:43:47 EDT 2016
CLIENT IP ADDRESS: 0:0:0:0:0:0:0:1
SERVER IP ADDRESS: 0:0:0:0:0:0:0:1
=============================================================

This seems a little fragile because it counts on the way log4j formats the json, so I would still love to hear how to get the json codec working with multiline json output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM