Skip to content

Instantly share code, notes, and snippets.

@jonepl
Last active March 11, 2024 17:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonepl/dd5dc90a5bc1b86b2fc2b3a244af7fc6 to your computer and use it in GitHub Desktop.
Save jonepl/dd5dc90a5bc1b86b2fc2b3a244af7fc6 to your computer and use it in GitHub Desktop.
Python Logging Cheat Sheet
'''
File: BasicLogger.py
Description: Logging cheat sheet.. You can find the documentation at
https://docs.python.org/3/library/logging.html.
'''
import logging
import SpecificLogger
# DEBUG: Detailed information, typically of interest only when diagnosing problems.
# INFO: Confirmation that things are working as expected.
# WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
# ERROR: Due to a more serious problem, the software has not been able to perform some function.
# CRITICAL: A serious error, indicating that the program itself may be unable to continue running.
logging.basicConfig(filename='test.log', level=logging.DEBUG,
format='%(asctime)s:%(levelname)s:%(message)s')
def main() :
logging.info("Starting logging application")
result = SpecificLogger.add(10,20)
logging.info("Result from add {}".format(result))
result = SpecificLogger.multiply(10,20)
logging.info("Result from multiply {}".format(result))
if __name__ == "__main__":
main()
'''
File: Logging.py
Description: Logging module ment to reduce code in classes
'''
import logging
def setUpLogging(loggerName, level=logging.INFO) :
'''
loggerName: __name__
'''
logger = logging.getLogger(loggerName)
logger.setLevel(level)
formatter = logging.Formatter('%(levelname)s : %(name)s : %(message)s')
# Sets up file handler
fileHandler = logging.FileHandler(loggerName + ".log")
fileHandler.setFormatter(formatter)
# Sets up handler to std out
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(formatter)
streamHandler.setLevel(logging.DEBUG)
# Adds handler to logger
logger.addHandler(fileHandler)
logger.addHandler(streamHandler)
return logger
'''
File: SpecificLogger.py
Description: Use this to use logging to give your logger a unique name and include in
in mulitple classes. You can find the documentation at
https://docs.python.org/3/library/logging.html.
'''
import Logging
# DEBUG: Detailed information, typically of interest only when diagnosing problems.
# INFO: Confirmation that things are working as expected.
# WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
# ERROR: Due to a more serious problem, the software has not been able to perform some function.
# CRITICAL: A serious error, indicating that the program itself may be unable to continue running.
logger = Logging.setUpLogging(__name__)
def add(x, y):
result = x + y
logger.info('Performing add: {} + {} = {}'.format(x, y, result))
return result
def subtract(x, y):
result = x - y
logger.info('Performing subtract: {} - {} = {}'.format(x, y, result))
return result
def multiply(x, y):
result = x * y
logger.info('Performing multiply: {} * {} = {}'.format(x, y, result))
return result
def divide(x, y):
result = x/y
logger.info('Performing divide: {} / {} = {}'.format(x, y, result))
return result
if __name__ == "__main__":
add(10,20)
subtract(10,20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment