question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make INFO-level logs not red in PyCharm

See original GitHub issue

Description & Motivation

In PyCharm, informational messages from Lightning are printed in red, which doesn’t look great and obscures real errors.

This is because the logger is printing to stderr and PyCharm by default makes that red. image

Pitch

You can fix this by tweaking your root logger to print to stdout for INFO and below, and stderr for everything else. All the other loggers will inherit from this

In pytorch_lightning/__init__.py:

_logger = logging.getLogger(__name__)
_logger.setLevel(logging.INFO)

# log lower levels to stdout
stdout_handler = logging.StreamHandler(stream=sys.stdout)
stdout_handler.addFilter(lambda rec: rec.levelno <= logging.INFO)
_logger.addHandler(stdout_handler)

# log higher levels to stderr (red)
stderr_handler = logging.StreamHandler(stream=sys.stderr)
stderr_handler.addFilter(lambda rec: rec.levelno > logging.INFO)
_logger.addHandler(stderr_handler)

Which then prints like so: image

Alternatives

No response

Additional context

No response

cc @borda @justusschock

Issue Analytics

  • State:open
  • Created 9 months ago
  • Reactions:1
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
carmoccacommented, Dec 20, 2022

Logging to stderr is usually on purpose for libraries/frameworks. The main benefit is that it uses a different stream than the regular prints/logs that are generated from the user code.

This decision is mainly a “stylistic standard” but changing it would be an unavoidable breaking change.

For example, I’ve written PL scripts where I print results relevant to my experiment (to stdout) and pipe the command into a text file (python script.py > out.txt). Doing this change would break such workflow and would make it difficult to separate user logs from library logs.

The same can be said for colored logs. Many terminals do not support rendering so adding them can add intentionally annoying lines in between the valuable messages for the color output. Maybe this is fine as long as we adhere to the requirements in https://no-color.org. Still, a too-colorful choice can be distracting so I would ask for examples of how some experiment logs would show first.

0reactions
carmoccacommented, Dec 21, 2022

It’s probably more involved than that, because in the wild people use really different terminals. I suggest looking at the rich.Console implementation as a reference. If you cmd+F for “color” in the file, you’ll see several interesting details.

If you are feeling brave, feel free to open a PR for color support specifically and we can discuss details there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting log options | PyCharm Documentation - JetBrains
Use Logs tab in the Run/Debug Configuration dialogs to configure the way log files, generated by an application or server, are displayed in...
Read more >
PyCharm logging output colours - python - Stack Overflow
stdout. stderr stream is always colored with red color while stdout not. Now colours are properly displayed.
Read more >
Python Logging: A Stroll Through the Source Code
Because the LogRecord and its message only carry level INFO , the record gets written to Handler 1 (green arrow), but not to...
Read more >
Logging in Python - MachineLearningMastery.com
Emit a log message of level INFO, by default this is not print to the screen. logger.info("this is info level"). # Create `parent`...
Read more >
Debugging a test — Tavern 1.24.1 documentation
If this contains sensitive data, either log at the INFO level, or make sure that any data logged is obfuscated, or the logs...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found