# Production logs setup (DigitalOcean /var/www/boundary-fastapiandnextjs)

On the server the API may not be running as a systemd service yet, so `/var/log/plagis/` and `journalctl -u plagis-aumentum-api` don’t exist. Use one of the options below.

---

## Copy-paste: create log dirs on the server (no scripts needed)

Run these on the server. They don’t require any script files.

```bash
# API stdout/stderr (if you start the API with redirect later)
sudo mkdir -p /var/log/plagis
sudo touch /var/log/plagis/api.log /var/log/plagis/api-error.log

# Auth log (used by the API once it runs the latest code)
sudo mkdir -p /var/www/boundary-fastapiandnextjs/logs
sudo touch /var/www/boundary-fastapiandnextjs/logs/auth.log
sudo chmod 644 /var/www/boundary-fastapiandnextjs/logs/auth.log
```

Then you can run `tail -f` on any of these without "No such file or directory". The files will stay empty until something writes to them (e.g. after you deploy and restart the API for auth.log).

---

## Port 8001 in use = backend already running

If you see **`address already in use`** when trying to start the API again, that’s expected: the backend is already running on port 8001. Do **not** kill that process. To see logs, use whatever method was used to start it (e.g. tail the log files it was started with, or use systemd/journalctl if it runs as a service). For future restarts you can start it with output to `/var/log/plagis/` as in the sections below.

---

## Auth activity log (login / logout / auth/me)

The API logs all auth activity with a clear `[AUTH]` prefix so you can see why a user might be logged out immediately after login.

**Dedicated auth log file:** the API creates `logs/auth.log` when it starts (after you deploy the latest code and restart the API). To create the directory and file on the server so `tail -f` works even before the next restart, run:

```bash
sudo mkdir -p /var/www/boundary-fastapiandnextjs/logs
sudo touch /var/www/boundary-fastapiandnextjs/logs/auth.log
sudo chmod 644 /var/www/boundary-fastapiandnextjs/logs/auth.log
```

Then watch auth (file will fill once the API is restarted with the new code):

```bash
tail -f /var/www/boundary-fastapiandnextjs/logs/auth.log
```

Example lines:
- `POST /auth/login: attempt username=admin`
- `POST /auth/login: success username=admin`
- `GET /auth/me: success username=admin` or `GET /auth/me: invalid or expired token -> 401`
- `POST /auth/logout: username=admin`

If you see **login success** followed by **GET /auth/me: ... -> 401**, the frontend is calling `/auth/me` and the backend is rejecting the token (invalid/expired) or the user lookup failed; that causes the immediate redirect to login. Use the exact reason in the log (e.g. "user not found in DB") to fix the backend or data.

---

## Quick fix: create log dir so `tail -f` works

On the server run:

```bash
sudo mkdir -p /var/log/plagis
sudo touch /var/log/plagis/api.log /var/log/plagis/api-error.log
```

Then **start the API** (see Option A or B below). Once the API is running, logs will appear and `tail -f` will show them:

```bash
sudo tail -f /var/log/plagis/api.log /var/log/plagis/api-error.log
```

Or use the script: `sudo bash /var/www/boundary-fastapiandnextjs/scripts/create_log_dir.sh`

---

## Option A: Use systemd (recommended)

This creates `/var/log/plagis/` and runs the API as a service so logs are written to files and journalctl.

**1. On the server (as root):**

```bash
cd /var/www/boundary-fastapiandnextjs
chmod +x scripts/setup_api_logging_do.sh
./scripts/setup_api_logging_do.sh
```

**2. Ensure venv exists** (if you see “venv/bin/python3 not found”):

```bash
cd /var/www/boundary-fastapiandnextjs
python3 -m venv venv
venv/bin/pip install -r requirements.txt
```

Then run `./scripts/setup_api_logging_do.sh` again.

**3. View logs:**

```bash
sudo tail -f /var/log/plagis/api.log /var/log/plagis/api-error.log
# or
sudo journalctl -u plagis-aumentum-api -f
```

**4. Control the service:**

```bash
sudo systemctl status plagis-aumentum-api
sudo systemctl restart plagis-aumentum-api
sudo systemctl stop plagis-aumentum-api
```

The service file used is `scripts/plagis-aumentum-api-varwww.service` (WorkingDirectory and ExecStart point to `/var/www/boundary-fastapiandnextjs`, logs to `/var/log/plagis/`).

---

## Option B: Manual run with logs in project directory

If you prefer not to use systemd yet:

**1. Create a log directory in the project:**

```bash
cd /var/www/boundary-fastapiandnextjs
mkdir -p logs
```

**2. Start the API and send output to logs:**

```bash
cd /var/www/boundary-fastapiandnextjs
nohup venv/bin/python3 aumentum_api.py >> logs/api.log 2>> logs/api-error.log &
```

**3. View logs:**

```bash
tail -f /var/www/boundary-fastapiandnextjs/logs/api.log
tail -f /var/www/boundary-fastapiandnextjs/logs/api-error.log
```

---

## If you use the existing START_API.sh

`START_API.sh` runs from the **scripts** folder and was writing to `scripts/server.log` and using `venv` inside **scripts**, so `venv/bin/python3` was missing. Fix by running the API from the **project root** and using the project’s venv:

From project root:

```bash
cd /var/www/boundary-fastapiandnextjs
nohup venv/bin/python3 aumentum_api.py >> logs/api.log 2>> logs/api-error.log &
```

Then:

```bash
tail -f /var/www/boundary-fastapiandnextjs/logs/api.log
```
