Created
February 9, 2023 04:06
-
-
Save thsutton/d358901073d899cf3de7890f169c4d05 to your computer and use it in GitHub Desktop.
Basic Python logging usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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