Logging Strategy: What to Log, Where, and for How Long?

Last winter — while I was still working on the fleet systems side — the server of a vehicle tracking system we were looking into had stopped at 03:14 in the morning. When the phones started ringing, the team was discussing the possibility of an attack; the truth turned out to be far more mundane. Log files had filled 540 GB of the 600 GB disk. And here's the truly painful part: inside those 540 GB we couldn't find a single useful line explaining why the system had slowed down. They were logging everything but getting answers to nothing.

This picture is not an exception. In the projects that have passed through my hands since 2010, the two extremes I see most often are these: either there's no logging to speak of and when something breaks everyone gropes around in the dark, or every line of code writes a log and the signal gets lost in the noise. Both lead to the same place — when something goes wrong in production, you can't answer "what happened?" in a reasonable amount of time.

A Log Is an Answer, Not a Diary

The question to ask when you start logging shouldn't be "what should we log?" but "what questions will we be asking three months from now?" "Why did customer X's payment fail yesterday?", "What time did this endpoint start slowing down?", "Who changed this record, and when?" If your logging strategy can answer these questions within minutes, you're on the right track; if it can't, you're just piling up terabytes of files, and that's all.

In practice, assigning a correlation ID to every request is the backbone of this work. A request coming from the mobile app should carry the same ID as it travels to the API, from there to the queue, and from there to a third-party service. Last month, thanks to this, we found a payment integration bug on our platform in four minutes; years ago, on a project without correlation IDs, I spent two days hunting down a similar problem. The difference wasn't software quality; it was logging discipline.

So what should be logged at a minimum? Our line is this: a single summary line at the end of every request — who, which endpoint, how many milliseconds, which status code. On top of that, the result and duration of every external service call; because in half of the "the system is slow" complaints, the culprit isn't our code but a third-party API that takes eight seconds to respond, and if you don't have the data to prove it, the argument drags on for weeks. And finally, business-critical state changes: order status, payment status, permission changes. Once this three-part core is in place, the rest takes shape according to the project.

The minimum logging core: correlation ID, request summary line, the result and duration of external service calls, and critical business state changes.
The question is not what to log, but what questions we will be asking three months from now.

Then there's the message itself. A log line that says "An error occurred" is only slightly better than one that was never written. Which operation, with which input, at which step, hit which error — without these, that line is just taking up space.

Let's Be Honest About Levels

Log levels are a topic everyone knows in theory and almost no one uses correctly in practice. The typical picture in the projects I've inherited over the years: everything is either INFO or ERROR. Yet the distinction rests on a single question: when this line is written, does someone need to do something?

  • ERROR: Someone needs to look at it. If it won't wake anyone up at night, it's probably not an ERROR.
  • WARN: Not right now, but a problem if it piles up; requests falling into retry, quotas nearing their limit.
  • INFO: Events that tell the story of the business flow; order created, payment approved, vehicle entered its route.
  • DEBUG: For development and temporary diagnosis; off by default in production.

Years ago, at one client, 40 thousand lines a day were piling up at ERROR level. We asked: "How many of these do you look at?" The answer: "None, there are too many." If nobody looks at the ERROR channel, that channel is dead; and when a real disaster arrives, nobody notices. That is exactly what we call alert fatigue.

Log levels from top to bottom: ERROR, WARN, INFO and DEBUG; the distinction rests on whether someone needs to act when the line is written.
The distinction rests on one question: when this line is written, does someone need to do something?

Don't Leave Logs in Files, but Don't Ship Everything to a Central System Either

On a small single-server project, writing to a file and rotating it with logrotate is a perfectly legitimate solution. But the moment a second server comes online, wandering through files over SSH becomes torture. A centralized log system — on our platform, Loki sits next to Grafana; for small jobs, managed services are enough too — is no longer considered a luxury.

Let me add a budget warning here: the bill for centralized log systems grows with volume. Shipping the DEBUG level to the central system means paying every month for data that will never be queried. Our formula goes like this: ERROR and WARN always go to the central system, INFO is kept sampled or with a short retention period, and DEBUG is a tap you only open while chasing down a problem.

This is also where structured logging comes in. There's a world of difference between grepping for the sentence "User 4521 completed the order" and querying the user_id=4521 field inside JSON. Also, don't confuse metrics with logs: "how many requests per minute are we getting?" is a job for metrics, and trying to count that from logs is both expensive and slow. Logs tell you "what happened"; metrics tell you "how much." In projects that squeeze both into the same pipe, both work poorly. We've completely stopped writing plain-text logs in new services; in the legacy services on the platform, this is usually the first place we touch.

Plain-text logs are searched with grep, while structured logs can be queried by field; logs tell what happened, metrics tell how much.
We have completely stopped writing plain-text logs in new services.

Retention Is Also a Legal Question

There's no single answer to "how long should we keep them?" For access and traffic records, regulation comes into play; the retention periods for records covered by Law No. 5651 (Turkey's internet law) are defined, and KVKK (Turkey's personal data protection law) applies to every log that contains personal data. On the other hand, keeping application logs that contain personal data for years is a risk in itself — if your log files leak, you get burned as badly as if your database had leaked. Writing fields such as Turkish national ID numbers, phone numbers or card numbers to logs unmasked is an automatic rejection reason in code review on our team.

Our practical formula: debugging logs 14-30 days, business events and audit records 1-2 years, regulated records for as long as the law says. And these periods should live in a written policy; "I think we keep them forever" isn't a strategy, it's a confession.

Humans Aren't the Only Ones Reading Logs Anymore

Over the past year, the nature of the work has changed significantly. In our system, the first one to look at the logs is now often not a human but an AI agent connected to the log system via MCP. Before the on-call developer is woken up, the agent scans the relevant time window, finds similar past incidents, and writes up its hypothesis. Finding a ready-made preliminary analysis while drinking your morning coffee (I thought this was hype too, until I tried it) has completely changed our on-call culture.

But this setup only works if the logs are structured, levels are used honestly, and there's a correlation ID. An agent fed with messy logs drowns just like a human does; it just drowns faster and more expensively. Good logging is no longer just for your own sleep at night; it's also the eyes and ears of the AI working alongside you.

If you're going to review your project's logging infrastructure, start with three questions: is there a correlation ID, does anyone actually look at the ERROR channel, are retention periods written down? I've seen every version of this picture over sixteen years; yours won't surprise me. I'm always up for a conversation about logging; the contact page is open.