Skip to content

Instantly share code, notes, and snippets.

@jakebathman
Created August 19, 2018 00:06
Show Gist options
  • Star 87 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save jakebathman/a1122071428c78942c6bed4cade18dd6 to your computer and use it in GitHub Desktop.
Save jakebathman/a1122071428c78942c6bed4cade18dd6 to your computer and use it in GitHub Desktop.
Tail Laravel logs and filter out the stack traces
tail -f -n 450 storage/logs/laravel*.log \
| grep -i -E \
"^\[\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}\]|Next [\w\W]+?\:" \
--color
@andonovn
Copy link

Thanks 👍

@jsilva74
Copy link

+1 Thanks a lot!

@ajtrichards
Copy link

Awesome. Thanks for this

@sineld
Copy link

sineld commented Aug 20, 2018

A bit modified for faster commands:

function log.l {
    echo "Printing contents of Laravel Log File"
    echo "Press Ctrl + C to exit"
    echo "Tip: Type 'log.c' to clear contents of Laravel Log File"
    tail -f -n 450 storage/logs/laravel*.log \
  | grep -i -E \
    "^\[\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}\]|Next [\w\W]+?\:" \
    --color
}

function log.c {
    echo "Clearing contents of Laravel Log File"
    echo > storage/logs/laravel*.log
}

@torkiljohnsen
Copy link

Thought I'd try sticking this into a composer script, but can't get any output.

I have scripts/tail_laravel_log.sh:

tail -f -n 450 storage/logs/laravel*.log \
  | grep -i -E \
    "^\[\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}\]|Next [\w\W]+?\:"

And then in composer.json:

  "scripts": {
    "logtail": [
      "./scripts/tail_laravel_log.sh"
    ]
  }

Running ./scripts/tail_laravel_log.sh works like a charm, but running composer logtail produces no output. Any ideas?

@secmohammed
Copy link

secmohammed commented Jul 22, 2019

on opening a new tab it will check for the file Laravel log file and will throw an error that file doesn't exist, which is annoying
so I guess it would be better to check if the directory actually exists first before reading/writing from the file
NOTE: this scenario will happen if you adding these two functions two your .zshrc file or .bashrc file, so I had to add an extra condition.

function log_laravel {
    DIRECTORY=storage/logs/
    if [[ -d "$DIRECTORY" ]]; then
        echo "Printing contents of Laravel Log File"
        tail -f -n 450 storage/logs/laravel*.log \
      | grep -i -E \
        "^\[\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}\]|Next [\w\W]+?\:" \
        --color
    fi
}

function log_clear {
    DIRECTORY=storage/logs/
    if [[ -d "$DIRECTORY" ]]; then
        echo > storage/logs/laravel*.log
    fi

}


hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment