From be7d2f53b3de4e6ac5241907112eeda82203ad31 Mon Sep 17 00:00:00 2001 From: crazywoola <100913391+crazywoola@users.noreply.github.com> Date: Wed, 30 Oct 2024 21:57:34 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"chore:=20improve=20validation=20and?= =?UTF-8?q?=20handler=20of=20logging=20timezone=20with=20Timezo=E2=80=A6"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit bab5c542197f935d35fe75eb5d2c3301fbbf58b4. --- api/configs/feature/__init__.py | 6 ++---- api/extensions/ext_logging.py | 16 ++++++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/api/configs/feature/__init__.py b/api/configs/feature/__init__.py index a8a4170f670969..0fa926038d3013 100644 --- a/api/configs/feature/__init__.py +++ b/api/configs/feature/__init__.py @@ -10,7 +10,6 @@ PositiveInt, computed_field, ) -from pydantic_extra_types.timezone_name import TimeZoneName from pydantic_settings import BaseSettings from configs.feature.hosted_service import HostedServiceConfig @@ -340,9 +339,8 @@ class LoggingConfig(BaseSettings): default=None, ) - LOG_TZ: Optional[TimeZoneName] = Field( - description="Timezone for log timestamps. Allowed timezone values can be referred to IANA Time Zone Database," - " e.g., 'America/New_York')", + LOG_TZ: Optional[str] = Field( + description="Timezone for log timestamps (e.g., 'America/New_York')", default=None, ) diff --git a/api/extensions/ext_logging.py b/api/extensions/ext_logging.py index 0fa832f4204e00..56b1d6bd28ba90 100644 --- a/api/extensions/ext_logging.py +++ b/api/extensions/ext_logging.py @@ -1,10 +1,8 @@ import logging import os import sys -from datetime import datetime from logging.handlers import RotatingFileHandler -import pytz from flask import Flask from configs import dify_config @@ -32,10 +30,16 @@ def init_app(app: Flask): handlers=log_handlers, force=True, ) - log_tz = dify_config.LOG_TZ if log_tz: + from datetime import datetime + + import pytz + + timezone = pytz.timezone(log_tz) + + def time_converter(seconds): + return datetime.utcfromtimestamp(seconds).astimezone(timezone).timetuple() + for handler in logging.root.handlers: - handler.formatter.converter = lambda seconds: ( - datetime.fromtimestamp(seconds, tz=pytz.UTC).astimezone(log_tz).timetuple() - ) + handler.formatter.converter = time_converter