Skip to content

Instantly share code, notes, and snippets.

@msyvr
Last active October 4, 2021 23:36
Show Gist options
  • Save msyvr/e16023f6410ade360273ca00423ce231 to your computer and use it in GitHub Desktop.
Save msyvr/e16023f6410ade360273ca00423ce231 to your computer and use it in GitHub Desktop.
python logger - from socratica https://www.socratica.com/lesson/logging
import logging
import math #since we'll need the sqrt fn
# Create and configure logger
LOG_FORMAT = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(filename = "log_file.log", level = logging.DEBUG, format = LOG_FORMAT, filemode = 'w')
# we'll call the logger without a name (aka 'root logger') because there are subtle issues with logger names (*what are they?*)
logger = logging.getLogger()
def quadratic_formula(a, b, c):
""" Return the solution to ax^2 + bx + c = 0."""
logger.info("quadratic_formula({0}, {1}, {2})".format(a, b, c))
# Compute the discriminant: b^2 - 4ac
logger.debug("# Compute the discriminant")
disc = b**2 - 4*a*c
# Compute the two roots
logger.debug("# Compute the two roots")
r1 = (-b + math.sqrt(disc))/(2*a)
r2 = (-b - math.sqrt(disc))/(2*a)
# Return the roots as a tuple
logger.debug("# Return the roots as a tuple")
return(r1, r2)
if __name__ == '__main__':
a = int(input("For ax^2 + bx + c = 0, choose integer a: "))
b = int(input("For ax^2 + bx + c = 0, choose integer b: "))
c = int(input("For ax^2 + bx + c = 0, choose integer c: "))
roots = quadratic_formula(a, b, c)
print(f'logger level set to: {logger.level}')
print(f"the roots are: {roots}")
# edge cases:
# sqrt(negative #)
# roots = quadratic_formula(1, 0, 1)
# print(f"roots are: {roots}")
# divide by zero: a = 0
# roots = quadratic_formula(1, 1, 4)
# print(f"roots are: {roots}")
# Test the logger
#logger.debug("Harmless debug message.")
#logger.info("Just some useful info.")
#logger.warning("This is a warning message: eg I'm sorry, I can't do that.")
#logger.error("This is an error message: eg Did you try to divide by zero or carry out some other invalid action?")
#logger.critical("This is a critical message: eg The world is on fire!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment