Package: click-logging
Click-loggingSimple and beautiful logging for click applicationsProject sources and documentation are available onGithubDocumentationGetting startedAssuming you have this Click application:importclick@click.command()defcli():click.echo("Dividing by zero.")try:1/0exceptZeroDivisionError:click.echo("ERROR: Failed to divide by zero.")Ignore the application's core functionality for a moment. The much more pressing question here is: How do we add an option to not print anything on success? We could try this:importclick@click.command()@click.option('--quiet',default=False,is_flag=True)defcli(quiet):ifnotquiet:click.echo("Dividing by zero.")try:1/0exceptZeroDivisionError:click.echo("ERROR: Failed to divide by zero.")Wrapping if-statements around eachecho-call is cumbersome though. And with that, we discover logging:importloggingimportclicklogger=logging.getLogger(__name__)# More setup for logging handlers here@click.command()@click.option('--quiet',default=False,is_flag=True)defcli(quiet):ifquiet:logger.setLevel(logging.ERROR)else:logger.setLevel(logging.INFO)# ...Logging is a better solution, but partly because Python's logging module aims to be so generic, it doesn't come with sensible defaults for CLI applications. At some point you might also want to expose more logging levels through more options, at which point the boilerplate code grows even more.This is where click-logging comes in:importloggingimportclickimportclick_logginglogger=logging.getLogger(__name__)click_logging.basic_config(logger)@click.command()@click_logging.simple_verbosity_option(logger)defcli():logger.info("Dividing by zero.")try:1/0exceptZeroDivisionError:logger.error("Failed to divide by zero.")The output will look like this:Dividing by zero.
error: Failed to divide by zero.Theerror:-prefix will be red, unless the output is piped to another command.Thesimple_verbosity_optiondecorator adds a--verbosityoption that takes a (case-insensitive) value ofDEBUG,INFO,WARNING,ERROR, orCRITICAL, and callssetLevelon the given logger accordingly.noteMake sure to define thesimple_verbosity_optionas early as possible. Otherwise logging setup will not be early enough for some of your other eager options.Customize outputYou can customizeclick stylesfor each log level withstyle_kwargskeyword argument ofbasic_configfunction.importloggingimportclick_logginglogger=logging.getLogger(__name__)style_kwargs={'error':dict(fg='red',blink=True),'exception':dict(fg='red',blink=True),'critical':dict(fg='red',blink=True)}click_logging.basic_config(logger,style_kwargs=style_kwargs)You can customizeclick echofor each log level usingecho_kwargskeyword argument ofbasic_configfunction.importloggingimportclick_logginglogger=logging.getLogger(__name__)echo_kwargs={'error':dict(err=True),'exception':dict(err=True),'critical':dict(err=True),}click_logging.basic_config(logger,echo_kwargs=True)ForkThis is a fork ofclick-contrib/click-logMotivations of the fork:semantic-release and github actions to merge and release often.more configuration options.LicenseLicensed under the MIT, seeLICENSE.Changelogv1.0.1 (2021-01-06)FixAdd missing string interpolation in error message (3de6f06)help:Add trailing dot to help text (1ed426f)Documentationreadme:Enhance README.md formatting (3507ba3)v1.0.0 (2021-01-06)Featurecustomize:Add more customization capabilities in basic_config (621c0a2)Breakingclick_log has been renamed to click_logging  (f4a8ddf)