Colored terminal output for Python's logging module

Overview

coloredlogs: Colored terminal output for Python's logging module

https://travis-ci.org/xolox/python-coloredlogs.svg?branch=master https://coveralls.io/repos/github/xolox/python-coloredlogs/badge.svg?branch=master

The coloredlogs package enables colored terminal output for Python's logging module. The ColoredFormatter class inherits from logging.Formatter and uses ANSI escape sequences to render your logging messages in color. It uses only standard colors so it should work on any UNIX terminal. It's currently tested on Python 2.7, 3.5+ and PyPy (2 and 3). On Windows coloredlogs automatically tries to enable native ANSI support (on up-to-date Windows 10 installations) and falls back on using colorama (if installed). Here is a screen shot of the demo that is printed when the command coloredlogs --demo is executed:

https://coloredlogs.readthedocs.io/en/latest/_images/defaults.png

Note that the screenshot above includes custom logging levels defined by my verboselogs package: if you install both coloredlogs and verboselogs it will Just Work (verboselogs is of course not required to use coloredlogs).

Installation

The coloredlogs package is available on PyPI which means installation should be as simple as:

$ pip install coloredlogs

There's actually a multitude of ways to install Python packages (e.g. the per user site-packages directory, virtual environments or just installing system wide) and I have no intention of getting into that discussion here, so if this intimidates you then read up on your options before returning to these instructions 😉 .

Optional dependencies

Native ANSI support on Windows requires an up-to-date Windows 10 installation. If this is not working for you then consider installing the colorama package:

$ pip install colorama

Once colorama is installed it will be used automatically.

Usage

Here's an example of how easy it is to get started:

import coloredlogs, logging

# Create a logger object.
logger = logging.getLogger(__name__)

# By default the install() function installs a handler on the root logger,
# this means that log messages from your code and log messages from the
# libraries that you use will all show up on the terminal.
coloredlogs.install(level='DEBUG')

# If you don't want to see log messages from libraries, you can pass a
# specific logger object to the install() function. In this case only log
# messages originating from that logger will show up on the terminal.
coloredlogs.install(level='DEBUG', logger=logger)

# Some examples.
logger.debug("this is a debugging message")
logger.info("this is an informational message")
logger.warning("this is a warning message")
logger.error("this is an error message")
logger.critical("this is a critical message")

Format of log messages

The ColoredFormatter class supports user defined log formats so you can use any log format you like. The default log format is as follows:

%(asctime)s %(hostname)s %(name)s[%(process)d] %(levelname)s %(message)s

This log format results in the following output:

2015-10-23 03:32:22 peter-macbook coloredlogs.demo[30462] DEBUG message with level 'debug'
2015-10-23 03:32:23 peter-macbook coloredlogs.demo[30462] VERBOSE message with level 'verbose'
2015-10-23 03:32:24 peter-macbook coloredlogs.demo[30462] INFO message with level 'info'
...

You can customize the log format and styling using environment variables as well as programmatically, please refer to the online documentation for details.

Enabling millisecond precision

If you're switching from logging.basicConfig() to coloredlogs.install() you may notice that timestamps no longer include milliseconds. This is because coloredlogs doesn't output milliseconds in timestamps unless you explicitly tell it to. There are three ways to do that:

  1. The easy way is to pass the milliseconds argument to coloredlogs.install():

    coloredlogs.install(milliseconds=True)
    

    This became supported in release 7.1 (due to #16).

  2. Alternatively you can change the log format to include 'msecs':

    %(asctime)s,%(msecs)03d %(hostname)s %(name)s[%(process)d] %(levelname)s %(message)s
    

    Here's what the call to coloredlogs.install() would then look like:

    coloredlogs.install(fmt='%(asctime)s,%(msecs)03d %(hostname)s %(name)s[%(process)d] %(levelname)s %(message)s')
    

    Customizing the log format also enables you to change the delimiter that separates seconds from milliseconds (the comma above). This became possible in release 3.0 which added support for user defined log formats.

  3. If the use of %(msecs)d isn't flexible enough you can instead add %f to the date/time format, it will be replaced by the value of %(msecs)03d. Support for the %f directive was added to release 9.3 (due to #45).

Custom logging fields

The following custom log format fields are supported:

  • %(hostname)s provides the hostname of the local system.
  • %(programname)s provides the name of the currently running program.
  • %(username)s provides the username of the currently logged in user.

When coloredlogs.install() detects that any of these fields are used in the format string the applicable logging.Filter subclasses are automatically registered to populate the relevant log record fields.

Changing text styles and colors

The online documentation contains an example of customizing the text styles and colors.

Colored output from cron

When coloredlogs is used in a cron job, the output that's e-mailed to you by cron won't contain any ANSI escape sequences because coloredlogs realizes that it's not attached to an interactive terminal. If you'd like to have colors e-mailed to you by cron there are two ways to make it happen:

Modifying your crontab

Here's an example of a minimal crontab:

MAILTO="your-email-address@here"
CONTENT_TYPE="text/html"
* * * * * root coloredlogs --to-html your-command

The coloredlogs program is installed when you install the coloredlogs Python package. When you execute coloredlogs --to-html your-command it runs your-command under the external program script (you need to have this installed). This makes your-command think that it's attached to an interactive terminal which means it will output ANSI escape sequences which will then be converted to HTML by the coloredlogs program. Yes, this is a bit convoluted, but it works great :-)

Modifying your Python code

The ColoredCronMailer class provides a context manager that automatically enables HTML output when the $CONTENT_TYPE variable has been correctly set in the crontab.

This requires my capturer package which you can install using pip install 'coloredlogs[cron]'. The [cron] extra will pull in capturer 2.4 or newer which is required to capture the output while silencing it - otherwise you'd get duplicate output in the emails sent by cron.

The context manager can also be used to retroactively silence output that has already been produced, this can be useful to avoid spammy cron jobs that have nothing useful to do but still email their output to the system administrator every few minutes :-).

