Skip to content

OPENROUTER_DEBUG reconfigures the root logger, forcing DEBUG output for the entire process #590

Description

@rajarshidattapy

What happens

src/openrouter/utils/logger.py:25 calls logging.basicConfig(level=logging.DEBUG) when
OPENROUTER_DEBUG is set. get_default_logger() runs at client construction
(src/openrouter/sdk.py:183), so simply instantiating OpenRouter() mutates the host
application's root logger.

Measured before/after OpenRouter(...) with OPENROUTER_DEBUG=1:

root level BEFORE: WARNING  handlers: []
root level AFTER:  DEBUG    handlers: [<StreamHandler <stderr>>]

Every library in the process then emits DEBUG to stderr — httpx's request log showed up in
my capture — not just the SDK.

Why it's wrong

A library should configure its own logger, never the root one. basicConfig is for
applications; it's a global side effect that the caller didn't opt into and can't easily
undo. It also overrides any logging setup the app performed before the client was built.

No credential leakage here — httpx renders the auth header as 'authorization': '[secure]'
— so this is noise/ownership, not a security issue.

Repro

import logging, os
os.environ["OPENROUTER_DEBUG"] = "1"
print(logging.getLogger().level, logging.getLogger().handlers)  # 30 []
from openrouter import OpenRouter
OpenRouter(api_key="x")
print(logging.getLogger().level, logging.getLogger().handlers)  # 10 [StreamHandler]

Suggested fix

Scope the handler and level to the openrouter logger:

def get_default_logger() -> Logger:
    if os.getenv("OPENROUTER_DEBUG"):
        logger = logging.getLogger("openrouter")
        if not logger.handlers:
            logger.addHandler(logging.StreamHandler())
        logger.setLevel(logging.DEBUG)
        return logger
    return NoOpLogger()

Note: utils/logger.py is Speakeasy-generated (DO NOT EDIT header), so the change needs
to land in the generator template / gen config rather than as a direct edit here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions