### Install coloredlogs with pip Source: https://github.com/xolox/python-coloredlogs/blob/master/README.rst Install the coloredlogs package from PyPI using pip. ```console $ pip install coloredlogs ``` -------------------------------- ### Example output with default log format Source: https://github.com/xolox/python-coloredlogs/blob/master/README.rst Example log output produced by the default log format, showing timestamps, hostname, logger name, process ID, and level. ```text 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' ... ``` -------------------------------- ### Install colorama for Windows ANSI support Source: https://github.com/xolox/python-coloredlogs/blob/master/README.rst Install the optional colorama package for native ANSI support on Windows when the automatic fallback is not working. ```console $ pip install colorama ``` -------------------------------- ### Minimal crontab for coloredlogs HTML output Source: https://github.com/xolox/python-coloredlogs/blob/master/README.rst A minimal crontab configuration that sets email headers and runs a command with coloredlogs --to-html to convert ANSI escape sequences to HTML for email delivery. Requires the external 'script' program to be installed. ```crontab MAILTO="your-email-address@here" CONTENT_TYPE="text/html" * * * * * root coloredlogs --to-html your-command ``` -------------------------------- ### Basic usage of coloredlogs.install() Source: https://github.com/xolox/python-coloredlogs/blob/master/README.rst Shows how to set up coloredlogs with the root logger or a specific logger, and demonstrates logging at various levels. ```python 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") ``` -------------------------------- ### coloredlogs.install() with custom format including milliseconds Source: https://github.com/xolox/python-coloredlogs/blob/master/README.rst Call coloredlogs.install() with a custom format string that includes milliseconds. ```python coloredlogs.install(fmt='%(asctime)s,%(msecs)03d %(hostname)s %(name)s[%(process)d] %(levelname)s %(message)s') ``` -------------------------------- ### Set environment variables and run coloredlogs demo Source: https://github.com/xolox/python-coloredlogs/blob/master/docs/examples/custom-colors.txt Sets environment variables to customize the log format, date format, and color styles, then runs the coloredlogs demo to display the customized output. ```bash $ export COLOREDLOGS_LOG_FORMAT='%(asctime)s - %(message)s' $ export COLOREDLOGS_DATE_FORMAT='%H:%M:%S' $ export COLOREDLOGS_FIELD_STYLES='' # no styles $ export COLOREDLOGS_LEVEL_STYLES='spam=22;debug=28;verbose=34;notice=220;warning=202;success=118,bold;error=124;critical=background=red' $ coloredlogs --demo ``` ```output 15:45:23 - message with level spam (5) 15:45:24 - message with level debug (10) 15:45:25 - message with level verbose (15) 15:45:26 - message with level info (20) 15:45:27 - message with level notice (25) 15:45:28 - message with level warning (30) 15:45:29 - message with level success (35) 15:45:30 - message with level error (40) 15:45:31 - message with level critical (50) ``` -------------------------------- ### Set custom log format and run demo Source: https://github.com/xolox/python-coloredlogs/blob/master/docs/examples/custom-log-format.txt Sets the COLOREDLOGS_LOG_FORMAT environment variable to include the hostname, timestamp, and message, then runs the coloredlogs demo to show the effect. ```bash export COLOREDLOGS_LOG_FORMAT='[%(hostname)s] %(asctime)s %(message)s' ``` ```bash coloredlogs --demo ``` -------------------------------- ### Enable milliseconds with coloredlogs.install() Source: https://github.com/xolox/python-coloredlogs/blob/master/README.rst Pass the milliseconds argument to coloredlogs.install() to include milliseconds in timestamps. Supported since release 7.1. ```python coloredlogs.install(milliseconds=True) ``` -------------------------------- ### coloredlogs --demo output Source: https://github.com/xolox/python-coloredlogs/blob/master/docs/examples/defaults.txt Run `coloredlogs --demo` to see a sample log message for every supported level. The output shows the level name and its numeric value (e.g., SPAM=5, DEBUG=10, VERBOSE=15, INFO=20, NOTICE=25, WARNING=30, SUCCESS=35, ERROR=40, CRITICAL=50). ```text $ coloredlogs --demo 2018-01-14 15:45:54 peter-mbp coloredlogs.demo[23238] SPAM message with level spam (5) 2018-01-14 15:45:55 peter-mbp coloredlogs.demo[23238] DEBUG message with level debug (10) 2018-01-14 15:45:56 peter-mbp coloredlogs.demo[23238] VERBOSE message with level verbose (15) 2018-01-14 15:45:57 peter-mbp coloredlogs.demo[23238] INFO message with level info (20) 2018-01-14 15:45:58 peter-mbp coloredlogs.demo[23238] NOTICE message with level notice (25) 2018-01-14 15:45:59 peter-mbp coloredlogs.demo[23238] WARNING message with level warning (30) 2018-01-14 15:46:00 peter-mbp coloredlogs.demo[23238] SUCCESS message with level success (35) 2018-01-14 15:46:01 peter-mbp coloredlogs.demo[23238] ERROR message with level error (40) 2018-01-14 15:46:02 peter-mbp coloredlogs.demo[23238] CRITICAL message with level critical (50) ``` -------------------------------- ### Set custom date/time format with COLOREDLOGS_DATE_FORMAT Source: https://github.com/xolox/python-coloredlogs/blob/master/docs/examples/custom-datetime-format.txt Set the COLOREDLOGS_LOG_FORMAT and COLOREDLOGS_DATE_FORMAT environment variables to customize the log output, then run the coloredlogs demo to see the effect. The output shows timestamps in the HH:MM:SS format. ```bash $ export COLOREDLOGS_LOG_FORMAT='%(asctime)s - %(message)s' $ export COLOREDLOGS_DATE_FORMAT='%H:%M:%S' $ coloredlogs --demo 15:45:34 - message with level spam (5) 15:45:35 - message with level debug (10) 15:45:36 - message with level verbose (15) 15:45:37 - message with level info (20) 15:45:38 - message with level notice (25) 15:45:39 - message with level warning (30) 15:45:40 - message with level success (35) 15:45:41 - message with level error (40) 15:45:42 - message with level critical (50) ``` -------------------------------- ### Default log format Source: https://github.com/xolox/python-coloredlogs/blob/master/README.rst The default log format used by ColoredFormatter, shown as a format string. ```text %(asctime)s %(hostname)s %(name)s[%(process)d] %(levelname)s %(message)s ``` -------------------------------- ### Log format including milliseconds Source: https://github.com/xolox/python-coloredlogs/blob/master/README.rst A log format that includes milliseconds using the msecs field. ```text %(asctime)s,%(msecs)03d %(hostname)s %(name)s[%(process)d] %(levelname)s %(message)s ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.