Contact

The latest version of coloredlogs is available on PyPI and GitHub. The online documentation is available on Read The Docs and includes a changelog. For bug reports please create an issue on GitHub. If you have questions, suggestions, etc. feel free to send me an e-mail at [email protected].

License

This software is licensed under the MIT license.

© 2020 Peter Odding.

Comments
  • isatty should force usage of the colored formatter

    isatty should force usage of the colored formatter

    some terminals (like travis and jupyter) can not have colored output, because the terminal is checked again with humanfriendly.terminal_supports_colors(stream)

    this leads to no coloured output on travis and jupyter.

    the 'isatty' keyword is triadic :

    isatty = None  # autodetect if we can use color
    isatty = False  # dont  use color
    

    at the Moment You treat isatty = True the same way like it would be None - but isatty=True should force the use of the ColoredFormatter !

    If You dont like it for a reason, You also can put a new keyword like force_color but from my point of view it should not be neccessary.

    However, with that patch the colored output is possible now for Jupyter (on stdout) and Travis (on stderr) !

    please consider it, until then I need to host my own fork on pypi to get the wheels ....

    yours sincerely

    Robert

    opened by bitranox 7
  • Drop support for EOL Python 2.6

    Drop support for EOL Python 2.6

    Python 2.6 is breaking the build. It is EOL since 2013-10-29 and no longer receiving security updates (or any updates) from the core Python team.

    It's also little used. Here's installs from PyPI:

    image

    Source: https://pypistats.org/packages/coloredlogs

    Also add python_requires to help pip, and remove redundant sudo: required: https://blog.travis-ci.com/2018-11-19-required-linux-infrastructure-migration

    opened by hugovk 7
  • Option isatty=True not always working

    Option isatty=True not always working

    Detected issue

    When passing the parameter isatty=True to the method coloredlogs.install() the logger does not always generate a colored output.

    Conditions

    I use this option because I run on windows and use a git bash terminal. Since git bash is using pipes it's not detecting the terminal correctly with sys.stdout.isatty().

    Expected behavior

    I would expect with the isatty=True to the method coloredlogs.install() that the colored output is forced independent of sys.stdout.isatty().

    Version

    This has been discovered when running on version 14.0.

    More information on issue

    Debugging the module has shown that the method terminal_supports_colors(stream) returns false and the use_colors gets deactivated. More debugging has shown that in there the sys.stdout.isatty() is called.
    Looking at those lines there is no difference from 'isatty'=None and 'isatty'=True.

    Possible fix

    For me changing line 433 to if use_colors is None: solved the issue. But I cannot tell if this is a valid fix, since I do not know other use cases.

    opened by pascal-hari 5
  • Problem when use coloredlogs with other libraries.

    Problem when use coloredlogs with other libraries.

    When I use selenium+phantomjs and coloredlogs, I found all log will output to the console, include selenuim's log. I just want show my log. How can I close the other logs?

    This is my code:

    FIELD_STYLES = dict(
        asctime=dict(color='green'),
        hostname=dict(color='magenta'),
        levelname=dict(color='green', bold=coloredlogs.CAN_USE_BOLD_FONT),
        filename=dict(color='magenta'),
        name=dict(color='blue'),
        threadName=dict(color='green')
    )
    
    LEVEL_STYLES = dict(
        debug=dict(color='green'),
        info=dict(color='cyan'),
        verbose=dict(color='blue'),
        warning=dict(color='yellow'),
        error=dict(color='red'),
        critical=dict(color='red', bold=coloredlogs.CAN_USE_BOLD_FONT)
    )
    logger = logging.getLogger("ProjectName")
    
    cf = ConfigParser.ConfigParser()
    cf.read("config.ini")
    if cf.has_option("ProjectName", "debug") and cf.get("ProjectName", "debug") == "On":
        level = "DEBUG"
    else:
        level = "INFO"
    
    coloredlogs.install(
        level=level,
        fmt="[%(levelname)s] [(%(threadName)s)] [%(asctime)s] [%(filename)s:%(lineno)d] %(message)s",
        datefmt="%H:%M:%S",
        level_styles=LEVEL_STYLES,
        field_styles=FIELD_STYLES,
    )
    

    And I write log like this:

    logger.debug("debug information")
    

    And the output like this:

    [INFO] [(MainThread)] [00:06:46] [WebSpider.py:74] initial web spider phantomjs process pool...
    [DEBUG] [(MainThread)] [00:06:51] [remote_connection.py:415] POST http://127.0.0.1:11225/wd/hub/session {"requiredCapabilities": {}, "desiredCapabilities": {"javascriptEnabled": true, "phantomjs.page.settings.resourceTimeout": 10, "phantomjs.page.settings.loadImages": false, "platform": "ANY", "browserName": "phantomjs", "version": ""}}
    [DEBUG] [(MainThread)] [00:06:51] [remote_connection.py:510] Finished Request
    [INFO] [(MainThread)] [00:06:51] [WebSpider.py:85] 25.00% finished.
    

    The "remote_connection.py:510" isn't my log, maybe it create by selenium or phantomjs.

    After read some docs, I found the coloredlogs.install() function has a param called logger, if it None, it will set to the root logger, so I pass my logger to it, like this below:

    coloredlogs.install(
        level=level,
        logger=logger,
        fmt="[%(levelname)s] [(%(threadName)s)] [%(asctime)s] [%(filename)s:%(lineno)d] %(message)s",
        datefmt="%H:%M:%S",
        level_styles=LEVEL_STYLES,
        field_styles=FIELD_STYLES,
    )
    

    But this time, there is no log output to console, so I pass a stream to this function, "stream=sys.stdout", also no output.

    Did I do something wrong?

    opened by lightless233 5
  • Termination and spaces

    Termination and spaces

    In the present version, colouring of the different fields (name, loglevel, hostname, etc.) seems to be dependent on placement of white spaces. I.e. if no white spaces are placed between fields, consecutive fields will have the colour of the first in string. Is that intended behaviour? It can be seen using the following code:

    import coloredlogs, logging
    from logging import debug, info, error, warning as warn
    coloredlogs.install(level='DEBUG', fmt=formatstring, datefmt='%d.%m.%y %H:%M' )
    debug("debug")
    info("info")
    warn("warn")
    error("error")
    

    where two different format strings are used (note the spaces around levelanme, name and process string formatters):

    formatstring='[ %(levelname)-8s ] (%(asctime)s %(name)s @ %(hostname)s [%(process)d]'   # with spaces
    formatstring='[%(levelname)-8s] (%(asctime)s %(name)s@%(hostname)s[%(process)d]'       # without spaces
    

    The result is something like this (note the color of the levelname and the blurred hostname field):

    with spaces without spaces

    opened by con-f-use 5
  • AttributeError: 'module' object has no attribute 'install'

    AttributeError: 'module' object has no attribute 'install'

    The following example from the documentation:

    import coloredlogs, logging
    coloredlogs.install()
    logger = logging.getLogger()
    logger.info("this is an informational message")
    

    Gives this error in python 2.7.6 on Ubuntu 14.04:

    Traceback (most recent call last):
      File "./coloredlogs.py", line 21, in <module>
        import coloredlogs, logging
      File "/home/confus/misc/test/coloredlogs.py", line 22, in <module>
        coloredlogs.install()
    AttributeError: 'module' object has no attribute 'install'
    
    opened by con-f-use 4
  • Filter instead of logger

    Filter instead of logger

    IIRC, a LogFilter can actually modify the LogRecord objects. Perhaps instead of (inflexibly) hard-coding the log format, you could just define a custom filter that wraps parts of the LogRecord with the proper unicode color codes?

    opened by hamiltont 4
  • AttributeError: module 'coloredlogs' has no attribute 'CAN_USE_BOLD_FONT'

    AttributeError: module 'coloredlogs' has no attribute 'CAN_USE_BOLD_FONT'

    Hello, thank you for this beautiful package :)

    In version 14.0 we are facing this error:

    AttributeError: module 'coloredlogs' has no attribute 'CAN_USE_BOLD_FONT'
    

    Was this removed or something?

    opened by AbdulRahmanAlHamali 3
  • Drop support for EOL Python 3.4

    Drop support for EOL Python 3.4

    Includes #79, the last commit https://github.com/xolox/python-coloredlogs/commit/036faef9fde9421aabb01d6e7f48d34c6bbdc359 here is new.

    Here's the pip installs for coloredlogs from PyPI for January 2020:

    | category | percent | downloads | |----------|--------:|----------:| | 3.7 | 43.45% | 247,468 | | 3.6 | 34.84% | 198,447 | | null | 6.02% | 34,265 | | 2.7 | 5.87% | 33,427 | | 3.5 | 4.91% | 27,946 | | 3.8 | 4.48% | 25,516 | | 3.4 | 0.44% | 2,506 | | 2.6 | 0.00% | 7 | | 3.9 | 0.00% | 7 | | Total | | 569,589 |

    Source: pip install -U pypistats && pypistats python_minor coloredlogs --last-month

    opened by hugovk 3
  • Windows 10 ANSI support

    Windows 10 ANSI support

    Windows 10 supports ANSI by default. But right now the use of Colorama on Windows is hardcoded. terminal.terminal_supports_colors looks for the env var ANSICON maybe the same can be used for coloredlogs?

    compatibility 
    opened by TheOneRing 3
  • After PIP install, ImportError: No module named coloredlogs

    After PIP install, ImportError: No module named coloredlogs

    I get this error when trying to import coloredlogs into my python program. I tried the suggestions here https://stackoverflow.com/questions/32680081/importerror-after-successful-pip-installation but they didn't help

    opened by Patronics 3
  • Same level is set for both StreamHandler and FileHanlder

    Same level is set for both StreamHandler and FileHanlder

    I am trying to use to logger with different levels for FileHandler (DEBUG) and StreamHandler (WARNING). However, after I set the handlers for the logger, and whether I set the level argument in coloredlog.install or not, the FileHandler debug level is overwritten with the level set in level argument in coloredlogs.install whether I set the argument or not.

    import logging, coloredlogs
    logger = logging.getLogger(__name__)
    
    c_handler = logging.StreamHandler()
    f_handler = logging.FileHandler("app.log", "w")
    
    c_handler.setLevel(logging.DEBUG)
    f_handler.setLevel(logging.DEBUG)
    
    c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
    f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    c_handler.setFormatter(c_format)
    f_handler.setFormatter(f_format)
    
    logger.addHandler(c_handler)
    logger.addHandler(f_handler)
    
    coloredlogs.install(level=logging.WARNING, logger=logger)
    
    logger.debug('This is a debug message')
    logger.info('This is an info message')
    logger.warning('This is a warning message')
    logger.error('This is an error message')
    logger.critical('This is a critical message')
    
    logger.critical("NICE MESSAGES")
    

    Here is the output in the app.logs file:

    2023-01-03 11:30:15,565 - __main__ - WARNING - This is a warning message
    2023-01-03 11:30:15,566 - __main__ - ERROR - This is an error message
    2023-01-03 11:30:15,567 - __main__ - CRITICAL - This is a critical message
    2023-01-03 11:30:15,567 - __main__ - CRITICAL - NICE MESSAGES
    

    You can that the file output doesn't include debug message, which I clearly have set above.

    opened by ahmedheakl 0
  • Strange coloring failure with a subprocess call

    Strange coloring failure with a subprocess call

    If I make a subprocess call (of any kind, Popen, run, check_call, check_output, e.t.c) it will break the coloring of every single further log.

    Windows Terminal: Windows Terminal

    PyCharm: PyCharm

    You can see that I added test1 and test2 logs before and after the subprocess call and only test2 broke.

    I have no idea why this is happening. I've also tried run() with stdout and stderr redirected to subprocess.DEVNULL, and it still happens.

    The relevant line of code can be found here:

    https://github.com/rlaphoenix/nton/blob/ccf9dbfcbb5ea25849733630173e96bf27062132/nton/main.py#L220

    opened by rlaphoenix 1
  • Examples of changing colours?

    Examples of changing colours?

    Are there any examples of changing colours? The official documentation seems to show everything as environment variables. How do you do this in code? Is it passed as a parameter to the install function similarly to fmt= ?

    opened by GanizaniSitara 1
  • add GitHub URL for PyPi

    add GitHub URL for PyPi

    Warehouse now uses the project_urls provided to display links in the sidebar on this screen, as well as including them in API responses to help the automation tool find the source code for Requests.

    opened by andriyor 0
  • Add defaults parameter to ColoredFormatter.__init__ in python3.10+

    Add defaults parameter to ColoredFormatter.__init__ in python3.10+

    Python 3.10 added a defaults argument to the Formatter.__init__ to enable you to use custom fields in the format string. Itʼd be nice if ColoredFormatter and coloredlogs.install supported this too.

    opened by dhouck 0
