log-no-print (BTLOG002)¶
Added in 0.8.0 · Stable · Enabled by default (warning)
Family: logging · Standard: CES-46 · Languages: javascript, python, tsx, typescript
Directive¶
Importable library code logs through a logger. In Python, print(), sys.stdout.write() and
sys.stderr.write() are violations. In TypeScript and JavaScript (.ts, .tsx, .js), a call to
console.log, info, debug, warn, error, trace, dir or table, and
process.stdout.write() or process.stderr.write(), are violations. A warning by default.
Why¶
These calls write unstructured text to stdout or stderr, with no level, timestamp or context, and a library that prints pollutes the output of every program that imports it. Logging lets the application decide where output goes.
Exceptions¶
The entry points of a command line program and the tests write there on purpose. Another entry
point goes in the extend-ignores of the rule.
The rule skips:
__main__.py,cli.pyand every file under acli/directory.conftest.pyand every file undertests/.- A write inside an
if __name__ == "__main__":block.
TypeScript and JavaScript¶
The rule skips:
cli.*and every file under acli/orbin/directory.*.test.*,*.spec.*and every file under__tests__/ortests/.- A write inside an
if (import.meta.main)orif (require.main === module)block.
Do¶
Don't¶
Suppression¶
A comment on the line silences it. There are two forms: # botica: ignore[<slug>] names the rule by
its slug, and # noqa: <code> -- <reason> names it by its code, with the reason after --.