title: conditional matching with grok for logstash
body: <p>I have php log of this format</p>
  
  <pre><code>[Day Mon DD HH:MM:SS YYYY] [Log-Type] [client &lt;ipv4 ip address&gt;] &lt;some php error type&gt;: &lt;other msg with /path/of/a/php/script/file.php and something else&gt;
  [Day Mon DD HH:MM:SS YYYY] [Log-Type] [client &lt;ipv4 ip address&gt;] &lt;some php error type&gt;: &lt;other msg without any file name in it&gt;
  [Day Mon DD HH:MM:SS YYYY] [Log-Type] [client &lt;ipv4 ip address&gt;] &lt;some msg with out semicolon in it but /path/of/a/file inside the message&gt;
  </code></pre>
  
  <p>This I am trying to send to Graylog2 after processing through logstash. Using <a href="https://stackoverflow.com/questions/17331593/parse-apache2-error-logs-with-grok-for-logstash">this post here</a>, I was able to start. now I would like to get some additional fields, so that my final version would look something like this.</p>
  
  <pre><code>{
         "message" =&gt; "&lt;The entire error message goes here&gt;",
        "@version" =&gt; "1",
      "@timestamp" =&gt; "converted timestamp from Day Mon DD HH:MM:SS YYYY",
            "host" =&gt; "&lt;ipv4 ip address&gt;",
         "logtime" =&gt; "Day Mon DD HH:MM:SS YYYY",
        "loglevel" =&gt; "Log-Type",
        "clientip" =&gt; "&lt;ipv4 ip address&gt;",
        "php_error_type" =&gt; "&lt;some php error type&gt;"
        "file_name_from_the_log" =&gt; "/path/of/a/file || /path/of/a/php/script/file.php"
        "errormsg" =&gt; "&lt;the error message after first colon (:) found&gt;"
  }
  </code></pre>
  
  <p>I have the expression for individual line, or atleast I think these should be able to parse, using <a href="https://grokdebug.herokuapp.com" rel="nofollow noreferrer">grokdebugger</a>. something like this:</p>
  
  <pre><code>%{DATA:php_error_type}: %{DATA:message_part1}%{URIPATHPARAM:file_name}%{GREEDYDATA:errormsg}
  %{DATA:php_error_type}: %{GREEDYDATA:errormsg}
  %{DATA:message_part1}%{URIPATHPARAM:file_name}%{GREEDYDATA:errormsg}
  </code></pre>
  
  <p>But somehow I am finding it very difficult to make it work for the entire log file.</p>
  
  <p>Any suggestion please? Also, not sure if there would be any other type of error messages coming in the log file. but the intention is to get the same format for all. Any suggestions how to tackle these logs to get the above mentioned format?</p>