Last active
May 3, 2025 02:49
-
-
Save murarisumit/5d8458e4471fcec8bc60c96751fa1d30 to your computer and use it in GitHub Desktop.
Python logging instead of print #python #logging
This file contains hidden or 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
import sys | |
import logging | |
# We don't need to use print statement, using logger removes many python compatibilit | |
# and need to modify code later, we can just configure logger and rest code remains as it is. # | |
# logging.basicConfig(stream=sys.stdout, level=logging.INFO) | |
logger = logging.getLogger("LOGGER_NAME") | |
logger.setLevel(logging.DEBUG) | |
ch = logging.StreamHandler(sys.stdout) | |
ch.setLevel(logging.DEBUG) | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
ch.setFormatter(formatter) | |
logger.addHandler(ch) | |
logger.info("Test info log") | |
logger.debug("Test debug log") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment