Skip to content

Commit

Permalink
move and rename config attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ychiucco committed Aug 31, 2024
1 parent 48fb370 commit 5abf1e6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
30 changes: 16 additions & 14 deletions fractal_server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,22 @@ def make_FRACTAL_RUNNER_WORKING_BASE_DIR_absolute(cls, v):
see details [here](../internals/logs/).
"""

FRACTAL_LOGGING_TIMEZONE: str = "UTC"
"""
Timezone of the Fractal logger.
List of possible values at `zoneinfo.available_timezones()`.
"""

@validator("FRACTAL_LOGGING_TIMEZONE")
def validate_timezone(cls, v):
if v not in zoneinfo.available_timezones():
raise FractalConfigurationError(
f"'{v}' is not a valid timezone. "
f"Available timezones:\n{zoneinfo.available_timezones()}"
)
return v

FRACTAL_LOCAL_CONFIG_FILE: Optional[Path]
"""
Path of JSON file with configuration for the local backend.
Expand Down Expand Up @@ -559,20 +575,6 @@ def check_tasks_python(cls, values) -> None:
Maximum value at which to update `pip` before performing task collection.
"""

FRACTAL_LOG_TIMEZONE: str = "UTC"
"""
The timezone to which we want the log timestamps to be converted.
"""

@validator("FRACTAL_LOG_TIMEZONE")
def validate_timezone(cls, v):
if v not in zoneinfo.available_timezones():
raise FractalConfigurationError(
f"'{v}' is not a valid timezone. "
f"Available timezones:\n{zoneinfo.available_timezones()}"
)
return v

###########################################################################
# BUSINESS LOGIC
###########################################################################
Expand Down
8 changes: 5 additions & 3 deletions fractal_server/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
def _converter(timestamp: float) -> time.struct_time:
return (
datetime.datetime.fromtimestamp(timestamp)
.astimezone(ZoneInfo(settings.FRACTAL_LOG_TIMEZONE))
.astimezone(ZoneInfo(settings.FRACTAL_LOGGING_TIMEZONE))
.timetuple()
)

Expand All @@ -41,12 +41,14 @@ def converter(self, timestamp: float) -> time.struct_time:

def _utc_offset(timezone: str) -> str:
"""
Input strings must come from `zoneinfo.available_timezones()`.
Input strings must come from `zoneinfo.available_timezones()`,
otherwise raises a `ZoneInfoNotFoundError`.
Examples input -> output:
* "Canada/Newfoundland" -> "UTC-2:30"
* "Europe/Rome" -> "UTC+2"
* "UTC" -> "UTC+0"
"""
timedelta = (
datetime.datetime.now(datetime.timezone.utc)
Expand All @@ -71,7 +73,7 @@ def _utc_offset(timezone: str) -> str:
"%(asctime)s::%(msecs)03d - %(name)s - %(levelname)s - %(message)s"
)
DATE_FORMAT = (
f"%Y-%m-%d %H:%M:%S({_utc_offset(settings.FRACTAL_LOG_TIMEZONE)})"
f"%Y-%m-%d %H:%M:%S({_utc_offset(settings.FRACTAL_LOGGING_TIMEZONE)})"
)
LOG_FORMATTER = FractalLoggingFormatter(LOG_FORMAT, DATE_FORMAT)

Expand Down
10 changes: 5 additions & 5 deletions tests/no_version/test_unit_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,16 +401,16 @@ def test_python_interpreters():
assert f"Non-absolute value {key}=" in str(e.value)


def test_FRACTAL_LOG_TIMEZONE():
def test_FRACTAL_LOGGING_TIMEZONE():

common_attributes = dict(
JWT_SECRET_KEY="something",
SQLITE_PATH="/something",
FRACTAL_RUNNER_WORKING_BASE_DIR="/something",
FRACTAL_TASKS_DIR="/something",
)

Settings(**common_attributes)
Settings(**common_attributes, FRACTAL_LOG_TIMEZONE="Europe/Rome")
Settings(**common_attributes, FRACTAL_LOG_TIMEZONE="America/New_York")
Settings(**common_attributes, FRACTAL_LOGGING_TIMEZONE="Asia/Tokyo")
Settings(**common_attributes, FRACTAL_LOGGING_TIMEZONE="America/New_York")
with pytest.raises(FractalConfigurationError):
Settings(**common_attributes, FRACTAL_LOG_TIMEZONE="Trieste")
Settings(**common_attributes, FRACTAL_LOGGING_TIMEZONE="Trieste")

0 comments on commit 5abf1e6

Please sign in to comment.