Application logs: what to log, where to keep them, how long
What to record in application logs, what to keep out, where to store them, how long to keep them (including under GDPR) and which alerts to set. With a prompt for your agent.
Monday morning. The office manager calls: a big customer’s order has disappeared from the system. You open the app and you don’t know who deleted it, when, or whether anyone deleted it at all. There are logs, but they only hold error messages from the last two days.
For logs to answer that kind of question, decide a few things before the app goes live. Record security events, errors and data changes, in JSON, and in every entry: when, where, who and what. Keep passwords, tokens, session IDs and keys out. Store logs off the app’s server, with restricted access. Match the retention period to the purpose and the law, and keep personal data no longer than needed. Set alerts for serious events.
We, the Drubricks team, mean by logs the records of events an app writes while it runs. A business app needs three kinds:
- error logs: what broke, where in the code and with what data; read by a developer or an agent,
- an audit log: who did what in the app, for example signed in, changed or deleted a record, exported data,
- server logs: HTTP requests, response times, web server and database errors.
What should go into the logs?
OWASP’s logging cheat sheet lists events worth logging every time. The most important ones for a business app:
- successful and failed sign-ins,
- access denials, i.e. attempts to open something the user has no permission for,
- input validation failures,
- application errors and connection problems, for example with the database,
- app start-ups and shut-downs,
- data export and import, including reports,
- access to sensitive data.
Add what matters to your company: changes and deletions of records, permission changes, settings changes.
According to OWASP, every entry records “when, where, who and what”:
- when: date and time of the event; OWASP recommends ISO 8601 with a UTC offset, for example
2026-11-21T09:14:03+01:00, - where: which app, on which server,
- who: the signed-in user or process, plus the source address,
- what: event type, severity and description.
Take severity levels from an existing standard. The syslog protocol (RFC 5424) has eight levels, from 0 (Emergency) to 7 (Debug). A business app usually needs four: Error, Warning, Info and Debug.
OWASP also advises against plain text and recommends a structured format such as JSON. You can search and count such entries without regular expressions:
{"time":"2026-11-21T09:14:03+01:00","level":"info","event":"record.delete",
"app":"orders","user":"anna.smith","ip":"203.0.113.24",
"record":"order/1532","message":"Deleted order 1532"}
This is an illustration. Agree on the field names once and use them across the whole app.
What to keep out of the logs
Logs end up in many places: on the server, in backups, in the team’s tools. According to OWASP, these should usually not go into logs:
- session IDs and access tokens,
- passwords,
- database connection strings,
- encryption keys and other secrets,
- bank account and payment card data,
- sensitive personal data, for example health data or ID document numbers.
Watch out for user input too. Someone can type a newline into a form field and add a fake entry to the log. OWASP calls this log injection and recommends sanitising event data (CR, LF and delimiter characters) and limiting the length of what goes into the log.
Personal data you can’t avoid is a separate matter. The username, email and IP address in the audit log are needed to know who did what. In the Breyer case, the EU Court of Justice held that even a dynamic IP address can be personal data for a website operator. Logs with such data fall under the GDPR, which affects how long you keep them. For GDPR questions, ask whoever is responsible for data protection in the company, or the data protection officer if there is one.
Where to keep logs: off the app’s server, with restricted access
Logs stored only on the app’s server disappear with it. If someone takes over the server, they can change them too. That is why we recommend shipping logs continuously to a separate place: a second server or the logging service of your cloud or hosting provider. Ask your agent how to send your app’s logs there.
OWASP adds a few rules for protecting logs. If your hosting or an external service keeps the logs, it covers some of them for you, so ask the provider:
- detect changes and deletions of entries,
- copy logs to read-only media as soon as possible,
- restrict and periodically review who can read logs, and log access to the logs too,
- keep logs on a separate partition from the operating system, and use a separate database account that only writes logs.
If the app runs on several servers, synchronise their clocks (an NTP service does this; ask your hosting whether it’s on). Without it, the order of events from different machines drifts apart.
Also check where your framework writes logs by default. Usually it’s the app’s database or a file on the same server. That is fine to start with, but the audit log is worth shipping elsewhere anyway.
How long should you keep logs?
There is no single number. OWASP notes that legal, regulatory and contractual obligations may affect the retention period. The GDPR adds the storage limitation principle: data that identifies a person is kept no longer than necessary for the purpose. So for each kind of log, write down the purpose and the period, and after it delete the entries or anonymise them.
As a starting point we recommend this split. It is our recommendation, not a legal requirement; check it against your customer contracts and your legal obligations:
| Logs | Purpose | Period (starting point) |
|---|---|---|
| Debug-level entries (in any kind of log) | tracking down a specific bug | off in production; switched on for a few days while you hunt a bug |
| Error logs | fixing and analysing failures | 30–90 days |
| Server logs | performance, attacks, outages | 30–90 days |
| Audit log | who did what, disputes, incidents | 12 months or more, if a contract or the law requires it |
A long period for the audit log doesn’t have to mean keeping personal data for long. After the period you need for security, you can strip the IP address and email from entries and keep the user ID, time and operation.
A setting like “keep the last 1,000 entries” counts entries, not days. On a busy day or during an attack, 1,000 entries can fill up in an hour and delete exactly what you’re looking for. Set the audit log’s retention in time, not in number of entries.
Who finds out that something is wrong in the logs?
In the 2021 OWASP Top 10 this category was called Security Logging and Monitoring Failures, and its description says plainly that without logging and monitoring, breaches cannot be detected. In the 2025 edition it is called Security Logging and Alerting Failures. OWASP’s cheat sheet recommends notifying the responsible people about more serious events immediately.
In a small company a few rules are enough to start:
- a series of failed sign-ins for one account or from one address,
- a sudden rise in server errors (500 codes),
- repeated access denials for one user,
- bulk deletion or export of records.
For each rule, decide who gets the alert and where: a named person, a phone, or the team chat.
Where to start: a logging policy in one file
Write the decisions down in the repository, for example in docs/logging.md: what you log, what you don’t, where logs are kept, for how long, who has access and what the alerts are. Point your agent to it in the project’s instruction file (for example CLAUDE.md or AGENTS.md) so it follows it with every change. You also have a ready answer when a customer asks about personal data in your logs.
Paste this prompt into your agent:
Review logging in this app and don't change anything yet. Answer with file names:
1. Which events are logged? Are there successful and failed sign-ins, access denials,
errors, changes and deletions of records, exports?
2. Does every entry have a time with UTC offset, user, location, type, level and description? In what format?
3. Do passwords, tokens, session IDs, keys or card data end up in the logs?
4. Is user input sanitised before it is written to a log?
5. Where do logs go, and who has access to them?
6. How long are they kept, and what deletes them?
7. Which events send an alert, and to whom?
Then propose a docs/logging.md file with a logging policy.
We build Drubricks, a free Drupal starter for business apps built with an AI agent: every create, change and delete of a record goes to the Drupal log, and the Config Log module records every configuration change. By default that log sits in the app’s database with an entry limit, so set up shipping it off the server and a retention period in days as described above.