2

I'm trying to write a script that returns stack traces from heroku logs.

Here's an example output I would like to capture:

```PHP Fatal error:  Uncaught exception 'Exception' with message 'Invalid authentication source: name' in /app/simplesamlphp/lib/SimpleSAML/Auth/Default.php:61
2018-10-18T20:36:09.617037+00:00 app[web.1]: Stack trace:
2018-10-18T20:36:09.617384+00:00 app[web.1]: #0 /app/simplesamlphp/lib/SimpleSAML/Auth/Simple.php(136): SimpleSAML_Auth_Default::initLogin('sp-name', '...', NULL, Array)
2018-10-18T20:36:09.617681+00:00 app[web.1]: #1 /app/config.php(21): SimpleSAML_Auth_Simple->login(Array)
2018-10-18T20:36:09.618083+00:00 app[web.1]: #2 /app/config.php(38): getSAMLUser('urn:oasis:names...')
2018-10-18T20:36:09.618337+00:00 app[web.1]: #3 /app/login.php(35): getSAMLUserErrorChecking('...')
2018-10-18T20:36:09.618343+00:00 app[web.1]: #4 {main}
2018-10-18T20:36:09.618465+00:00 app[web.1]:   thrown in /app/simplesamlphp/lib/SimpleSAML/Auth/Default.php on line 61
2018-10-18T20:38:48.323247+00:00 heroku[router]:
```

I've been trying to use the following to capture the important bits:

heroku logs -t -a appname | grep '(?<=PHP Fatal error)(.*)(?=router)'

edit Here's a proof of concept for the regex I'm using here: https://regex101.com/r/EpCwE0/1

  • 1
    Try `grep -Poz '(?<=PHP Fatal error).*?(?=router)'` – Wiktor Stribiżew Oct 18 '18 at 20:43
  • @WiktorStribiżew running that as the filter returns: ``` usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]] [-e pattern] [-f file] [--binary-files=value] [--color=when] [--context[=num]] [--directories=action] [--label] [--line-buffered] [--null] [pattern] [file ...] ``` For some reason I don't have the -Poz options? Perhaps it's because I'm on mac os – Jay Jackson Oct 18 '18 at 20:48
  • Ah, you need `pcregrep`. Install [via homebrew](https://stackoverflow.com/questions/44112312/how-do-i-install-pcre-with-homebrew). – Wiktor Stribiżew Oct 18 '18 at 20:50
  • @WiktorStribiżew It appears that I have it installed, I tried running: `heroku logs -t -a redacted | pcregrep -Poz '(?<=PHP Fatal error).*?(?=router)'` returns: ```pcregrep: Unknown option letter 'P' in "-Poz" Usage: pcregrep [-AaBCcDdeFfHhIilLMNnoqrsuVvwx] [long options] [pattern] [files] Type `pcregrep --help' for more information and the long options. ``` – Jay Jackson Oct 18 '18 at 20:52
  • Yeah, you do not need `P` I guess, since it is already `pcregrep`. – Wiktor Stribiżew Oct 18 '18 at 20:57
  • Sadly, `heroku logs -t -a redacted | pcregrep -o '(?<=PHP Fatal error).*?(?=router)'` didn't catch anything, : / – Jay Jackson Oct 18 '18 at 21:01
  • ``` heroku logs -t -a redacted | pcregrep -oz '(?<=PHP Fatal error).*?(?=router)' pcregrep: Unknown option letter 'z' in "-oz" Usage: pcregrep [-AaBCcDdeFfHhIilLMNnoqrsuVvwx] [long options] [pattern] [files] Type `pcregrep --help' for more information and the long options. ``` – Jay Jackson Oct 18 '18 at 21:03
  • Try `sed -n '/^PHP Fatal error/,/router/p'` instead – Wiktor Stribiżew Oct 18 '18 at 21:06
  • @WiktorStribiżew Does `pcregrep` have a `z` option? – revo Oct 18 '18 at 21:10
  • 1
    Hey @WiktorStribiżew thanks for the suggestion, but this didn't catch the result either. Hmm.. – Jay Jackson Oct 18 '18 at 21:12
  • pcregrep does not have a `z` option – Jay Jackson Oct 18 '18 at 21:12
  • remove `^` from the suggested `sed` command. – revo Oct 18 '18 at 21:19
  • Is `PHP Fatal error` at the start of a line? How many such matches in the file do you expect: one or more? Is it ok to only return a whole block of lines with the delimiters containing inside them? – Wiktor Stribiżew Oct 18 '18 at 21:33
  • 2
    @revo It worked!!! – Jay Jackson Oct 18 '18 at 21:33
  • @WiktorStribiżew after removing the `^` from the pattern, I got the expected result :))) – Jay Jackson Oct 18 '18 at 21:34
  • 1
    That is because it was not clear if that text is at the start of the line or not. – Wiktor Stribiżew Oct 18 '18 at 21:34
  • 2
    @revo You should write up an answer since OP says your alternative solution worked – thedouglenz Oct 19 '18 at 12:30
  • I don't think this is a duplicate because the problem isn't just select between two strings.. It's selecting over a stream of input... – Jay Jackson Oct 19 '18 at 12:57

0 Answers0