Logger

abb_rws_client_python_rw6.core.logger

Centralised logging configuration for abb_rws_client_python_rw6.

Author: Clement RACINET

The entire library uses the standard :mod:logging module with loggers named after their module (logging.getLogger(__name__)). All of them are children of the root logger "abb_rws_client_python_rw6", so a single call to :func:configure_logging is enough to control verbosity for the whole library without touching the application's own loggers.

Default behaviour (no call to :func:configure_logging): The "abb_rws_client_python_rw6" logger has no handler and no level set, which means it inherits from the root Python logger. In practice the library is silent unless the caller configures logging themselves.

Typical usage — application code::

from abb_rws_client_python_rw6import configure_logging
configure_logging(level="DEBUG")

Typical usage — examples / scripts::

import os
from abb_rws_client_python_rw6import configure_logging
configure_logging(level=os.getenv("RWS_LOG_LEVEL", "INFO"))

Typical usage — tests (silence the library)::

from abb_rws_client_python_rw6import configure_logging
configure_logging(level="WARNING")

configure_logging(level=logging.WARNING, fmt=_DEFAULT_FMT, date_fmt=_DEFAULT_DATE_FMT, handler=None, stream=sys.stderr)

Configure the abb_rws_client root logger.

Installs a :class:logging.StreamHandler (or a custom handler) on the "abb_rws_client_python_rw6" logger and sets its level. Calling this function more than once replaces the existing handlers.

All child loggers (abb_rws_client_python_rw6.core.client, abb_rws_client_python_rw6.highlevel.*, abb_rws_client_python_rw6.rws.*, …) inherit this configuration automatically — no per-module setup needed.

Parameters:
  • level (int | str, default: WARNING ) –

    Logging level for the library. Accepts an integer (e.g. logging.DEBUG) or a string (e.g. "DEBUG", "INFO", "WARNING"). Defaults to logging.WARNING (silent in production).

  • fmt (str, default: _DEFAULT_FMT ) –

    Log record format string (logging style). Defaults to "%(asctime)s [%(levelname)-8s] %(name)s — %(message)s".

  • date_fmt (str, default: _DEFAULT_DATE_FMT ) –

    Date/time format string passed to the formatter. Defaults to "%H:%M:%S".

  • handler (Handler | None, default: None ) –

    Custom :class:logging.Handler to attach. When None a :class:logging.StreamHandler writing to stream is created automatically.

  • stream (TextIO, default: stderr ) –

    Output stream used when handler is None. Defaults to :data:sys.stderr.

Returns:
  • Logger –

    The configured "abb_rws_client_python_rw6" :class:logging.Logger

  • Logger –

    instance (useful for immediate use or further customisation).

Raises:
  • ValueError –

    If level is a string that is not a recognised logging level name.

Example

Basic setup for a script::

from abb_rws_client_python_rw6import configure_logging
configure_logging(level="DEBUG")

Custom handler (e.g. file)::

import logging
from abb_rws_client_python_rw6import configure_logging

fh = logging.FileHandler("rws.log")
configure_logging(level="INFO", handler=fh)

Read level from environment::

import os
from abb_rws_client_python_rw6import configure_logging
configure_logging(level=os.getenv("RWS_LOG_LEVEL", "WARNING"))
Source code in abb_rws_client_python_rw6/core/logger.py
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 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
 93
 94
 95
 96
 97
 98
 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
