August 2018
Intermediate to advanced
366 pages
10h 14m
English
To test logging to a file, we are going to create a short tool that computes up to the nth Fibonacci number based on the current time. If it's 3:01 P.M., we want to compute only 1 number, while if it's 3:59 P.M., we want to compute 59 numbers.
The software will provide the computed numbers as the output, but we also want to log up to which number it computed and when it was run:
import logging, sys if __name__ == '__main__': if len(sys.argv) < 2: print('Please provide logging file name as argument') sys.exit(1) logging_file = sys.argv[1] logging.basicConfig(level=logging.INFO, filename=logging_file, format='%(asctime)s %(name)s %(levelname)s: %(message)s') log = logging.getLogger(__name__) def fibo(num): log.info('Computing ...