Create a directory named logging
and copy (or write yourself) logging_demo.py
.
Part 1: Add logging to logging_demo
These exercises use the logging_demo.py
file.
- In the file
logging_demo.py
complete the code forlog_demo(logger)
.- Log a message using each of the 5 standard logging functions (debug, warning, info, error, critical).
- For each log level, write a message that is a good example of what you would log at that level. Write your own message, not something from the class presentation.
- Run the code without any configuration of loggers. All the configuration code in the
__main__
block must be commented out.- which log messages are printed?
- which log messages are not printed?
- what is the format of the output?
- In
__main__
, before callinglog_demo
, call basicConfig with the default values:logging.basicConfig()
Run the code.
- which log messages are printed?
- did the format of the output change? how different?
- Add a
level=...
parameter to the call tobasicConfig
to set the threshold log level tologging.DEBUG
. Run the code again.- what is different?
- Complete the
console_config
function. The function should set:- log threshold is WARNING
- log format is “{datetime} {loglevelname}: {message}”
- log output goes to the console (the default) Run it.
- How did the output change from previous case?
- Complete the
file_config
function. The function should use basicConfig to:- log threshold is DEBUG
- log format is “{datetime} {logger_name} {loglevelname} {functionName}: {message}”
- functionName = name of function that logged the message
- send log messages to the file
demo.log
- modify
__main__
to usefile_config
instead ofconsole_config
. Run the program. - Did it print any messages on the console? (should not)
- Are messages logged to the file?
- Run it again. View the file
demo.log
.- Did the 2nd run append messages to the file or overrite the file contents? (Usually you want to append messages.)
- In
__main__
instead of using the root logger, create a logger named “demo”. Rerun the code.- Can you see any changes in the log messages?
Push your final code to Github.
Part 2: Add Logging to factor.py
- In
factor.py
replace “print” statements with suitable logging statements. - You should print the factors (the result) and a user-friendly message if an exception occurs.
- Log the exceptions in “main”, including a stacktrace (
logger.exception
). Print a user-friend message instead of exception. - Create a function named
configure()
that configures the root logger so that all log messages all go to a file namedfactor.log
. - Call
configure
from main. - Test your code.
Push your final factor.py
to Github.
Integer to factor: 99
Factors of 99 are [3, 3, 11]
Integer to factor: six <-- exception written to factor.log
Please input an integer or 0 to quit <-- user-friendly message
Integer to factor:
Logging and Log Formats
-
Python Logging complete documentation for logging, with links to tutorials & cookbook. Easy to understand after you read the Logging Howto.
-
Logging Formats section lists all the things you can include in the log message format.