def configure_logging(
    level: int | str = logging.WARNING,
    fmt: str = _DEFAULT_FMT,
    date_fmt: str = _DEFAULT_DATE_FMT,
    handler: logging.Handler | None = None,
    stream: TextIO = sys.stderr,
) -> logging.Logger:
    """Configure the ``abb_rws_client`` root logger.

    Installs a :class:`logging.StreamHandler` (or a custom *handler*) on
    the ``"abb_rws_client_python_rw6"`` logger and sets its level.  Calling this
    function more than once replaces the existing handlers.

    All child loggers (``abb_rws_client_python_rw6.core.client``,
    ``abb_rws_client_python_rw6.highlevel.*``, ``abb_rws_client_python_rw6.rws.*``, …)
    inherit this configuration automatically — no per-module setup needed.

    Args:
        level: Logging level for the library.  Accepts an integer
            (e.g. ``logging.DEBUG``) or a string (e.g. ``"DEBUG"``,
            ``"INFO"``, ``"WARNING"``).  Defaults to ``logging.WARNING``
            (silent in production).
        fmt: Log record format string (``logging`` style).
            Defaults to ``"%(asctime)s [%(levelname)-8s] %(name)s — %(message)s"``.
        date_fmt: Date/time format string passed to the formatter.
            Defaults to ``"%H:%M:%S"``.
        handler: Custom :class:`logging.Handler` to attach.  When
            ``None`` a :class:`logging.StreamHandler` writing to *stream*
            is created automatically.
        stream: Output stream used when *handler* is ``None``.
            Defaults to :data:`sys.stderr`.

    Returns:
        The configured ``"abb_rws_client_python_rw6"`` :class:`logging.Logger`
        instance (useful for immediate use or further customisation).

    Raises:
        ValueError: If *level* is a string that is not a recognised
            logging level name.

    Example:
        Basic setup for a script::

            from abb_rws_client_python_rw6import configure_logging
            configure_logging(level="DEBUG")

        Custom handler (e.g. file)::

            import logging
            from abb_rws_client_python_rw6import configure_logging

            fh = logging.FileHandler("rws.log")
            configure_logging(level="INFO", handler=fh)

        Read level from environment::

            import os
            from abb_rws_client_python_rw6import configure_logging
            configure_logging(level=os.getenv("RWS_LOG_LEVEL", "WARNING"))
    """
    # Resolve string level → int (raises ValueError on unknown name)
    if isinstance(level, str):
        numeric = logging.getLevelName(level.upper())
        if not isinstance(numeric, int):
            raise ValueError(
                f"Unknown logging level: {level!r}. "
                f"Expected one of DEBUG, INFO, WARNING, ERROR, CRITICAL."
            )
        level = numeric

    lib_logger = logging.getLogger(_ROOT_LOGGER_NAME)

    # Replace existing handlers to avoid duplicate output on repeated calls.
    lib_logger.handlers.clear()

    if handler is None:
        handler = logging.StreamHandler(stream)

    handler.setFormatter(logging.Formatter(fmt=fmt, datefmt=date_fmt))
    lib_logger.addHandler(handler)
    lib_logger.setLevel(level)

    # Do not propagate to the root Python logger — the library manages its
    # own output and should not interfere with the application's log config.
    lib_logger.propagate = False

    return lib_logger

get_logger(name)

Return a child logger of "abb_rws_client_python_rw6".

Convenience wrapper so that any module — including user code and example scripts — can obtain a properly namespaced logger without hard-coding the root name.

Parameters:
  • name (str) –

    Sub-name appended to "abb_rws_client_python_rw6.". Typically __name__ of the calling module, or a short descriptive label (e.g. "examples.ping").

Returns:
  • A( Logger ) –

    class:logging.Logger named

  • Logger –

    "abb_rws_client_python_rw6.<name>".

Example

Inside a library module::

from abb_rws_client_python_rw6.core.logger import get_logger
logger = get_logger(__name__)

Inside an example script::

from abb_rws_client_python_rw6import get_logger
logger = get_logger("examples.ping")
Source code in abb_rws_client_python_rw6/core/logger.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def get_logger(name: str) -> logging.Logger:
    """Return a child logger of ``"abb_rws_client_python_rw6"``.

    Convenience wrapper so that any module — including user code and
    example scripts — can obtain a properly namespaced logger without
    hard-coding the root name.

    Args:
        name: Sub-name appended to ``"abb_rws_client_python_rw6."``.
            Typically ``__name__`` of the calling module, or a short
            descriptive label (e.g. ``"examples.ping"``).

    Returns:
        A :class:`logging.Logger` named
        ``"abb_rws_client_python_rw6.<name>"``.

    Example:
        Inside a library module::

            from abb_rws_client_python_rw6.core.logger import get_logger
            logger = get_logger(__name__)

        Inside an example script::

            from abb_rws_client_python_rw6import get_logger
            logger = get_logger("examples.ping")
    """
    return logging.getLogger(f"{_ROOT_LOGGER_NAME}.{name}")