Package: yacl
YACL - Yet Another Color LoggerOverviewYACL is a very simple to use color logger for Python intended to be used for stderr logging. It can be set up with a
single function call in existing projects and enables colored logging output with reasonable defaults. Colors are
disabled automatically if stderr is not connected to a tty (e.g. on file redirection) or if not supported by the
connected terminal. Currently, Linux and macOS are supported.You can use Markdown style formattings to produce bold and italic text. Additionally, text enclosed in double
underscores will be displayed underlined. YACL checks the terminal capabilities and automatically disables unsupported
formats.Breaking changesFrom version 0.4.x to 0.5:Color codes are now appliedafterthe logger created the final logging string with all placeholders replaced.Example:item_count=3logger.info("Found%ditems.",item_count)Before version 0.5, color codes were applied to the string"Found %d items". Now, color codes are added to the
final string"Found 3 items". This requires the library user to rework customkeyword_colorsdictionaries but is
more flexible for applying color rules.InstallationYACL is available on PyPI for Python 3.3+ and can be installed withpip:python3-mpipinstallyaclIf you use Arch Linux or one of its derivatives, you can also installyaclfrom theAUR:yay-Spython-yaclUsageSimpleCallsetup_colored_stderr_loggingafter the root logger has been set up, for example:#!/usr/bin/env python3importloggingfromyaclimportsetup_colored_stderr_loggingdefmain():logging.basicConfig(level=logging.DEBUG)setup_colored_stderr_logging()if__name__=="__main__":main()Afterwards, get module level loggers and use them without any further configuration:importlogginglogger=logging.getLogger(__name__)defmy_func():logger.debug('Failed to open file "abc"')You will get an output like:This example only works if you don't attach any output handlers to loggers other than the root logger as recommended in
thePython logging documentation:If you attach a handler to a logger and one or more of its ancestors, it may emit the same record multiple times. In
general, you should not need to attach a handler to more than one logger - if you just attach it to the appropriate
logger which is highest in the logger hierarchy, then it will see all events logged by all descendant loggers,
provided that their propagate setting is left set to True. A common scenario is to attach handlers only to the root
logger, and to let propagation take care of the rest.CustomizationYou can pass several arguments to thesetup_colored_stderr_loggingfunction to customize the logging behavior:logger: The logger which will be configured to print colored logging output to stderr. By default, the root logger
is used.format_string: The format string to use for logging messages. By default the logging format[%(levelname)s] (%(name)s:%(lineno)s:%(funcName)s): %(message)sis used.Important: All formats must be passed asstring types. For example, in the default format,linenois given
as string ((%lineno)s) and not as number ((%lineno)d).remove_other_handlers: Bool flag to remove all other output handlers on the given logger. Is set totrueby
default to avoid duplicate logging messages.attribute_colors: A dictionary which assigns colors to logging attributes (which are used in the logging format
string). This dictionary is merged with the internal defaults:fromyaclimportTerminalColorCodes_attribute_colors={"funcName":TerminalColorCodes.blue,"lineno":TerminalColorCodes.yellow,"name":TerminalColorCodes.cyan,}keyword_colors: A dictionary which assigns colors to a given regular expressions. This setting can be used to
highlight expressions in the logging messages. This dictionary is merged with the internal defaults:fromyaclimportTerminalColorCodeskeyword_colors={r"\bcritical( error)?\b":TerminalColorCodes.red+TerminalColorCodes.blink+TerminalColorCodes.bold,r"\bdebug(ged|ging)?\b":TerminalColorCodes.green+TerminalColorCodes.bold,r"\berror\b":TerminalColorCodes.red+TerminalColorCodes.bold,r"\bfail(ed|ing)?\b":TerminalColorCodes.red+TerminalColorCodes.bold,r"\binfo\b":TerminalColorCodes.blue+TerminalColorCodes.bold,r"\bwarn(ed|ing)?\b":TerminalColorCodes.yellow+TerminalColorCodes.bold,r'"[^"]*"':TerminalColorCodes.yellow,r"\*([^*]+)\*":TerminalColorCodes.italics,r"\*\*([^*]+)\*\*":TerminalColorCodes.bold,r"__([^_]+)__":TerminalColorCodes.underline,r"`([^`]+)`":TerminalColorCodes.standout,}Example: Pass a dictionary{r"'[^']*'":TerminalColorCodes.green+TerminalColorCodes.italics,}to highlight strings in single quotes with green color and italic font (if supported by the Terminal).level_colors: A dictionary which assigns colors to logging levels (DEBUG, INFO, ...). This dictionary is merged with
the internal defaults:fromyaclimportTerminalColorCodeslevel_colors={"DEBUG":TerminalColorCodes.green+TerminalColorCodes.bold,"INFO":TerminalColorCodes.blue+TerminalColorCodes.bold,"WARNING":TerminalColorCodes.yellow+TerminalColorCodes.bold,"ERROR":TerminalColorCodes.red+TerminalColorCodes.bold,"CRITICAL":TerminalColorCodes.red+TerminalColorCodes.blink+TerminalColorCodes.bold,}Colored ExceptionsIfPygmentsis installed, YACL exports an additonal functionsetup_colored_exceptionsto generate colored exception tracebacks. You can force to install Pygments as a YACL
dependency with thecolored_exceptionsextra:python3-mpipinstall'yacl[colored_exceptions]'The functionsetup_colored_exceptionsneeds to be called once (for example aftersetup_colored_stderr_logging) to
install a customPython excepthook. It takes an optional
bool parameterdark_backgroundwhich can be set toTrueto activate brighter colors on dark terminal backgrounds. A
full example is:#!/usr/bin/env python3importloggingfromyaclimportsetup_colored_exceptions,setup_colored_stderr_loggingdefmain():logging.basicConfig(level=logging.DEBUG)setup_colored_stderr_logging()setup_colored_exceptions()if__name__=="__main__":main()ContributingPlease openan issue on GitHubif you experience bugs or miss
features. Please consider to send a pull request if you can spend time on fixing the issue yourself. This project usespre-committo ensure code quality and a consistent code style. Runmakegit-hooks-installto install all linters as Git hooks in your local clone ofyacl.