Mark Pitt
claude-skills
  1. claude-skills
  2. Engineering practices
  3. twelve-factor-app
  4. resources/logs-and-admin.md

A file in twelve-factor-app

resources/logs-and-admin.md

Markdown 9.2 KB
logs-and-admin.mdMarkdown, rendered

Logs and Admin Processes

Covers Factor XI: Logs and Factor XII: Admin Processes — how twelve-factor apps handle observability and one-off management tasks.

---

Factor XI: Logs

Principle

Treat logs as event streams.

A twelve-factor app never concerns itself with the routing or storage of its output stream. It writes each event to stdout, unbuffered, as a stream. The execution environment is responsible for capturing, collating, routing, and archiving that stream.

What Logs Are (and Are Not)

Logs are:

  • A time-ordered stream of events from running processes
  • Written line-by-line to stdout (one event per line)
  • Each line has a timestamp, log level, and message
  • Continuous as long as the app runs — no fixed beginning or end

Logs are not:

  • Files managed by the application
  • Rotated, archived, or moved by the application
  • Written to a specific directory the app controls

stdout vs. Log Files

ApproachTwelve-Factor?Reason
print(message) / console.log(message) to stdout✅ CorrectApp writes; environment routes
logging.basicConfig(filename="/var/log/app.log")❌ ViolationApp managing file routing
winston.createWriteStream("app.log")❌ ViolationApp choosing storage
syslog.info(message)✅ AcceptableSyslog on the host captures and routes
Writing to a named logfile and configuring logrotate❌ ViolationApp is now responsible for log management

Correct Logging Setup

Python

import logging
import sys

# Log to stdout, not to a file
logging.basicConfig(
    stream=sys.stdout,
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s %(message)s"
)

logger = logging.getLogger(__name__)
logger.info("Server started on port %s", port)

Node.js

// console.log and console.error go to stdout/stderr by default
// Use a library that outputs to stdout:
const pino = require("pino");
const logger = pino({ level: process.env.LOG_LEVEL || "info" });

logger.info({ userId: 123, action: "login" }, "User logged in");

Go

import (
    "log"
    "os"
)

// Default log package writes to stderr; set to stdout
logger := log.New(os.Stdout, "", log.LstdFlags)
logger.Println("Server started")

Java / Spring Boot

# application.properties: console appender (stdout) is default
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} [%level] %logger{36} - %msg%n
# Do NOT configure a file appender here

Structured Logging

Structured (JSON) logs improve machine-readability and enable powerful querying in log aggregation systems:

{"timestamp": "2025-11-15T14:32:11Z", "level": "info", "service": "api", "request_id": "abc123", "method": "POST", "path": "/orders", "status": 201, "duration_ms": 45}
{"timestamp": "2025-11-15T14:32:12Z", "level": "error", "service": "api", "request_id": "xyz789", "error": "connection timeout", "database": "postgres"}
# Python: structlog
import structlog
logger = structlog.get_logger()
logger.info("order_created", order_id=42, user_id=7, amount=99.99)
// Node.js: pino (outputs JSON by default)
const logger = require("pino")();
logger.info({ orderId: 42, userId: 7, amount: 99.99 }, "order_created");

Execution Environment Log Routing

The environment (not the app) decides what to do with the stream:

EnvironmentLog Routing Mechanism
Local developmentStream visible in terminal foreground
Herokulogplex — captured and forwarded to log drains
Kuberneteskubelet captures stdout; fluentd/fluentbit forwards
Dockerdocker logs + configured log driver
AWS ECSCloudWatch Logs agent captures stdout
systemdjournald captures stdout automatically

Log drains / aggregation destinations:

  • Elastic Stack (ELK) / OpenSearch
  • Datadog Logs
  • Splunk
  • Loggly
  • AWS CloudWatch
  • Google Cloud Logging
  • Grafana Loki

Log Levels

Use consistent log levels that map to actionable severity:

LevelUse forExample
DEBUGDetailed diagnostics (dev/staging only)SQL query text, full request headers
INFONormal operational eventsRequest handled, user logged in, job completed
WARNDegraded but recoverable stateRetrying failed request, deprecation usage
ERRORFailure requiring investigationUnhandled exception, database connection failed
FATAL/CRITICALApp cannot continueCannot connect to required backing service

