Skip to content

Inconsistent logging detection #7502

@konstin

Description

@konstin

There are three ways you can call the logging module:

import logging
logger = logging.getLoger(__name__)
logger.info("msg")
import logging
logging.info("msg")
from logging import info
info("msg")

The logging rules are enforced inconsistently between those three call kinds:

import logging
from logging import warn, info, error

logger = logging.getLogger(__name__)

logging.info(f"{1}")  # G004
logger.info(f"{1}")  # G004
info(f"{1}")

logging.info("", 2)  # PLE1205
logger.info("", 2)  # PLE1205
info("", 2)

logging.warn("a")  # PGH002, G010
logger.warn("a")  # G010
warn("a")  # PGH002

try:
    print(1 / 0)
except Exception:
    logging.error("a")  # TRY400
    logger.error("a")  # TRY400
    error("a")

Checking for a common logger name is generally consistent, but PGH002 mismatches expectations here.

It might also make sense to look for usages of logging.getLogger for a more semantic coverage, even though the vast majority of usages in the wild seem to match our pattern (see also https://grep.app/search?q=logging.getLogger&filter[lang][0]=Python, https://github.com/hummingbot/hummingbot/blob/b3ba248e7b9bab26c010fcb69a53040146084adb/hummingbot/client/config/conf_migration.py#L45, https://github.com/qutebrowser/qutebrowser/blob/4190a470c56ed90d97af89711a30c4603a347858/qutebrowser/utils/log.py#L104)

The main questions i'm trying to answer are, a) should we change the scope of the existing rule (or not) and b) which of the three cases should new logging rules capture?

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions