Skip to content

hostname

HostnameField

Bases: DataProvider

Finds the hostname and adds it to the LogRecord as hostname

Example

>>> import logging
>>> HostnameField().install()
Source code in src/loggext/extradata/hostname.py
class HostnameField(DataProvider):
    r"""
    Finds the hostname and adds it to the LogRecord as `hostname`

    !!! example
        ```pycon
        >>> import logging
        >>> HostnameField().install()
        ```
    """

    @cache
    def _hostname(self) -> str:
        import socket
        return socket.gethostname()

    def add_data(self, record):
        record.hostname = self._hostname()