NalleRooth.com

Don't log HTTP 4XX as Errors

HTTP 4XX is a perfectly valid result to ignore - if the source of the error is a bad request. Those cases should be handled in code and should not affect your data or the stability of the system.

Even if the response status code is HTTP 4XX, the system didn’t encounter a real error. It just wasn’t happy with whatever the user was trying to do. To the user, it may look like an error, but the system still works as intended. At this stage, a helpful error message should allow the user to make a new attempt. Hopefully with a greater level of success.

A typo in an URL or incomplete form data is not something you want to handle via the server logs - it’s something you should find when testing you system.

4XX Example: Attempting to order a pizza at McDonald’s will not prevent anyone else from ordering from the menu. The restaurant is not broken.

Sadly, a lot of web frameworks have a hard time distinguishing between user errors and system errors. They’re all logged as errors. From the user’s perspective, this makes sense; whatever they wanted to do didn’t work, and they get an error message. But we, as developers, are left with logs looking like this:

Timestamp Error Exception ... Not Found /wp-admin/index.php
Timestamp Error Exception ... Invalid email format

Filtering the log by log level == error quickly tells us that something is wrong with this setup.

HTTP 5XX statuses on the other hand are errors that occur because the system is unable to handle something. It may help to retry a failed connection, or read from another database replica, but something is still broken, misconfigured or not reachable over the network.

Those things should be handled in code - if possible. But when the system is unable to reach the database, or missing write permissions on the local cache path - someone needs to sit down and fix it. At those times you only want the real errors in the system log. The ones which allows you to understand - and fix the issue.

5XX Example: Due to a blown fuse, the cash registers are offline and nobody can order anything at the restaurant. The restaurant is broken.

At this time, you want to know that the solution is to go to the fuse box. You don’t care about any pizza orders. So don’t hide your most important logs in an ocean of noise. Move your 4XX logs to the info or warning log level (or you know, track them in a better way outside the system logs) and let your real errors be important.

Posted:
Tags: development software logs
comments powered by Disqus