Code snippets for the Medium post: Link
Last active
February 3, 2022 07:17
-
-
Save stephenleo/fc48d08efaf98071d719c809900893bd to your computer and use it in GitHub Desktop.
[Medium] Stop Using Print! Python Logging for Data Scientists
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
import logging | |
# Setup Basic Configuration | |
logging.basicConfig(level=logging.INFO, | |
format='%(asctime)s %(levelname)s: %(message)s', | |
datefmt='%Y-%m-%d %H:%M:%S') |
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
# Instantiate the Logger | |
LOGGER_NAME = 'main' | |
logger = logging.getLogger(LOGGER_NAME) |
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
# 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) |
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
# 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' |
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
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() |
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
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