Skip to content

Feature/logging #2334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ build/
dist/
.build/
.swiftpm/
.vscode
.vscode/*
!.vscode/launch.json

# Android specific
**/android/**/build/
Expand Down Expand Up @@ -162,6 +163,9 @@ app/lib/env/prod_env.g.dart
/backend/scripts/stt/pretrained_models
/backend/scripts/stt/results
/backend/scripts/stt/diarization.json

/backend/syncing/*

/docs/.docusaurus


Expand All @@ -184,3 +188,6 @@ web-report/

lefthook.yml
omiGlass/.expo

# Logs
/backend/logs/
4 changes: 2 additions & 2 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ RUN apt-get update && apt-get -y install ffmpeg curl unzip && rm -rf /var/lib/ap
COPY --from=builder /opt/venv /opt/venv
COPY backend/ .

EXPOSE 8080
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]
EXPOSE 8000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls change the port back to 8080

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
102 changes: 102 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,108 @@ This README provides a quick setup guide for the Omi backend. For a comprehensiv
deactivate
```

## Running with Logging Enabled

1. Start the backend with logging enabled:
Running the server with your virtual environment:

```bash
# Enable logging
source venv/bin/activate && uvicorn main:app --reload --env-file .env
```

2. Or via Docker:

```bash
# Build and run with Docker, then follow logs
docker compose up --build -d && docker compose logs -f
```

### Tracking and Viewing Logs

1. Log files are created in the backend directory:
- `debug.log` - Contains all debug-level and above messages
- `error.log` - Contains only error-level messages

2. Monitor logs in real-time using the terminal:
```bash
# Follow the debug log in real-time
tail -f debug.log

# Follow only error logs
tail -f error.log
```

### Customizing Logging

Logging is configured in `utils/logging_config.py`:
- The file handles log rotation automatically
- Adjust log levels and formats by modifying the Python configuration
- The default format shows: `[timestamp] [level] [module] message`
- Default log level is set to `INFO` unless overridden by the `LOG_LEVEL` environment variable

## Running with Docker

The backend can be easily run using Docker. This approach ensures consistency across different development environments and simplifies the setup process.

### Prerequisites

- Docker installed on your system
- Docker Compose installed on your system

### Running the Backend with Docker

1. Navigate to the backend directory:
```bash
cd backend
```

2. Build and start the container in detached mode:
```bash
docker compose up --build -d
```

This command:
- Builds the Docker image defined in the Dockerfile
- Creates and starts the container in the background
- Uses the environment variables from .dev.env
- Exposes the backend on port 8000

3. View the logs (optional):
```bash
docker compose logs -f
```

The `-f` flag follows the log output in real-time. Press Ctrl+C to stop following the logs.

4. Your backend API is now available at:
```
http://localhost:8000
```

### Managing the Container

- To stop the container:
```bash
docker compose down
```

- To restart the container:
```bash
docker compose restart
```

- To rebuild and restart (after code changes):
```bash
docker compose up --build -d
```

### Notes

- The backend runs on port 8000 by default, matching the behavior of running with uvicorn directly
- The container uses the same environment variables from .dev.env as the local development setup
- Any changes to the code will require rebuilding the container

## Additional Resources

- [Full Backend Setup Documentation](https://docs.omi.me/developer/backend/Backend_Setup)
Expand Down
17 changes: 17 additions & 0 deletions backend/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
backend:
image: omi-backend
build:
context: .
container_name: omi-backend
ports:
- "8000:8000"
env_file:
- .dev.env
restart: unless-stopped
volumes:
- ./logs:/app/logs
environment:
- PYTHONPATH=/app
- LD_LIBRARY_PATH=/usr/local/lib
- LIBRARY_PATH=/usr/local/lib
13 changes: 13 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import logging

import firebase_admin
from fastapi import FastAPI
Expand All @@ -10,6 +11,18 @@
payment, integration, conversations, memories, mcp

from utils.other.timeout import TimeoutMiddleware
from utils.logging_config import setup_logging
from utils.stt.vad import initialize_vad

# Ensure logs directory exists
logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
os.makedirs(logs_dir, exist_ok=True)

# Set up logging
logger = setup_logging()

# Initialize VAD after logging is configured
initialize_vad()

if os.environ.get('SERVICE_ACCOUNT_JSON'):
service_account_info = json.loads(os.environ["SERVICE_ACCOUNT_JSON"])
Expand Down
Loading