Skip to content

Instantly share code, notes, and snippets.

@murarisumit
Last active May 3, 2025 02:49
Show Gist options
  • Save murarisumit/5d8458e4471fcec8bc60c96751fa1d30 to your computer and use it in GitHub Desktop.
Save murarisumit/5d8458e4471fcec8bc60c96751fa1d30 to your computer and use it in GitHub Desktop.
Python logging instead of print #python #logging
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