Skip to content

PHP error logs, eh? Isn’t it great when you are so busy you just forget to look at them for a while, then you do only to find they they have been spammed with errors for a couple of weeks that have gone unnoticed because the host error recovery has kicked in and saved the site?

Well, that happened me recently, so I thought: Well why not just build a little plugin that will check the error logs for me and print the errors in the dashboard? Then I’ll never miss another error/warning again (hopefully!). So that’s what I did.

What it does

De-duplication

Each distinct problem is one row with a count, first and last seen, file and line, and an expandable stack trace. I don’t want potentially hundreds and hundreds of the same PHP error getting in the way of my debugging. Numbers are masked, so the 11 “Undefined array key N” warnings became one row. It was a nice one-liner to mask the numbers like so:

return md5($severity.'|'.preg_replace('/\d+/', '#', $message).'|'.$file.'|'.$line);

What it reports

Fatal errors and warnings, plus WordPress database errors. For now, I think this is all I want to include to keep it lightweight, but in future I can see it being expanded to other logs by simply by adding another class.

Deprecations, notices and wp-cli’s own noise are hidden by default, but I can toggle that on the settings page.

It can read a 50 MB log in 0.1 s by only looking at the bits that have changed. It remembers a byte offset plus the file’s inode, so it only reads what’s new and notices when the log has been rotated.

$resume = $state['path'] === $path && $state['inode'] === $inode && $state['offset'] <= $size;
$offset = $resume ? $state['offset'] : max(0, $size - self::FIRST_SCAN_BYTES);

Dashboard widget 

I wanted this to notify me easily and clearly when something was wrong (without actually worrying about email notifications), so I made a dashboard widget that colour-codes the errors by severity.

The title shows the open count, for example “SC Monitor — 7 open issues” and it’s only visible to administrators.

Dismissible rows

Hide the rows that have been fixed/addressed. If the same problem is logged again, it comes back flagged as “Returned”. So you know you’ve got some more work to do!

Retention

Rows are forgotten after 30 days without recurrence and capped at 150, so it doesn’t just keep growing forever.

WP-CLI

Finally, wp sc-monitor scan and wp sc-monitor issues show the same data over SSH, so I can use them for the periodic checks quickly and easily.

Is it a resource hog?

No. On a synthetic 50 MB log with 263,000 deprecation lines:

  • First scan: 0.10 s and about 13 MB of memory. It reads only the last 2 MB, which held 10,569 entries, and those became 3 rows.
  • Hourly after that: 2–5 ms, because it only checks the file size when nothing new was logged. It remembers where it stopped, and it detects log rotation.

Have a look yourself…

It’s available for download on my plugins page and check out the github repo.