Beautifully colored, quick and simple Python logging

Python Quick Logging | QLogging Beautifully colored, quick and simple Python logging. This logger is based on Python logging package Screenshots: Term

null 45 Sep 25, 2022
A Python library that tees the standard output & standard error from the current process to files on disk, while preserving terminal semantics

A Python library that tees the standard output & standard error from the current process to files on disk, while preserving terminal semantics (so breakpoint(), etc work as normal)

Greg Brockman 7 Nov 30, 2022
Python logging made (stupidly) simple

Loguru is a library which aims to bring enjoyable logging in Python. Did you ever feel lazy about configuring a logger and used print() instead?... I

null 13.7k Jan 2, 2023
Structured Logging for Python

structlog makes logging in Python faster, less painful, and more powerful by adding structure to your log entries. It's up to you whether you want str

Hynek Schlawack 2.3k Jan 5, 2023
A cool logging replacement for Python.

Welcome to Logbook Travis AppVeyor Supported Versions Latest Version Test Coverage Logbook is a nice logging replacement. It should be easy to setup,

null 1.4k Nov 11, 2022
Stand-alone parser for User Access Logging from Server 2012 and newer systems

KStrike Stand-alone parser for User Access Logging from Server 2012 and newer systems BriMor Labs KStrike This script will parse data from the User Ac

