Make INFO-level logs not red in PyCharm
See original GitHub issueDescription & 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.
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:
Alternatives
No response
Additional context
No response
Issue Analytics
- State:
- Created 9 months ago
- Reactions:1
- Comments:8 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.