Skip to content

Instantly share code, notes, and snippets.

@thsutton
Created February 9, 2023 04:06
Show Gist options
  • Save thsutton/d358901073d899cf3de7890f169c4d05 to your computer and use it in GitHub Desktop.
Save thsutton/d358901073d899cf3de7890f169c4d05 to your computer and use it in GitHub Desktop.
Basic Python logging usage
#!/usr/bin/env python3
#
# I do this infrequently enough I always forget how this stuff works and have to read the code to figure it out.
#
import logging
import sys
formatter = logging.Formatter(
fmt='%(asctime)s - %(levelname)s - %(message)s',
datefmt=None,
)
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(formatter)
logger = logging.getLogger("foo")
logging.getLogger().addHandler(handler)
def main(args):
if "--verbose" in args:
logging.getLogger().setLevel(level="DEBUG")
logging.warning("logging warning")
logger.warning("logger warning")
logger.debug("logger debug")
logger.info("logger info")
if __name__ == "__main__":
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment