Skip to content

Commit 183037f

Browse files
seapaganclaude
andcommitted
docs: add comprehensive Admin Pages and Logging configuration documentation
Added detailed documentation for all Admin Pages and Logging environment variables to docs/usage/configuration/environment.md. Admin Pages Configuration (5 variables): - ADMIN_PAGES_ENABLED: Enable/disable admin panel - ADMIN_PAGES_ROUTE: Customize admin panel route - ADMIN_PAGES_TITLE: Customize browser/page title - ADMIN_PAGES_ENCRYPTION_KEY: Session encryption key (with security warnings) - ADMIN_PAGES_TIMEOUT: Session timeout configuration Logging Configuration (8 variables): - LOG_PATH: Log output directory - LOG_LEVEL: Logging verbosity (DEBUG/INFO/WARNING/ERROR/CRITICAL) - LOG_ROTATION: When to rotate log files - LOG_RETENTION: How long to keep old logs - LOG_COMPRESSION: Compression format for rotated logs - LOG_CATEGORIES: Fine-grained category control (ALL/NONE/REQUESTS/AUTH/etc.) - LOG_FILENAME: Custom log filename - LOG_CONSOLE_ENABLED: Enable console output (with duplicate warning) Documentation includes: - Clear explanations for each setting - Security warnings for sensitive settings (encryption key) - Multiple practical configuration examples (dev, production, minimal) - Production recommendations for LOG_CATEGORIES - Troubleshooting section for common issues - Cross-references to admin-panel.md Added ~380 lines of comprehensive documentation following existing style. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 5cfd068 commit 183037f

File tree

1 file changed

+380
-0
lines changed

1 file changed

+380
-0
lines changed

docs/usage/configuration/environment.md

Lines changed: 380 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,386 @@ MAIL_SERVER=smtp.mailserver.com
217217
MAIL_FROM_NAME="FastAPI Template"
218218
```
219219

220+
## Configure Admin Pages (Optional)
221+
222+
The API includes an optional admin panel for managing users and API keys through a web interface. It's disabled by default and must be explicitly enabled.
223+
224+
!!! info "Admin Panel Access"
225+
Only existing admin users can access the admin panel. See the [Admin Panel Documentation](../admin-panel.md) for details on usage and features.
226+
227+
### Enable Admin Pages
228+
229+
Set this to `True` to enable the admin panel web interface:
230+
231+
```ini
232+
ADMIN_PAGES_ENABLED=True
233+
```
234+
235+
When enabled, the admin panel will be accessible at the route specified in `ADMIN_PAGES_ROUTE`. When disabled (default), the admin routes return a 404 error.
236+
237+
### Customize Admin Pages Route
238+
239+
The admin panel is accessible at `/admin` by default. You can customize this:
240+
241+
```ini
242+
ADMIN_PAGES_ROUTE=/admin
243+
```
244+
245+
You can change this to any route you prefer, for example `/management` or `/dashboard`. The route must start with a forward slash (`/`).
246+
247+
### Customize Admin Panel Title
248+
249+
The title shown in the browser tab and page header:
250+
251+
```ini
252+
ADMIN_PAGES_TITLE="API Administration"
253+
```
254+
255+
Customize this to match your application name or branding.
256+
257+
### Session Encryption Key
258+
259+
!!! danger "Critical Security Setting"
260+
This key encrypts admin session tokens. Treat it like your SECRET_KEY - keep it secret, unique per environment, never commit to version control, and regenerate if compromised.
261+
262+
The encryption key for admin session tokens:
263+
264+
```ini
265+
ADMIN_PAGES_ENCRYPTION_KEY=
266+
```
267+
268+
**Behavior:**
269+
270+
- **Empty (default):** Auto-generates a new key on each server startup
271+
- Sessions are invalidated when the server restarts
272+
- Admins must re-login after each restart
273+
- Fine for development
274+
- **Set with a key:** Persistent sessions across server restarts
275+
- Admins stay logged in through restarts
276+
- **Required for production**
277+
278+
**Generate a persistent key:**
279+
280+
Using the CLI (recommended):
281+
282+
```console
283+
$ api-admin keys -a
284+
```
285+
286+
Or manually with Python:
287+
288+
```console
289+
$ python -c 'from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())'
290+
bXlfc2VjcmV0X2VuY3J5cHRpb25fa2V5X2V4YW1wbGU=
291+
```
292+
293+
Then add to your `.env`:
294+
295+
```ini
296+
ADMIN_PAGES_ENCRYPTION_KEY=bXlfc2VjcmV0X2VuY3J5cHRpb25fa2V5X2V4YW1wbGU=
297+
```
298+
299+
!!! tip "Production Recommendation"
300+
Always set ADMIN_PAGES_ENCRYPTION_KEY in production to maintain persistent sessions. Use different keys for dev/staging/production environments.
301+
302+
### Session Timeout
303+
304+
How long (in seconds) before admin sessions expire:
305+
306+
```ini
307+
ADMIN_PAGES_TIMEOUT=86400
308+
```
309+
310+
Default is 86400 seconds (24 hours). Adjust based on your security requirements:
311+
312+
- Higher values: Better user experience, less re-authentication
313+
- Lower values: Better security, more frequent re-authentication
314+
315+
Common values:
316+
317+
- `43200` - 12 hours
318+
- `86400` - 24 hours (default)
319+
- `604800` - 7 days
320+
321+
### Complete Admin Pages Example
322+
323+
```ini
324+
# Enable admin panel
325+
ADMIN_PAGES_ENABLED=True
326+
ADMIN_PAGES_ROUTE=/admin
327+
ADMIN_PAGES_TITLE="My API Administration"
328+
ADMIN_PAGES_ENCRYPTION_KEY=your_generated_encryption_key_here
329+
ADMIN_PAGES_TIMEOUT=86400
330+
```
331+
332+
## Configure Logging (Optional)
333+
334+
The API uses loguru for structured logging with rotation, retention, and category-based filtering. Logging provides debugging, monitoring, and compliance capabilities.
335+
336+
!!! note "Console Logging"
337+
FastAPI/Uvicorn already log to console. The file-based logging configured here is in addition to that and provides persistent, categorized logs.
338+
339+
### Log Output Directory
340+
341+
Directory where log files are written:
342+
343+
```ini
344+
LOG_PATH=./logs
345+
```
346+
347+
Default is `./logs` (relative to project root). The directory must be writable by the application process. You can use absolute paths for production:
348+
349+
```ini
350+
LOG_PATH=/var/log/myapi
351+
```
352+
353+
### Logging Level
354+
355+
Controls which log messages are captured:
356+
357+
```ini
358+
LOG_LEVEL=INFO
359+
```
360+
361+
Available levels (from most to least verbose):
362+
363+
| Level | Description | Use Case |
364+
|----------|--------------------------------------|-----------------------------|
365+
| DEBUG | All messages including debug output | Development/troubleshooting |
366+
| INFO | Normal operations and events | Production (recommended) |
367+
| WARNING | Issues that may need attention | Production monitoring |
368+
| ERROR | Error conditions | Production (minimal) |
369+
| CRITICAL | Severe system failures | Production (minimal) |
370+
371+
Higher levels include all messages from lower levels (ERROR includes CRITICAL, INFO includes WARNING/ERROR/CRITICAL, etc.).
372+
373+
### Log File Rotation
374+
375+
When to rotate (start a new) log file:
376+
377+
```ini
378+
LOG_ROTATION=1 day
379+
```
380+
381+
Prevents log files from growing unbounded. Supported formats:
382+
383+
**Size-based rotation:**
384+
385+
- `"100 MB"`, `"500 MB"`, `"1 GB"`
386+
387+
**Time-based rotation:**
388+
389+
- `"1 day"`, `"1 week"`, `"1 month"`, `"1 year"`
390+
391+
Examples by scenario:
392+
393+
- High-traffic API: `"100 MB"` or `"6 hours"`
394+
- Standard API: `"1 day"` (default)
395+
- Low-traffic API: `"1 week"`
396+
397+
### Log File Retention
398+
399+
How long to keep old rotated log files:
400+
401+
```ini
402+
LOG_RETENTION=30 days
403+
```
404+
405+
Old log files are automatically deleted after this period. Examples:
406+
407+
- Development: `"7 days"`
408+
- Production: `"30 days"` or `"90 days"`
409+
- Compliance requirements: `"1 year"` or more
410+
411+
Balance storage costs with compliance and debugging needs.
412+
413+
### Log File Compression
414+
415+
Compression format for rotated log files:
416+
417+
```ini
418+
LOG_COMPRESSION=zip
419+
```
420+
421+
Options:
422+
423+
- `"zip"` - ZIP compression (recommended, widely compatible)
424+
- `"gz"` - gzip compression (smaller than zip)
425+
- `"bz2"` - bzip2 compression (smaller than gz, slower)
426+
- `""` or omit - No compression (not recommended)
427+
428+
Compression saves significant disk space for archived logs, especially with high verbosity.
429+
430+
### Logging Categories
431+
432+
Fine-grained control over what gets logged:
433+
434+
```ini
435+
LOG_CATEGORIES=ALL
436+
```
437+
438+
**Special values:**
439+
440+
- `ALL` - Log everything (development/debugging)
441+
- `NONE` - Disable all logging (except critical errors)
442+
443+
**Individual categories (combine with commas):**
444+
445+
| Category | What It Logs | Recommended For |
446+
|------------|---------------------------------------|------------------------|
447+
| REQUESTS | HTTP request/response logging | Debugging, monitoring |
448+
| AUTH | Authentication, login, token ops | **Production (security)** |
449+
| DATABASE | Database CRUD operations | Debugging queries |
450+
| EMAIL | Email sending operations | **Production (if using email)** |
451+
| ERRORS | Error conditions and exceptions | **Production (always)** |
452+
| ADMIN | Admin panel operations | **Production (audit)** |
453+
| API_KEYS | API key operations | Production (security) |
454+
455+
**Configuration examples:**
456+
457+
```ini
458+
# Development: log everything
459+
LOG_CATEGORIES=ALL
460+
461+
# Production: security-focused
462+
LOG_CATEGORIES=ERRORS,AUTH,ADMIN,EMAIL
463+
464+
# Production: minimal logging
465+
LOG_CATEGORIES=ERRORS
466+
467+
# Debugging database issues
468+
LOG_CATEGORIES=ERRORS,DATABASE,REQUESTS
469+
470+
# Comprehensive monitoring
471+
LOG_CATEGORIES=ERRORS,AUTH,ADMIN,EMAIL,DATABASE
472+
473+
# Disable all logging
474+
LOG_CATEGORIES=NONE
475+
```
476+
477+
!!! tip "Production Recommendation"
478+
For production, use `LOG_CATEGORIES=ERRORS,AUTH,ADMIN,EMAIL` with `LOG_LEVEL=INFO`. This provides security monitoring (AUTH/ADMIN), error tracking (ERRORS), and email operation logging (EMAIL) while keeping log files manageable.
479+
480+
**How combinations work:**
481+
482+
- Comma-separated values are combined (bitwise OR)
483+
- Order doesn't matter: `AUTH,ERRORS` = `ERRORS,AUTH`
484+
- Case-insensitive: `auth` = `AUTH`
485+
- Whitespace is trimmed: `AUTH, ERRORS` works fine
486+
487+
### Log Filename
488+
489+
Custom filename for the log file:
490+
491+
```ini
492+
LOG_FILENAME=api.log
493+
```
494+
495+
Default is `api.log`. The filename **cannot contain path separators** (`/` or `\`) - use `LOG_PATH` to set the directory.
496+
497+
Useful for separating logs by environment:
498+
499+
```ini
500+
# In .env for production
501+
LOG_FILENAME=api.log
502+
503+
# In test config
504+
LOG_FILENAME=test_api.log
505+
```
506+
507+
The full log path will be: `{LOG_PATH}/{LOG_FILENAME}`
508+
509+
### Console Logging
510+
511+
Enable console output in addition to file logging:
512+
513+
```ini
514+
LOG_CONSOLE_ENABLED=false
515+
```
516+
517+
!!! warning "Duplicate Console Output"
518+
FastAPI/Uvicorn already log to console. Setting this to `true` causes **duplicate console output** - each log message appears twice in the console. Only enable if you have a specific reason (e.g., custom log formatting, Docker/Kubernetes setups).
519+
520+
**When to use:**
521+
522+
- ✅ Custom log formatting requirements
523+
- ✅ Centralized logging systems that only capture console
524+
- ✅ Docker/Kubernetes environments without file access
525+
526+
**When NOT to use:**
527+
528+
- ❌ Local development (already has console output)
529+
- ❌ High-traffic APIs (performance impact)
530+
- ❌ Default installations (causes confusion)
531+
532+
### Complete Logging Configuration Examples
533+
534+
**Development configuration:**
535+
536+
```ini
537+
LOG_PATH=./logs
538+
LOG_LEVEL=DEBUG
539+
LOG_ROTATION=1 day
540+
LOG_RETENTION=7 days
541+
LOG_COMPRESSION=zip
542+
LOG_CATEGORIES=ALL
543+
LOG_FILENAME=api.log
544+
LOG_CONSOLE_ENABLED=false
545+
```
546+
547+
**Production configuration (security-focused):**
548+
549+
```ini
550+
LOG_PATH=/var/log/myapi
551+
LOG_LEVEL=INFO
552+
LOG_ROTATION=1 day
553+
LOG_RETENTION=90 days
554+
LOG_COMPRESSION=zip
555+
LOG_CATEGORIES=ERRORS,AUTH,ADMIN,EMAIL
556+
LOG_FILENAME=api.log
557+
LOG_CONSOLE_ENABLED=false
558+
```
559+
560+
**Production configuration (minimal):**
561+
562+
```ini
563+
LOG_PATH=/var/log/myapi
564+
LOG_LEVEL=ERROR
565+
LOG_ROTATION=1 day
566+
LOG_RETENTION=30 days
567+
LOG_COMPRESSION=zip
568+
LOG_CATEGORIES=ERRORS
569+
LOG_FILENAME=api.log
570+
LOG_CONSOLE_ENABLED=false
571+
```
572+
573+
### Troubleshooting Logging
574+
575+
**Log files not created:**
576+
577+
- Check `LOG_PATH` directory exists and is writable
578+
- Check file permissions on the directory
579+
- Verify the application process has write access
580+
581+
**Log files filling disk:**
582+
583+
- Adjust `LOG_ROTATION` to rotate more frequently
584+
- Reduce `LOG_RETENTION` to delete old logs sooner
585+
- Use `LOG_COMPRESSION` to save space
586+
- Reduce `LOG_LEVEL` to ERROR or WARNING
587+
- Limit `LOG_CATEGORIES` to only what you need
588+
589+
**Missing expected logs:**
590+
591+
- Check `LOG_CATEGORIES` includes the category you're looking for
592+
- Verify `LOG_LEVEL` isn't filtering out messages
593+
- Check logs are being written to the correct file path
594+
- Ensure the operation you're logging is actually executing
595+
596+
**Duplicate console output:**
597+
598+
- Set `LOG_CONSOLE_ENABLED=false` (this is the default)
599+
220600
## Example full `.env` file
221601

222602
Below is a full .env file. This can also be found in the root of the API as

0 commit comments

Comments
 (0)