Package: gr-colorlog
Log formatting with colors!colorlog.ColoredFormatteris a formatter for use with Python'sloggingmodule that outputs records using terminal colors.Source on GitHubPackages on PyPIBuilds on Travis CIInstallationInstall from PyPI with:pipinstallcolorlogSeveral Linux distributions provide official packages (Debian,Gentoo,OpenSuseandUbuntu), and others have user provided packages (Arch AUR,BSD ports,Conda,Fedora packaging scripts).Usageimportcolorloghandler=colorlog.StreamHandler()handler.setFormatter(colorlog.ColoredFormatter('%(log_color)s%(levelname)s:%(name)s:%(message)s'))logger=colorlog.getLogger('example')logger.addHandler(handler)TheColoredFormatterclass takes several arguments:format: The format string used to output the message (required).datefmt: An optional date format passed to the base class. Seelogging.Formatter.reset: Implicitly adds a color reset code to the message output, unless the output already ends with one. Defaults toTrue.log_colors: A mapping of record level names to color names. The defaults can be found incolorlog.default_log_colors, or the below example.secondary_log_colors: A mapping of names tolog_colorsstyle mappings, defining additional colors that can be used in format strings. See below for an example.style: Available on Python 3.2 and above. Seelogging.Formatter.Color escape codes can be selected based on the log records level, by adding
parameters to the format string:log_color: Return the color associated with the records level.<name>_log_color: Return another color based on the records level if the formatter has secondary colors configured (seesecondary_log_colorsbelow).Multiple escape codes can be used at once by joining them with commas when
configuring the color for a log level (but can't be used directly in the format
string). For example,black,bg_whitewould use the escape codes for black
text on a white background.The following escape codes are made available for use in the format string:{color},fg_{color},bg_{color}: Foreground and background colors.bold,bold_{color},fg_bold_{color},bg_bold_{color}: Bold/bright colors.thin,thin_{color},fg_thin_{color}: Thin colors (terminal dependent).reset: Clear all formatting (both foreground and background colors).The available color names areblack,red,green,yellow,blue,purple,cyanandwhite.ExamplesThe following code creates aColoredFormatterfor use in a logging setup,
using the default values for each argument.fromcolorlogimportColoredFormatterformatter=ColoredFormatter("%(log_color)s%(levelname)-8s%(reset)s%(blue)s%(message)s",datefmt=None,reset=True,log_colors={'DEBUG':'cyan','INFO':'green','WARNING':'yellow','ERROR':'red','CRITICAL':'red,bg_white',},secondary_log_colors={},style='%')Usingsecondary_log_colorsSecondary log colors are a way to have more than one color that is selected
based on the log level. Each key insecondary_log_colorsadds an attribute
that can be used in format strings (messagebecomesmessage_log_color), and
has a corresponding value that is identical in format to thelog_colorsargument.The following example highlights the level name using the default log colors,
and highlights the message in red forerrorandcriticallevel log messages.fromcolorlogimportColoredFormatterformatter=ColoredFormatter("%(log_color)s%(levelname)-8s%(reset)s%(message_log_color)s%(message)s",secondary_log_colors={'message':{'ERROR':'red','CRITICAL':'red'}})WithdictConfiglogging.config.dictConfig({'formatters':{'colored':{'()':'colorlog.ColoredFormatter','format':"%(log_color)s%(levelname)-8s%(reset)s%(blue)s%(message)s"}}})A full example dictionary can be found intests/test_colorlog.py.WithfileConfig...[formatters]keys=color[formatter_color]class=colorlog.ColoredFormatterformat=%(log_color)s%(levelname)-8s%(reset)s %(bg_blue)s[%(name)s]%(reset)s %(message)s from fileConfigdatefmt=%m-%d %H:%M:%SAn instance of ColoredFormatter created with those arguments will then be used
by any handlers that are configured to use thecolorformatter.A full example configuration can be found intests/test_config.ini.With custom log levelsColoredFormatter will work with custom log levels added withlogging.addLevelName:importlogging,colorlogTRACE=5logging.addLevelName(TRACE,'TRACE')formatter=colorlog.ColoredFormatter(log_colors={'TRACE':'yellow'})handler=logging.StreamHandler()handler.setFormatter(formatter)logger=logging.getLogger('example')logger.addHandler(handler)logger.setLevel('TRACE')logger.log(TRACE,'a message using a custom level')Compatibilitycolorlog works on Python 2.6 and above, including Python 3.On Windows,coloramais required forcolorlogto work properly.  It will
automatically be included when installingcolorlogon windows.TestsTests similar to the above examples are found intests/test_colorlog.py.toxwill run the tests under all compatible python versions.Projects using colorlogCounterpartyErrbotPythranzenlogLicenceCopyright (c) 2012 Sam Clementssam@borntyping.co.ukPermission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.