Skip to content

extradata

>>> import logging
>>> logging.getLogger().addFilter(HostnameField() | ProgramNameField())

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()

ProgramNameField

Bases: DataProvider

Selects a suitable program-name and adds it to the LogRecord as programName

Example

>>> import logging
>>> ProgramNameField().install()
Source code in src/loggext/extradata/program_name.py
class ProgramNameField(DataProvider):
    r"""
    Selects a suitable program-name and adds it to the LogRecord as `programName`

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

    @cache
    def _program_name(self) -> str:
        import sys
        import os.path as p

        if sys.argv and sys.argv[0] != "-c":
            return p.basename(sys.argv[0])
        main = sys.modules.get('__main__')
        if main and hasattr(main, '__file__'):
            return p.basename(main.__file__)
        if sys.executable:
            return p.basename(sys.executable)
        return "python"

    def add_data(self, record):
        record.programName = self._program_name()

ShortNameField

Bases: DataProvider

Shortens the name of the current logger and adds it to the LogRecord as shortName. This can heavily decrease the size of your log-files in exchange for losing some clarity.

Example

project.custom.module -> p.c.module

Example

>>> import logging
>>> ShortNameField().install()
Source code in src/loggext/extradata/short_name.py
class ShortNameField(DataProvider):
    r"""
    Shortens the name of the current logger and adds it to the LogRecord as `shortName`.
    This can heavily decrease the size of your log-files in exchange for losing some clarity.

    !!! example
        `project.custom.module` -> `p.c.module`

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

    @cache
    def _short_name(self, name: str) -> str:
        *head, tail = name.split('.')
        return '.'.join(tuple(p[0] for p in head) + (tail,))

    def add_data(self, record):
        print(f"Adding ShortNameField for {record.name}")
        record.shortName = self._short_name(record.name)

UsernameField

Bases: DataProvider

Finds the username and adds it to the LogRecord as username

Example

>>> import logging
>>> UsernameField().install()

Info

On UNIX systems this uses the :mod:pwd module which means root will be reported when :man:sudo is used (as it should). If this fails (for example on Windows) then :func:getpass.getuser() is used as a fallback.

Source code in src/loggext/extradata/username.py
class UsernameField(DataProvider):
    r"""
    Finds the username and adds it to the LogRecord as `username`

    !!! example
        ```pycon
        >>> import logging
        >>> UsernameField().install()
        ```

    !!! info
        On UNIX systems this uses the :mod:`pwd` module which means ``root`` will
        be reported when :man:`sudo` is used (as it should). If this fails (for
        example on Windows) then :func:`getpass.getuser()` is used as a fallback.
    """

    @cache
    def _username(self) -> str:
        try:
            import pwd, os
            uid = os.getuid()
            return pwd.getpwuid(uid).pw_name
        except (ImportError, KeyError):
            import getpass
            return getpass.getuser()

    def add_data(self, record):
        record.username = self._username()