diff --git a/.gitignore b/.gitignore index 55c3426..9e09f24 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ __pycache__/ .idea/ # OS -.DS_Store" >> .gitignore \ No newline at end of file +.DS_Store" >> .gitignore +*.log \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py index 073f4aa..2d70e30 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,12 +1,22 @@ +import logging from flask import Flask +from flask_cors import CORS from app.api.v1.controllers import api_v1 from app.api.v1.zhipu_controller import zhipu_controller from app.config import Config def create_app(): app = Flask(__name__) - app.config.from_object(Config) + CORS(app) + app.config.from_object(Config) + + # Set up logging using configuration from Config + logging.basicConfig(**Config.LOGGING_CONFIG) + logger = logging.getLogger(__name__) + + logger.info("Application started") + # Register Blueprints app.register_blueprint(api_v1, url_prefix='/api/v1') @@ -15,4 +25,5 @@ def create_app(): # Register Zhipu-related routes under /api/v1/zhipu app.register_blueprint(zhipu_controller, url_prefix='/api/v1') + logger.info("Application setup completed") return app diff --git a/app/api/v1/zhipu_controller.py b/app/api/v1/zhipu_controller.py index 9c647d2..02b8ee4 100644 --- a/app/api/v1/zhipu_controller.py +++ b/app/api/v1/zhipu_controller.py @@ -1,9 +1,11 @@ +import logging from flask import Blueprint, request, Response from app.services.zhipu_service import ZhipuService from app.utils.prompt_repository import PromptRepository # Add this import zhipu_controller = Blueprint('zhipu_controller', __name__) zhipu_service = ZhipuService() +logger = logging.getLogger(__name__) @zhipu_controller.route('/zhipu/stream', methods=['POST']) def stream_sse(): diff --git a/app/config.py b/app/config.py index 0b943b1..eba8230 100644 --- a/app/config.py +++ b/app/config.py @@ -1,6 +1,15 @@ import os +import logging class Config: SECRET_KEY = os.environ.get('SECRET_KEY') or 'supersecretkey' SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///app.db' SQLALCHEMY_TRACK_MODIFICATIONS = False + + # Logging configuration + LOGGING_CONFIG = { + 'level': logging.INFO, + 'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s', + 'filename': 'app.log', + 'filemode': 'a' + } diff --git a/app/services/zhipu_service.py b/app/services/zhipu_service.py index fffa937..a5cfdce 100644 --- a/app/services/zhipu_service.py +++ b/app/services/zhipu_service.py @@ -1,6 +1,9 @@ from zhipuai import ZhipuAI +import logging +logger = logging.getLogger(__name__) + class ZhipuService: def __init__(self): self.model_name = "glm-4" diff --git a/manage.py b/manage.py index 61b9d38..12838dd 100644 --- a/manage.py +++ b/manage.py @@ -1,6 +1,11 @@ +import logging from app import create_app +logger = logging.getLogger(__name__) + app = create_app() if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000) + logger.info("Starting the application") + app.run(host='0.0.0.0', port=5001) + logger.info("Application stopped") diff --git a/requirements.txt b/requirements.txt index 5b57cea..6444f43 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ alembic==1.13.3 blinker==1.8.2 click==8.1.7 Flask==3.0.3 +Flask_Cors==5.0.0 Flask-Migrate==4.0.7 Flask-SQLAlchemy==3.1.1 importlib_metadata==8.5.0