BriMor Labs 69 Nov 1, 2022
Robust and effective logging for Python 2 and 3.

Robust and effective logging for Python 2 and 3.

Chris Hager 1k Jan 4, 2023
Python logging package for easy reproducible experimenting in research

smilelogging Python logging package for easy reproducible experimenting in research. Why you may need this package This project is meant to provide an

Huan Wang 20 Dec 23, 2022
A basic logging library for Python.

log.py ?? About: A basic logging library for Python with the capability to: save to files. have custom formats. have custom levels. be used instantiat

Sebastiaan Bij 1 Jan 19, 2022
Small toolkit for python multiprocessing logging to file

Small Toolkit for Python Multiprocessing Logging This is a small toolkit for solving unsafe python mutliprocess logging (file logging and rotation) In

Qishuai 1 Nov 10, 2021
Logging system for the TPC software.

tpc_logger Logging system for the TPC software. The TPC Logger class provides a singleton for logging information within C++ code or in the python API

UC Davis Machine Learning 1 Jan 10, 2022
A lightweight logging library for python applications

cakelog a lightweight logging library for python applications This is a very small logging library to make logging in python easy and simple. config o

null 2 Jan 5, 2022
Outlog it's a library to make logging a simple task

outlog Outlog it's a library to make logging a simple task!. I'm a lazy python user, the times that i do logging on my apps it's hard to do, a lot of

