Logging provider failures to a rotating file: enough detail to debug, nothing about the user
When a provider goes down, the first thing I want to know is exactly what happened — and the last thing I want is a log full of my visitors' personal details. Those two wants pull in opposite directions, and getting the
When a provider goes down, the first thing I want to know is exactly what happened — and the last thing I want is a log full of my visitors' personal details. Those two wants pull in opposite directions, and getting the balance right took me longer than I expected while building ramrom.io, my free video downloader.
The problem with logging everything
My first attempt logged the whole provider response, including the URL the visitor pasted. That felt thorough. Then I read one of those logs back and realised what I was actually keeping: the exact video links real people had pasted, complete with their usernames in the query strings, their tracking parameters, and sometimes the name of the account that posted the video. I was storing other people's browsing habits in a plain text file on shared hosting. That was the day the logging got a redesign.
A downloader only needs a URL for a few seconds. Once the resolve call is done, that URL has no business sitting in a file on my server. So the rule became simple: never log the pasted link, never log the video title, and never log anything that came back inside the provider's payload except the pieces I need to debug.
What I do log
Each failure entry is one line, and it carries five things:
- When it happened, in UTC, so I can line it up with the provider's own status page.
- Which platform the visitor asked for — TikTok, Instagram, Facebook, or YouTube.
- Which provider handled it, because I use a different API per platform.
- What kind of failure it was: a timeout, a 429, a 5xx, or a response I could not parse.
- How long the call took, in milliseconds, so I can tell a slow provider from a dead one.
That is enough to answer every question I ask when something breaks. "Is it one platform or all of them?" The platform field answers that. "Is it my code or theirs?" The provider and the failure type answer that. "Has this been getting worse all week?" The timestamps answer that.
The rotating file
The log lives in a single file outside the public web root, and it rotates by size. Once it passes about 200KB, the script renames it to yesterday's file and starts a fresh one, keeping the last seven days. Anything older gets deleted automatically. I did not want a log that grows forever, and I did not want to pay for a logging service on a free tool — plain PHP file operations do the job.
Rotation matters more than people think. A provider that is down for an afternoon can generate thousands of identical failure lines. Without a size cap, one bad day fills the disk quota on a shared plan, and then the whole site breaks for a reason that has nothing to do with the provider.
There is one more detail I am glad I added: the log file is blocked from the web by the same .htaccess rules that protect my config file. A log nobody can read over HTTP is not a leak waiting to happen.
What the log taught me
The best thing the log ever did was stop me blaming my own code. One afternoon the failures spiked and my first instinct was to start rewriting the resolve endpoint. The log said otherwise: every platform, every provider, timeouts across the board — but my own server was answering in under a second. The problem was upstream, not in my code, and the right move was to wait and let the honest "Service is busy" message do its job.
The rule I keep
Log enough to debug, and nothing about the person. If a log line would embarrass me if a visitor read it, it does not belong in the file. That rule has survived every provider outage and every late-night debugging session so far — and the file it produces is still small enough to read in one sitting, which is exactly how a log should be.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.