programing

LogStash에서의 JSON 사용

css3 2023. 3. 21. 22:27

LogStash에서의 JSON 사용

난 지금 제정신이 아니야.나는 파일에 로그를 쓰는 앱을 가지고 있다.각 로그 엔트리는 JSON 객체입니다..json 파일의 예는 다음과 같습니다.

{"Property 1":"value A","Property 2":"value B"}
{"Property 1":"value x","Property 2":"value y"}

로그 엔트리를 LogStash로 가져오려고 필사적으로 노력하고 있습니다.이를 위해 다음 LogStash 구성 파일을 만들었습니다.

input {
  file {
    type => "json"
    path => "/logs/mylogs.log"
    codec => "json"
  }
}
output {
  file {
    path => "/logs/out.log"
  }
}

현재 mylogs.log에 수동으로 레코드를 추가하여 작업을 시도하고 있습니다.하지만, 그들은 묘하게 스투우트에 나타난다.open out.log 를 참조하면 다음과 같은 것이 표시됩니다.

{"message":"\"Property 1\":\"value A\", \"Property 2\":\"value B\"}","@version":"1","@timestamp":"2014-04-08T15:33:07.519Z","type":"json","host":"ip-[myAddress]","path":"/logs/mylogs.log"}

따라서 ElasticSearch로 메시지를 보내면 필드가 표시되지 않습니다.그 대신 나는 혼란스러워진다.난 내 소유물이 여전히 소유물이길 원해.나는 그것들이 메시지 부분이나 출력에 주입되는 것을 원하지 않는다.코덱과 관련이 있는 것 같아요아직은 잘 모르겠어요.로그 저장 입력 설정에서 코덱을 변경해야 할지 모르겠습니다.또는 출력 구성의 입력을 변경해야 하는지 여부.

json 코덱을 삭제하고 json 필터를 추가합니다.

input {
  file {
    type => "json"
    path => "/logs/mylogs.log"
  }
}
filter{
    json{
        source => "message"
    }
}
output {
  file {
    path => "/logs/out.log"
  }
}

소스 JSON을 디코딩하는 것은 아니지만 입력을 필터링하여 @message 필드에서만 JSON 데이터를 가져오도록 하기 때문에 json 코덱은 필요하지 않습니다.

기본적으로는 json 코덱이 지정되지 않은 경우 tcp는 모든 것을 메시지필드에 저장합니다.

json 코덱을 지정한 후 메시지필드의 _json parsefailure에 대한 회피책도 다음과 같이 수정할 수 있습니다.

input {
  tcp {
    port => '9563'
  }
}
filter{
  json{
    source => "message"
    target => "myroot"
  }
  json{
    source => "myroot"
  }

}
output {
    elasticsearch {
      hosts => [ "localhost:9200" ]
    }
}

메시지 필드를 적절한 json 문자열로 해석하여 myroot 필드를 해석하고 myroot를 구문 분석하여 json을 생성합니다.

메시지와 같은 다중 필드를 삭제할 수 있습니다.

filter {
  json {
    source => "message"
    remove_field => ["message"]
  }
}

이걸로 시험해 보세요.

filter {
  json {
        source => "message"
        target => "jsoncontent" # with multiple layers structure
  }
}

언급URL : https://stackoverflow.com/questions/22941739/using-json-with-logstash