Skip to content
Snippets Groups Projects
Commit f943f8df authored by Ralf Mueller's avatar Ralf Mueller :fishing_pole_and_fish:
Browse files

add example for logging

parent 5c4e5be2
No related branches found
No related tags found
No related merge requests found
Pipeline #11313 passed
......@@ -62,11 +62,58 @@ Lessions:
More on floating point problems [here](https://floating-point-gui.de/) of as [pdf](https://www.phys.uconn.edu/~rozman/Courses/P2200_15F/downloads/floating-point-guide-2015-10-15.pdf)
# How to improve programs step-ny-step: debuggability
If `print()` is so useful, why not integrating it into the program?
## Logging
```python
import sys
import logging
logging.basicConfig(level=logging.INFO)
# or
# logging.basicConfig(filename='example.log',
# encoding='utf-8',
# format='%(asctime)s :: %(levelname)s :: %(message)s',
# level=logging.DEBUG)
def sum(a,b):
logging.debug("started method sum()")
logging.info("Got a = {0} of type {1}".format(a,type(a)))
logging.info("Got b = {0} of type {1}".format(b,type(b)))
return a+b
if __name__ == "__main__":
a,b = sys.argv[1], sys.argv[2]
print("{1}+{2}= {0}".format(sum(a,b)),a,b)
```
### basic log levels
- DEBUG: Detailed information, for diagnosing problems
- INFO: Confirm things are working as expected
- WARNING: Something unexpected happened, or indicative of some problem. But the software is still working as expected
- ERROR: More serious problem, the software is not able to perform some function
- CRITICAL: A serious error, the program itself may be unable to continue running
## Exceptions: try/except
Python has means to deal with non-intended behaviour called Exceptions. Using them will make your scripts easier to understand by others (and by yourself next week).
```python
try:
filehandle = open(filename)
except IOError:
print('File cannot be opened:', filename)
exit()
```
- Think about what could possibly go wrong
- Think about which requirements must be met for your code work
# python's own debugger: pdb/pudb
[list of python debugging tools](https://wiki.python.org/moin/PythonDebuggingTools)
# How to improve programs step-ny-step: debuggability
## logging
## try/except
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment