Files
multi_camera/app/__init__.py

47 lines
1.3 KiB
Python

"""
Flask应用工厂
"""
import logging
from flask import Flask
from .config import DEBUG, HOST, PORT
from .routes.main import register_main_routes
def create_app():
"""创建Flask应用实例"""
app = Flask(__name__,
static_folder='../static',
template_folder='templates')
# 配置
app.config['DEBUG'] = DEBUG
# 配置日志
configure_logging(app)
# 注册路由
register_main_routes(app)
return app
def configure_logging(app):
"""配置日志"""
if not app.debug:
# 在生产环境中,将日志输出到文件
import logging
import os
from logging.handlers import RotatingFileHandler
# 日志目录
log_dir = './logs'
if not os.path.exists(log_dir):
os.makedirs(log_dir)
log_file = os.path.join(log_dir, 'multi_camera.log')
file_handler = RotatingFileHandler(log_file, maxBytes=10240, backupCount=10)
file_handler.setFormatter(logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s'
))
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO)
app.logger.info('多摄像头监控系统启动')