Skip to content

Scope logToFile and logToConsole handlers to ib_async logger#218

Open
mahimn01 wants to merge 1 commit into
ib-api-reloaded:nextfrom
mahimn01:scope-log-handlers
Open

Scope logToFile and logToConsole handlers to ib_async logger#218
mahimn01 wants to merge 1 commit into
ib-api-reloaded:nextfrom
mahimn01:scope-log-handlers

Conversation

@mahimn01
Copy link
Copy Markdown

@mahimn01 mahimn01 commented May 19, 2026

Fixes #24.

Both logToFile and logToConsole were attaching their handlers to the root logger via logging.getLogger(), so calling them captured every log record in the process, not just records from ib_async.

Attaches handlers to logging.getLogger("ib_async") instead, and gives both helpers a duplicate-detection check over the ib_async namespace's own handlers plus root's (since records propagate up): logToConsole skips a redundant stderr handler and logToFile skips a redundant handler for the same file path. This makes repeat calls (e.g. a notebook re-run or a re-init) idempotent rather than stacking handlers and double-writing.

All internal modules already log under the ib_async.* namespace (decoder, wrapper, ib, client, ibcontroller, flexreport) and none disable propagation, so user setups that catch records on the root logger keep working.

The v0.9.67 fix referenced in docs/changelog.rst:437 (ib_insync #361) addressed the level-pollution side of this but left the handler attachment on root.

Includes tests/test_log_scoping.py covering: logToFile scope + third-party isolation, logToConsole scope, duplicate-handler detection on repeat call for both helpers, and cross-namespace duplicate detection when root already has a stderr handler. An autouse fixture restores logger levels so the tests do not leak their level mutations into the rest of the session.

Both helpers were attaching their handlers to the root logger via
`logging.getLogger()`, so calling them captured every log record in the
process, not just records from ib_async.

Attach handlers to `logging.getLogger("ib_async")` instead, and give
both helpers a duplicate-detection check over the ib_async namespace's
own handlers plus root's (since records propagate up): `logToConsole`
skips a redundant stderr handler and `logToFile` skips a redundant
handler for the same file path.

The v0.9.67 fix referenced in `docs/changelog.rst:437` addressed the
level-pollution side of this but left the handler attachment on root.

Fixes ib-api-reloaded#24
@mahimn01 mahimn01 force-pushed the scope-log-handlers branch from 8aa05fc to bc93fce Compare May 31, 2026 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant