Skip to content

logging_helper.aibs

AibsLogServerHandlerSettings

Bases: ServiceSettings

Settings for the AIBS log server handler.

Handler that sends log records to a remote AIBS log server over HTTP.

settings_customise_sources classmethod

settings_customise_sources(
    settings_cls: Type[BaseSettings],
    init_settings: PydanticBaseSettingsSource,
    env_settings: PydanticBaseSettingsSource,
    dotenv_settings: PydanticBaseSettingsSource,
    file_secret_settings: PydanticBaseSettingsSource,
) -> Tuple[PydanticBaseSettingsSource, ...]

Customizes the settings sources to include the safe YAML settings source.

Parameters:

Name Type Description Default
settings_cls Type[BaseSettings]

The settings class

required
init_settings PydanticBaseSettingsSource

The initial settings source

required
env_settings PydanticBaseSettingsSource

The environment settings source

required
dotenv_settings PydanticBaseSettingsSource

The dotenv settings source

required
file_secret_settings PydanticBaseSettingsSource

The file secret settings source

required

Returns:

Type Description
Tuple[PydanticBaseSettingsSource, ...]

Tuple[PydanticBaseSettingsSource, ...]: A tuple of settings sources

Source code in src/clabe/services.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
@classmethod
def settings_customise_sources(
    cls,
    settings_cls: t.Type[ps.BaseSettings],
    init_settings: ps.PydanticBaseSettingsSource,
    env_settings: ps.PydanticBaseSettingsSource,
    dotenv_settings: ps.PydanticBaseSettingsSource,
    file_secret_settings: ps.PydanticBaseSettingsSource,
) -> t.Tuple[ps.PydanticBaseSettingsSource, ...]:
    """
    Customizes the settings sources to include the safe YAML settings source.

    Args:
        settings_cls: The settings class
        init_settings: The initial settings source
        env_settings: The environment settings source
        dotenv_settings: The dotenv settings source
        file_secret_settings: The file secret settings source

    Returns:
        Tuple[PydanticBaseSettingsSource, ...]: A tuple of settings sources
    """
    return (
        init_settings,
        *(
            _SafeYamlSettingsSource(settings_cls, yaml_file=p, yaml_config_section=cls.__yml_section__)
            for p in KNOWN_CONFIG_FILES
        ),
        env_settings,
        dotenv_settings,
        file_secret_settings,
    )

AibsLogServerHandler

AibsLogServerHandler(
    settings: AibsLogServerHandlerSettings, *args, **kwargs
)

Bases: SocketHandler

A custom logging handler that sends log records to the AIBS log server.

Extends the standard SocketHandler to include project-specific metadata in the log records before sending them to the log server.

Initializes the AIBS log server handler.

Parameters:

Name Type Description Default
settings AibsLogServerHandlerSettings

Configuration for the handler

required
*args

Additional arguments to pass to the SocketHandler

()
**kwargs

Additional keyword arguments to pass to the SocketHandler

{}
Source code in src/clabe/logging_helper/aibs.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def __init__(
    self,
    settings: AibsLogServerHandlerSettings,
    *args,
    **kwargs,
):
    """
    Initializes the AIBS log server handler.

    Args:
        settings: Configuration for the handler
        *args: Additional arguments to pass to the SocketHandler
        **kwargs: Additional keyword arguments to pass to the SocketHandler
    """
    super().__init__(settings.host, settings.port, *args, **kwargs)
    self.setLevel(settings.level)
    self._settings = settings

    self.formatter = logging.Formatter(
        fmt="%(asctime)s\n%(name)s\n%(levelname)s\n%(funcName)s (%(filename)s:%(lineno)d)\n%(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
    )

emit

emit(record: LogRecord) -> None

Emits a log record with project-specific metadata.

Parameters:

Name Type Description Default
record LogRecord

The log record to emit

required
Source code in src/clabe/logging_helper/aibs.py
84
85
86
87
88
89
90
91
92
93
94
95
96
def emit(self, record: logging.LogRecord) -> None:
    """
    Emits a log record with project-specific metadata.

    Args:
        record: The log record to emit
    """
    record.project = self._settings.project_name
    record.rig_id = self._settings.rig_id
    record.comp_id = self._settings.comp_id
    record.version = self._settings.version
    record.extra = None  # set extra to None because this sends a pickled record
    super().emit(record)

add_handler

add_handler(
    logger: TLogger, settings: AibsLogServerHandlerSettings
) -> TLogger

Adds an AIBS log server handler to the logger.

Parameters:

Name Type Description Default
logger TLogger

The logger to add the handler to.

required
settings AibsLogServerHandlerSettings

AibsLogServerHandlerSettings containing configuration options

required

Returns:

Type Description
TLogger

The logger with the added handler.

Examples:

import logging
from clabe.logging_helper.aibs import add_handler, AibsLogServerHandlerSettings

# Create a logger
logger = logging.getLogger('my_logger')
logger.setLevel(logging.INFO)

# Add the AIBS log server handler with default ERROR level
settings = AibsLogServerHandlerSettings(
    project_name='my_project',
    version='1.0.0',
    host='localhost',
    port=5000
)
logger = add_handler(logger, settings)

# Add handler with custom level
settings = AibsLogServerHandlerSettings(
    project_name='my_project',
    version='1.0.0',
    host='localhost',
    port=5000,
    level=logging.WARNING
)
logger = add_handler(logger, settings)
Source code in src/clabe/logging_helper/aibs.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def add_handler(
    logger: TLogger,
    settings: AibsLogServerHandlerSettings,
) -> TLogger:
    """
    Adds an AIBS log server handler to the logger.

    Args:
        logger: The logger to add the handler to.
        settings: AibsLogServerHandlerSettings containing configuration options

    Returns:
        The logger with the added handler.

    Examples:
        ```python
        import logging
        from clabe.logging_helper.aibs import add_handler, AibsLogServerHandlerSettings

        # Create a logger
        logger = logging.getLogger('my_logger')
        logger.setLevel(logging.INFO)

        # Add the AIBS log server handler with default ERROR level
        settings = AibsLogServerHandlerSettings(
            project_name='my_project',
            version='1.0.0',
            host='localhost',
            port=5000
        )
        logger = add_handler(logger, settings)

        # Add handler with custom level
        settings = AibsLogServerHandlerSettings(
            project_name='my_project',
            version='1.0.0',
            host='localhost',
            port=5000,
            level=logging.WARNING
        )
        logger = add_handler(logger, settings)
        ```
    """
    socket_handler = AibsLogServerHandler(settings=settings)
    logger.addHandler(socket_handler)
    return logger

attach_to_launcher

attach_to_launcher(
    launcher: Launcher,
    settings: AibsLogServerHandlerSettings,
) -> Launcher

Attaches an AIBS log server handler to a launcher instance.

Parameters:

Name Type Description Default
launcher Launcher

The launcher instance to attach the handler to.

required
settings AibsLogServerHandlerSettings

AibsLogServerHandlerSettings containing configuration options

required

Returns:

Type Description
Launcher

The launcher instance with the attached handler.

Examples:

import logging
from clabe.launcher import Launcher
from clabe.logging_helper.aibs import attach_to_launcher, AibsLogServerHandlerSettings

# Initialize the launcher
launcher = MyLauncher(...) # Replace with your custom launcher class

# Attach the AIBS log server handler with default ERROR level
settings = AibsLogServerHandlerSettings(
    project_name='my_launcher_project',
    version='1.0.0',
    host='localhost',
    port=5000
)
launcher = attach_to_launcher(launcher, settings)

# Attach handler with custom level
settings = AibsLogServerHandlerSettings(
    project_name='my_launcher_project',
    version='1.0.0',
    host='localhost',
    port=5000,
    level=logging.WARNING
)
launcher = attach_to_launcher(launcher, settings)
Source code in src/clabe/logging_helper/aibs.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
def attach_to_launcher(launcher: "Launcher", settings: AibsLogServerHandlerSettings) -> "Launcher":
    """
    Attaches an AIBS log server handler to a launcher instance.

    Args:
        launcher: The launcher instance to attach the handler to.
        settings: AibsLogServerHandlerSettings containing configuration options

    Returns:
        The launcher instance with the attached handler.

    Examples:
        ```python
        import logging
        from clabe.launcher import Launcher
        from clabe.logging_helper.aibs import attach_to_launcher, AibsLogServerHandlerSettings

        # Initialize the launcher
        launcher = MyLauncher(...) # Replace with your custom launcher class

        # Attach the AIBS log server handler with default ERROR level
        settings = AibsLogServerHandlerSettings(
            project_name='my_launcher_project',
            version='1.0.0',
            host='localhost',
            port=5000
        )
        launcher = attach_to_launcher(launcher, settings)

        # Attach handler with custom level
        settings = AibsLogServerHandlerSettings(
            project_name='my_launcher_project',
            version='1.0.0',
            host='localhost',
            port=5000,
            level=logging.WARNING
        )
        launcher = attach_to_launcher(launcher, settings)
        ```
    """

    add_handler(
        launcher.logger,
        settings=settings,
    )
    return launcher