Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging rules not flagged if logger imported from another module #5694

Closed
petermattia opened this issue Jul 11, 2023 · 5 comments · Fixed by #5750
Closed

Logging rules not flagged if logger imported from another module #5694

petermattia opened this issue Jul 11, 2023 · 5 comments · Fixed by #5750
Assignees
Labels
bug Something isn't working

Comments

@petermattia
Copy link
Contributor

petermattia commented Jul 11, 2023

I'm observing that logging rules aren't flagged if a logger is imported from another module.

  • A minimal code snippet that reproduces the bug.

If I set up logging here, both of these violations are flagged:

# logging_setup.py
import logging

linter = "ruff"
logging.info(f"Hello, {linter}!") # G004 flagged

logger = logging.getLogger(__name__)

logger.info(f"Hello, {linter}!") # G004 flagged

However, if I import logger into another module, the below identical violation is not flagged:

# my_module.py
from logging_setup import logger

linter = "ruff"
logger.info(f"Hello, {linter}!") # G004 *not* flagged
  • The command you invoked (e.g., ruff /path/to/file.py --fix), ideally including the --isolated flag.

ruff .

  • The current Ruff settings (any relevant sections from your pyproject.toml).

"G" ruleset

  • The current Ruff version (ruff --version).
    0.0.277
@petermattia
Copy link
Contributor Author

Hi ruff team, friendly follow up on this one, please let me know if I'm missing something here. Thanks!

@charliermarsh
Copy link
Member

Will take a look.

@charliermarsh charliermarsh self-assigned this Jul 13, 2023
@charliermarsh
Copy link
Member

Looks like an oversight, we would flag logging_setup.logger.info(f"Hello, {linter}!") but not the import from variant.

@charliermarsh charliermarsh added the bug Something isn't working label Jul 13, 2023
@petermattia
Copy link
Contributor Author

Thanks @charliermarsh, that makes sense.

@charliermarsh
Copy link
Member

I think I misdiagnosed this, logging_setup.logger.info also isn't working in my initial testing, but regardless it'll be fixed in the next release.

charliermarsh added a commit that referenced this issue Jul 24, 2023
…s` (#5750)

## Summary

This PR adds a `logger-objects` setting that allows users to mark
specific symbols a `logging.Logger` objects. Currently, if a `logger` is
imported, we only flagged it as a `logging.Logger` if it comes exactly
from the `logging` module or is `flask.current_app.logger`.

This PR allows users to mark specific loggers, like
`logging_setup.logger`, to ensure that they're covered by the
`flake8-logging-format` rules and others.

For example, if you have a module `logging_setup.py` with the following
contents:

```python
import logging

logger = logging.getLogger(__name__)
```

Adding `"logging_setup.logger"` to `logger-objects` will ensure that
`logging_setup.logger` is treated as a `logging.Logger` object when
imported from other modules (e.g., `from logging_setup import logger`).

Closes #5694.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants