Module: log_settings

Expand source code
# Copyright (C) 2023-present The Project Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime as dt
from dataclasses import dataclass
from cl.runtime.primitive.datetime_util import DatetimeUtil
from cl.runtime.settings.settings import Settings


@dataclass(slots=True, kw_only=True)
class LogSettings(Settings):
    """REST API settings."""

    filename_format: str = "prefix-timestamp"
    """
    Log filename format, the choices are:
    - prefix: Prefix only
    - prefix-timestamp: Prefix followed by UTC timestamp to millisecond precision in dash-delimited format
    """

    filename_prefix: str = "default"
    """Log filename prefix."""

    filename_timestamp: dt.datetime = DatetimeUtil.now()
    """Timestamp to use for log file, set to the time of program launch if not specified in settings."""

    level: str = "info"
    """Log level using logging module conventions (lower, upper or mixed case can be used)."""

    def init(self) -> None:
        """Same as __init__ but can be used when field values are set both during and after construction."""

        # Convert logging level to uppercase and validate its values
        self.level = self.level.upper()
        valid_levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
        if self.level not in valid_levels:
            raise RuntimeError(
                f"Invalid log level: {self.level}, permitted values are: {', '.join(valid_levels)}. "
                f"Lower, upper or mixed case can be used."
            )

    @classmethod
    def get_prefix(cls) -> str:
        return "runtime_log"

Classes

class LogSettings (*, filename_format: str = 'prefix-timestamp', filename_prefix: str = 'default', filename_timestamp: datetime.datetime = datetime.datetime(2024, 11, 6, 22, 46, 56, 424000, tzinfo=datetime.timezone.utc), level: str = 'info')

REST API settings.

Expand source code
@dataclass(slots=True, kw_only=True)
class LogSettings(Settings):
    """REST API settings."""

    filename_format: str = "prefix-timestamp"
    """
    Log filename format, the choices are:
    - prefix: Prefix only
    - prefix-timestamp: Prefix followed by UTC timestamp to millisecond precision in dash-delimited format
    """

    filename_prefix: str = "default"
    """Log filename prefix."""

    filename_timestamp: dt.datetime = DatetimeUtil.now()
    """Timestamp to use for log file, set to the time of program launch if not specified in settings."""

    level: str = "info"
    """Log level using logging module conventions (lower, upper or mixed case can be used)."""

    def init(self) -> None:
        """Same as __init__ but can be used when field values are set both during and after construction."""

        # Convert logging level to uppercase and validate its values
        self.level = self.level.upper()
        valid_levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
        if self.level not in valid_levels:
            raise RuntimeError(
                f"Invalid log level: {self.level}, permitted values are: {', '.join(valid_levels)}. "
                f"Lower, upper or mixed case can be used."
            )

    @classmethod
    def get_prefix(cls) -> str:
        return "runtime_log"

Ancestors

Class variables

var is_inside_test

Inherited from: Settings.is_inside_test

True if we are inside a test.

var process_timestamp

Inherited from: Settings.process_timestamp

Unique UUIDv7-based timestamp set during the Python process launch.

Static methods

def get_prefix() -> str

Inherited from: Settings.get_prefix

Dynaconf fields will be filtered by ‘prefix_’ before being passed to the settings class constructor …

def get_project_root() -> str

Inherited from: Settings.get_project_root

Returns absolute path of the directory containing .env file, and if not present the directory containing the first Dynaconf settings file found. Error …

def instance() -> Self

Inherited from: Settings.instance

Return singleton instance.

def normalize_path(field_name: str, field_value: str | None) -> str

Inherited from: Settings.normalize_path

Convert to absolute path if path relative to the location of .env or Dynaconf file is specified.

def normalize_paths(field_name: str, field_value: Iterable[str] | str | None) -> List[str]

Inherited from: Settings.normalize_paths

Convert to absolute path if path relative to the location of .env or Dynaconf file is specified and convert to list if single value is specified.

Fields

var filename_format -> str

Log filename format, the choices are: – prefix: Prefix only – prefix-timestamp: Prefix followed by UTC timestamp to millisecond precision in dash-delimited format

var filename_prefix -> str

Log filename prefix.

var filename_timestamp -> datetime.datetime

Timestamp to use for log file, set to the time of program launch if not specified in settings.

var level -> str

Log level using logging module conventions (lower, upper or mixed case can be used).

Methods

def init(self) -> None

Same as init but can be used when field values are set both during and after construction.