Skip to content

Instantly share code, notes, and snippets.

@stephenleo
Last active February 3, 2022 07:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenleo/fc48d08efaf98071d719c809900893bd to your computer and use it in GitHub Desktop.
Save stephenleo/fc48d08efaf98071d719c809900893bd to your computer and use it in GitHub Desktop.
[Medium] Stop Using Print! Python Logging for Data Scientists

Stop Using Print! Python Logging for Data Scientists

Code snippets for the Medium post: Link

import logging
# Setup Basic Configuration
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
# Instantiate the Logger
LOGGER_NAME = 'main'
logger = logging.getLogger(LOGGER_NAME)
# Create file handler and set level to custom level above CRITICAL
METRICS = 60
logging.addLevelName(METRICS, 'METRICS')
file_handler = logging.FileHandler('metrics.log')
file_handler.setLevel(METRICS)
# Add file handler to logger
logger.addHandler(file_handler)
# Create a custom logging level called "METRICS" with a value 60
# 60 is higher than CRITICAL logging.
# This ensures no other log levels write to file
METRICS = 60
# Unique name for the logger
LOGGER_NAME = 'main'
import constants as cst
import logging
import test_print
# Setup Basic Configuration
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
# Instantiate the Logger
logger = logging.getLogger(cst.LOGGER_NAME)
# Create file handler and set level to custom level above CRITICAL
logging.addLevelName(cst.METRICS, 'METRICS')
file_handler = logging.FileHandler('metrics.log')
file_handler.setLevel(cst.METRICS)
# Add file handler to logger
logger.addHandler(file_handler)
if __name__ == '__main__':
# Test out the logger from the original script
logger.info(
'This message is displayed on the terminal but NOT written to file')
logger.log(
cst.METRICS,
'This message is both displayed on the terminal AND written to file')
# Test out the logger in an imported function
test_print.test_print()
import constants as cst
import logging
# Instantiate the same logger
logger = logging.getLogger(cst.LOGGER_NAME)
def test_print():
logger.log(cst.METRICS, 'This also writes to file!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment