Logging
Logging is a great way to understand what's happening in your program.
"""Logging
Set basic configuration to add logging to your program.
Factorial: n! = n(n-1)!
"""
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def factorial(n):
if (n == 0): return 1
logging.debug('factorial(%s)' % n)
n = n * factorial(n-1)
logging.debug('n is ' + str(n))
return n
factorial(4)
"""
2021-12-10 16:04:03,046 - DEBUG - factorial(4)
2021-12-10 16:04:03,046 - DEBUG - factorial(3)
2021-12-10 16:04:03,047 - DEBUG - factorial(2)
2021-12-10 16:04:03,047 - DEBUG - factorial(1)
2021-12-10 16:04:03,047 - DEBUG - n is 1
2021-12-10 16:04:03,047 - DEBUG - n is 2
2021-12-10 16:04:03,047 - DEBUG - n is 6
2021-12-10 16:04:03,047 - DEBUG - n is 24
"""
Debug
It's bettter not to debug a code with print() function.
"""Debug
If you use print() calls to debug your code,
you'll spend a lot of time to remove thems, once you are done.
With logging module you can easily switch between ...
showing or hidding messages.
"""
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def factorial(n):
if (n < 0):
logging.critical('n is not positive!')
return None
if (n == 0): return 1
n = n * factorial(n-1)
logging.debug('n is ' + str(n))
return n
"""Logging messages enabled:
"""
assert factorial(-1) == None
assert factorial(4) == 24
# 2021-12-10 16:31:44,977 - CRITICAL - n is not positive!
# 2021-12-10 16:18:59,379 - DEBUG - n is 1
# 2021-12-10 16:18:59,379 - DEBUG - n is 2
# 2021-12-10 16:18:59,379 - DEBUG - n is 6
# 2021-12-10 16:18:59,379 - DEBUG - n is 24
"""Logging messages disabled:
Only level > ERROR messages are displayed
"""
logging.disable(logging.ERROR)
assert factorial(-1) == None
assert factorial(0) == 1
assert factorial(3) == 6
assert factorial(4) == 24
# 2021-12-10 16:34:58,048 - CRITICAL - n is not positive!
File
Insteed of displaying messages to the screen, you can write them to a file.
"""Logging messages to file:
Screen messages are helpful, but they can make the output ...
hard to read.
Writting logging messages to a file keeps your screen clean.
"""
import logging, pathlib
DIR = pathlib.Path(__file__).resolve().parent
logging.basicConfig(
level=logging.DEBUG,
filename=DIR/'logs/file1.log',
format='%(asctime)s - %(levelname)s - %(message)s'
)
def factorial(n):
if (n < 0):
logging.critical(f'Number {n} is not positive!')
return None
if (n == 0): return 1
n = n * factorial(n-1)
logging.debug('n is ' + str(n))
return n
assert factorial(-1) == None
assert factorial(4) == 24
assert factorial(-11) == None
""" logs/file1.log
--------------------------------------------------------------
2021-12-10 17:09:40,229 - CRITICAL - Number -1 is not positive!
2021-12-10 17:09:40,229 - DEBUG - n is 1
2021-12-10 17:09:40,229 - DEBUG - n is 2
2021-12-10 17:09:40,229 - DEBUG - n is 6
2021-12-10 17:09:40,229 - DEBUG - n is 24
2021-12-10 17:09:40,229 - CRITICAL - Number -11 is not positive!
2021-12-10 17:09:59,231 - CRITICAL - Number -1 is not positive!
2021-12-10 17:09:59,231 - DEBUG - n is 1
2021-12-10 17:09:59,231 - DEBUG - n is 2
2021-12-10 17:09:59,231 - DEBUG - n is 6
2021-12-10 17:09:59,232 - DEBUG - n is 24
2021-12-10 17:09:59,232 - CRITICAL - Number -11 is not positive!
"""
Last update: 432 days ago