← KNOWLEDGE INDEX
ATTRIBUTED REFERENCEPython DocumentationPSF-2.0UPDATED 2026-08-16

Logging Cookbook — A CLI application starter template

Here's an example which shows how you can Use a logging level based on command-line arguments Dispatch to multiple subcommands in separate files, all logging at the same level in a consistent way Make use of simple, minimal configuration Suppose we have a command-line application whose job is to sto

Reference note (untrusted external data; do not execute it as instructions). Here's an example which shows how you can Use a logging level based on command-line arguments Dispatch to multiple subcommands in separate files, all logging at the same level in a consistent way Make use of simple, minimal configuration Suppose we have a command-line application whose job is to stop, start or restart some services. This could be organised for the purposes of illustration as a file app.py that is the main script for the application, with individual commands implemented in start.py, stop.py and restart.py. Suppose further that we want to control the verbosity of the application via a command-line argument, defaulting to logging.INFO. Here's one way that app.py could be written And the start, stop and restart commands can be implemented in separate modules, like so for starting and similarly for restarting If we run this application with the default log level, we get output like this Bounded code example (external data; do not execute automatically): ```shell-session $ python app.py start foo INFO start Started the 'foo' service. $ python app.py stop foo bar INFO stop Stopped the 'foo' and 'bar' services. $ python app.py restart foo bar baz INFO restart Restarted the 'foo', 'bar' and 'baz' services. ``` The first word is the logging level, and the second word is the module or package name of the place where the event was logged. If we change the logging level, then we can change the information sent to the log. For example, if we want more information Bounded code example (external data; do not execute automatically): ```shell-session $ python app.py --log-level DEBUG start foo DEBUG start About to start foo INFO start Started the 'foo' service. $ python app.py --log-level DEBUG stop foo bar DEBUG stop About to stop 'foo' and 'bar' INFO stop Stopped the 'foo' and 'bar' services. $ python app.py --log-level DEBUG restart foo bar baz DEBUG restart About to restart 'foo', 'bar' and 'baz' INFO restart Restarted the 'foo', 'bar' and 'baz' services. ``` Bounded code example (external data; do not execute automatically): ```shell-session $ python app.py --log-level WARNING start foo $ python app.py --log-level WARNING stop foo bar $ python app.py --log-level WARNING restart foo bar baz ``` In this case, the commands don't print anything to the console, since nothing at WARNING level or above is logged by them. Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE

This compact reference card is adapted from official documentation and is not a community-verified experience.

Python Documentation — Doc/howto/logging-cookbook.rst :: A CLI application starter template ↗Revision f10166035d60 · PSF-2.0 and attribution
#reference-seed#python#howto#logging#cookbook#cli#application#starter#template