Control the active level via an environment variable:

LOG_LEVEL=debug   # development
LOG_LEVEL=info    # production default
LOG_LEVEL=warn    # high-traffic production (reduce noise)

---

Factor XII: Admin Processes

Principle

Run admin/management tasks as one-off processes.

Database migrations, data import/export scripts, REPL sessions, and one-time fix scripts are not part of the regular process formation (web, worker). They run as one-off processes in an identical environment to the app's regular processes.

What Counts as an Admin Process

Task TypeExamples
Database migrationsmanage.py migrate, rake db:migrate, flyway migrate
Interactive console / REPLrails console, python manage.py shell, flask shell
One-time data scriptspython scripts/backfill_timestamps.py
Report generationrake report:monthly
Cache warmingpython scripts/warm_cache.py
Seed datarails db:seed
Manual fixespython scripts/fix_bad_records.py

Key Rules for Admin Processes

1. Run against the same release Admin processes run against the same codebase and config as the regular long-running processes — not a different version.

# Wrong: running a migration against a different version
git checkout v1.3 && python manage.py migrate   # mismatch with running app

# Right: run the migration as part of the deploy, before restarting the app
# Using the same release artifact

2. Use the same dependency isolation

# Ruby: use bundle exec to ensure the correct dependencies
bundle exec rake db:migrate   # not: rake db:migrate

# Python: activate the same virtualenv used by the app
source .venv/bin/activate && python manage.py migrate

# Docker: run inside the same container image as the app
docker run --rm myapp:v1.2.3 python manage.py migrate

3. Admin code ships with application code

Admin scripts must live in the application repository to avoid synchronisation issues. A script that assumes schema version N must deploy alongside the code that requires schema version N.

Running Admin Processes

Local development:

# Direct shell command inside the app's checkout directory
python manage.py migrate
rails console
flask db upgrade
node scripts/import_data.js

Remote production (Heroku-style):

# SSH-equivalent: run inside the execution environment
heroku run python manage.py migrate
heroku run rails console
heroku run bash   # interactive shell inside a dyno

Kubernetes:

# One-off pod with the same image and config as the running app
kubectl run migrate --image=myapp:v1.2.3 --rm -it \
  --env="DATABASE_URL=$DATABASE_URL" \
  -- python manage.py migrate

Migration Strategy in CI/CD

A common pattern is to run migrations as part of the deploy pipeline, after the new build is deployed but before traffic is switched to the new app version:

# GitHub Actions deploy job
- name: Run database migrations
  run: |
    docker run --rm \
      -e DATABASE_URL=${{ secrets.DATABASE_URL }} \
      myapp:${{ github.sha }} \
      python manage.py migrate --no-input

- name: Deploy new version
  run: kubectl set image deployment/web app=myapp:${{ github.sha }}

Migration safety in rolling deployments:

When running multiple app versions simultaneously (during a rolling deploy), migrations must be backward-compatible with the previous version. Follow the expand-contract pattern:

  1. Expand: Add new columns/tables as optional (backward-compatible)
  2. Deploy new code that uses new columns
  3. Contract: Remove old columns in a subsequent migration (after old code is gone)

Twelve-Factor Strongly Favours REPL Languages

Languages with first-class REPL support make twelve-factor admin processes easier:

LanguageREPLFramework Console
Pythonpythonflask shell, django shell_plus
Rubyirbrails console
Node.jsnodecustom repl.js scripts
Elixiriexiex -S mix
ClojurecljnREPL / lein repl
Scalascalasbt console

Common Violations

ViolationRiskFix
Running migrations at app startupRace condition on multi-instance deploy; slow startup (Factor IX)Run as pre-deploy one-off process
Admin script with hardcoded database URLCredentials in source code (Factor III)Read from env var
Admin code in a separate repoVersion mismatch between app and migrationsCo-locate in same repo
Using a different Python/Ruby version for admin tasksDependency discrepanciesUse the same container image
Interactive REPL in production connected to live DB (without care)Accidental data mutationUse read replicas where possible; always test destructive queries first