2024-10-22 09:00:44 +08:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import sys
|
2024-10-30 11:18:23 +08:00
|
|
|
from datetime import datetime
|
2024-10-22 09:00:44 +08:00
|
|
|
from logging.handlers import RotatingFileHandler
|
|
|
|
|
2024-10-30 11:18:23 +08:00
|
|
|
import pytz
|
2024-10-22 09:00:44 +08:00
|
|
|
from flask import Flask
|
|
|
|
|
2024-10-22 11:01:32 +08:00
|
|
|
from configs import dify_config
|
|
|
|
|
2024-10-22 09:00:44 +08:00
|
|
|
|
|
|
|
def init_app(app: Flask):
|
|
|
|
log_handlers = None
|
2024-10-22 11:01:32 +08:00
|
|
|
log_file = dify_config.LOG_FILE
|
2024-10-22 09:00:44 +08:00
|
|
|
if log_file:
|
|
|
|
log_dir = os.path.dirname(log_file)
|
|
|
|
os.makedirs(log_dir, exist_ok=True)
|
|
|
|
log_handlers = [
|
|
|
|
RotatingFileHandler(
|
|
|
|
filename=log_file,
|
2024-10-23 17:24:36 +08:00
|
|
|
maxBytes=dify_config.LOG_FILE_MAX_SIZE * 1024 * 1024,
|
|
|
|
backupCount=dify_config.LOG_FILE_BACKUP_COUNT,
|
2024-10-22 09:00:44 +08:00
|
|
|
),
|
|
|
|
logging.StreamHandler(sys.stdout),
|
|
|
|
]
|
|
|
|
|
|
|
|
logging.basicConfig(
|
2024-10-22 11:01:32 +08:00
|
|
|
level=dify_config.LOG_LEVEL,
|
|
|
|
format=dify_config.LOG_FORMAT,
|
|
|
|
datefmt=dify_config.LOG_DATEFORMAT,
|
2024-10-22 09:00:44 +08:00
|
|
|
handlers=log_handlers,
|
|
|
|
force=True,
|
|
|
|
)
|
2024-10-30 11:18:23 +08:00
|
|
|
|
2024-10-22 11:01:32 +08:00
|
|
|
log_tz = dify_config.LOG_TZ
|
2024-10-22 09:00:44 +08:00
|
|
|
if log_tz:
|
|
|
|
for handler in logging.root.handlers:
|
2024-10-30 11:18:23 +08:00
|
|
|
handler.formatter.converter = lambda seconds: (
|
|
|
|
datetime.fromtimestamp(seconds, tz=pytz.UTC).astimezone(log_tz).timetuple()
|
|
|
|
)
|