ZSendokame 2 Mar 5, 2022
metovlogs is a very simple logging library

metovlogs is a very simple logging library. Setup is one line, then you can use it as a drop-in print replacement. Sane and useful log format out of the box. Best for small or early projects.

Azat Akhmetov 1 Mar 1, 2022
Simple and versatile logging library for python 3.6 above

Simple and versatile logging library for python 3.6 above

Miguel 1 Nov 23, 2022
A Python package which supports global logfmt formatted logging.

Python Logfmter A Python package which supports global logfmt formatted logging. Install $ pip install logfmter Usage Before integrating this library,

Joshua Taylor Eppinette 15 Dec 29, 2022
Prettify Python exception output to make it legible.

pretty-errors Prettifies Python exception output to make it legible. Install it with python -m pip install pretty_errors If you want pretty_errors to

Iain King 2.6k Jan 4, 2023
Rich is a Python library for rich text and beautiful formatting in the terminal.

Rich 中文 readme • lengua española readme • Läs på svenska Rich is a Python library for rich text and beautiful formatting in the terminal. The Rich API

Will McGugan 41.5k Jan 7, 2023
Keylogger with Python which logs words into server terminal.

word_logger Experimental keylogger with Python which logs words into server terminal.

Selçuk 1 Nov 15, 2021