Skip to content

Use module-level named loggers instead of the root logger #48

Closed

Description

@tipatterson-dev

Sub-issue of #46.

Problem

24 log calls across 6 modules go to the root logger via the module-level functions — logging.warning(...), logging.error(...) — rather than a named logger:

  • resources/base.py (14 calls), resources/system.py (3), resources/datastream.py (2), resources/controlstream.py (2), oshconnectapi.py (2), events/handler.py (1)

Only csapi4py/mqtt.py:12 and csapi4py/nats.py:55 do the right thing with logger = logging.getLogger(__name__).

Two consequences for anything embedding OSHConnect:

  1. No selective control. An app cannot raise OSHConnect to DEBUG, or silence its schema-fetch warnings, without reconfiguring the root logger and affecting every other library it uses.
  2. Implicit basicConfig. Calling logging.warning(...) on the root logger auto-installs a handler on stderr if none exists — a library writing to a consumer's stderr uninvited.

Reproduce

import logging
logging.getLogger("oshconnect").setLevel(logging.ERROR)   # has no effect

system.discover_datastreams()   # schema-fetch warnings still print

(Note: the SchemaFetchWarning docstring in resources/base.py:56-58 names
Node.discover_systems as a source of schema-fetch failures. It isn't — that
method never fetches a schema. Worth correcting while in here.)

Possible fix

Mechanical, one line per module plus a call-site rename:

logger = logging.getLogger(__name__)
...
logger.warning(...)   # was logging.warning(...)

Add a NullHandler on the package logger in oshconnect/__init__.py so the library never configures logging for its consumer:

logging.getLogger(__name__).addHandler(logging.NullHandler())

Then document the oshconnect.* logger namespace in the docs so consumers know what to configure.

Note resources/base.py and resources/system.py pair several of these log calls with warnings.warn(..., SchemaFetchWarning) — that dual-channel behavior is intentional (see the SchemaFetchWarning docstring) and should be preserved, not collapsed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions