I have PHP configured to log all of its errors to a single log file. I always have Terminal open and tail
watching this file. However, sometimes I don’t realize that new errors have been logged right away, which is annoying. To overcome this, I thought it would be nice if I could get a popup notification each time there was an error, just like many Mac apps do. I found this thread on SO, which helped me solve my problem.
If you have Homebrew installed, all you need to do is run:
$ brew install terminal-notifier $ brew install fswatch
Then add this function to your .bash_profile
:
notify-php-logs() { fswatch -0 ~/zebug.log | xargs -0 -n 1 \ terminal-notifier -title "PHP Error" \ -message "New errors in zebug.log" \ -group "php-errors" \ -activate "com.apple.Terminal" }
Then run:
$ notify-php-logs >/dev/null 2>&1 & $ tail -f ~/zebug.log
You’ll want to replace ~/zebug.log
with the path of the log file that you want to listen to, of course.
Now whenever there is a PHP error, you will get a notification, and when you click on it the Terminal app will be brought to the front so you can see the error.