PyCharm logging output colours
Asked Answered
P

8

73

I'm using PyCharm to develop a GAE app in Mac OS X. Is there any way to display colours in the run console of PyCharm?

I've set a handler to output colours in ansi format. Then, I've added the handler:

LOG = logging.getLogger()
LOG.setLevel(logging.DEBUG)
for handler in LOG.handlers:
    LOG.removeHandler(handler)

LOG.addHandler(ColorHandler())

LOG.info('hello!')
LOG.warning('hello!')
LOG.debug('hello!')
LOG.error('hello!')

But the colour is the same.

PyCharm run console output

EDIT:

A response from JetBrains issue tracker: Change line 55 of the snippet from sys.stderr to sys.stdout. stderr stream is always colored with red color while stdout not.

Now colours are properly displayed.

Plexus answered 2/12, 2013 at 16:58 Comment(5)
What kind of PyCharm and what version is it?Infield
PyCharm Pro v3.0.2 for Mac OS XPlexus
The snippet referrenced went offline (as did xsnippet.org maybe?) - could you explain where you changed sys.stderr to sys.stdout?Kreindler
In the handler definition of your logging conf you can also simply add 'stream': sys.stdout to achieve thisVries
See this SO answerNoe
A
39

PyCharm doesn't support that feature natively, however you can download the Grep Console plugin and set the colors as you like.

Here's a screenshot: http://plugins.jetbrains.com/files/7125/screenshot_14104.png (link is dead)

I hope it helps somewhat :) although it doesn't provide fully colorized console, but it's a step towards it.

Arabia answered 19/2, 2014 at 12:46 Comment(2)
This plugin is great! Just what I was looking for. I really like the "Filter out" option as well.Plexus
Plugin IS awesome. Solved this for me.Katleen
N
86

In PyCharm 2024.1.3 you can do this by enabling:

Run | Edit Configurations... | Run/Debug Configurations | Modify options | Emulate terminal in output console

enter image description here enter image description here

Noe answered 6/8, 2017 at 17:32 Comment(7)
I was using the coloredlogs package in this screenshot.Noe
Note that the indicated checkbox only shows if you have "Python" selected in the list view. Other run types do not have this option.Rb
Not available in pycharm 2020Siddra
@Siddra I have pycharm 2020 and it is there, just not displayed exactly the same as in the screenshot.Suprematism
Can you please share where is it available now?Siddra
Never mind, figured it out. This feature is only available for local interpreter. I'm using a remote oneSiddra
I see this in Pycharm 2020.3.3. Interestingly, turning it on, running my script, then turning it off allowed me to debug with the console while still having colored output. Very useful.Mortensen
A
39

PyCharm doesn't support that feature natively, however you can download the Grep Console plugin and set the colors as you like.

Here's a screenshot: http://plugins.jetbrains.com/files/7125/screenshot_14104.png (link is dead)

I hope it helps somewhat :) although it doesn't provide fully colorized console, but it's a step towards it.

Arabia answered 19/2, 2014 at 12:46 Comment(2)
This plugin is great! Just what I was looking for. I really like the "Filter out" option as well.Plexus
Plugin IS awesome. Solved this for me.Katleen
E
23

Sept. 2019: PyCharm Community 2019.1

PyCharm colored all the logs including info/debug in red.

The upshot is: it is not a PyCharm problem, this is how the default logging is configured. Everything written to sys.stderr is colored red by PyCharm. When using StreamHandler() without arguments, the default stream is sys.stderr.

For getting non-colored logs back, specify logging.StreamHandler(stream=sys.stdout) in basic config like this:

logging.basicConfig(
    level=logging.DEBUG,
    format='[%(levelname)8s]:  %(message)s',
    handlers=[
        logging.FileHandler(f'{os.path.basename(__file__)}.log'),
        logging.StreamHandler(sys.stdout),
    ])

or be more verbose:

logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))

This fixed my red PyCharm logs.

Elianore answered 20/9, 2019 at 9:50 Comment(1)
I followed this approach. Info logs are printed in white , nicely. But now the ERROR logs are also printed in whiteShifrah
D
22

Late to the party, but anyone else with this issue, here's the solution that worked for me:

import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

This came from this answer

Danais answered 22/3, 2018 at 21:29 Comment(0)
P
4

What solved it for me (on PyCharm 2017.2) was going to Preferences -> Editor -> Color Scheme -> Console Colors and changing the color of Console -> Error output. Of course this also changes the error color but at least you don't see red all the time...

Prostate answered 25/12, 2017 at 14:19 Comment(0)
C
3

PyCharm 2019.1.1 (Windows 10, 1709) - runned snippet as is - works correctly.

Bug: setFormatter - does not work.

Fix: make change in line 67 and get rid on line 70-71 (unformatted handler adding).

self.stream.write(record.msg + "\n", color)

to

self.stream.write(self.format(record) + "\n", color)

Line 70-71 can be moved under manual file run construction for save test ability:

if __name__ == "__main__":
    logging.getLogger().setLevel(logging.DEBUG)
    logging.getLogger().addHandler(ColorHandler())

    logging.debug("Some debugging output")
    logging.info("Some info output")
    logging.error("Some error output")
    logging.warning("Some warning output")

Compared it with standard StreamHandler:

import logging
import logging_colored

log_format = logging.Formatter("[%(threadName)-15.15s] [%(levelname)-5.5s]  %(message)s")
logger = logging.getLogger('Main')
logger.setLevel(logging.DEBUG)

console = logging.StreamHandler()
console.setFormatter(log_format)
logger.addHandler(console)

console = logging_colored.ColorHandler()
console.setFormatter(log_format)
logger.addHandler(console)
...

Ceremony answered 17/4, 2019 at 9:58 Comment(0)
S
0

I discovered the following solution. Apparently Pycharm redirects sys.stdout. From the sys module documentation:

sys.__stdin__
sys.__stdout__
sys.__stderr__

These objects contain the original values of stdin, stderr and stdout at the start of the program. They are used during finalization, and could be useful to print to the actual standard stream no matter if the sys.std* object has been redirected.

It can also be used to restore the actual files to known working file objects in case they have been overwritten with a broken object. However, the preferred way to do this is to explicitly save the previous stream before replacing it, and restore the saved object.

Therefore, to solve this issue you can redirect output to sys.__stdout__. Example configuration from my log_config.yml:

console:
  class: logging.StreamHandler
  level: DEBUG
  stream: "ext://sys.__stdout__"
  formatter: colorFormatter
Swamy answered 23/1, 2018 at 15:56 Comment(0)
D
0

Grep Console Plugin can config any colour for any pattern text by your requirement, such as this simple one:

The Scrapy output plain text is Red in the original colour, add this rule, and all changes to the default grey colour.

enter image description here

Disinfection answered 5/5, 2023 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.