---
title: "Escape line breaks in every logged value · Agent Wranglers"
url: https://agentwranglers.se/playbook/operations/logs/log-injection
id: OPS-76
level: Recommended
updated: 2026-10-27
build: a2a6bdfed830
tokens: 914
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Escape line breaks in every logged value

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Logs](https://agentwranglers.se/playbook/operations/logs.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-27.

- ID: OPS-76
- Level: Recommended
- Chapter: [Logs](https://agentwranglers.se/playbook/operations/logs.md)
- Rule: Write each outside value into its own log field, through an encoder that escapes carriage returns, line feeds and other control characters. Never paste input into the message text.
- Check: A unit test logs the user name `alice\nINFO sign-in ok user=admin`. The output holds exactly one new line for that event, and the value shows the line break as `\n`.
- How: A user name that holds a line break can add a forged line to the log, such as a fake admin sign-in. That misleads whoever reads the log after an attack, and the weakness is listed as CWE-117. A JSON logger escapes control characters inside fields, which is one more reason for [Log structured events](https://agentwranglers.se/playbook/operations/logs/structured-logs.md). A plain text format needs the value escaped before it is written. A log viewer that shows HTML must escape the value as HTML too. The events worth logging for attacks are in [Log attacks without personal data](https://agentwranglers.se/playbook/application-security/errors-logs-and-vulnerabilities/security-logging.md).

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### JavaScript example

```js
import pino from 'pino';

const log = pino();
// The value goes in a field, and the JSON encoder escapes the line break.
log.warn({ event: 'signin.failed', username: 'alice\nINFO sign-in ok user=admin' });
```

### Python example

```python
import json
import logging

class JsonFormatter(logging.Formatter):
    def format(self, record):
        return json.dumps({"level": record.levelname, "event": record.getMessage(), **getattr(record, "fields", {})})

handler = logging.StreamHandler()
handler.setFormatter(JsonFormatter())
log = logging.getLogger("shop")
log.addHandler(handler)
log.warning("signin.failed", extra={"fields": {"username": "alice\nINFO sign-in ok user=admin"}})
```

## Sources

- [OWASP Logging Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html)
- [CWE-117 Improper Output Neutralization for Logs](https://cwe.mitre.org/data/definitions/117.html)

## Related rules

- OPS-6 [Log structured events](https://agentwranglers.se/playbook/operations/logs/structured-logs.md) in Operations and observability
- APS-80 [Log attacks without personal data](https://agentwranglers.se/playbook/application-security/errors-logs-and-vulnerabilities/security-logging.md) in Application security

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/operations/logs/log-injection">Agent Wranglers operations and observability playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Keep personal data out of logs](https://agentwranglers.se/playbook/operations/logs/logs-without-personal-data.md)
- Next: [Keep logs for a fixed time](https://agentwranglers.se/playbook/operations/logs/log-retention.md)
- [Logs](https://agentwranglers.se/playbook/operations/logs.md)
