Python-Ref > Basic usage > Debugging > Using python logging module
 
 

<-^^->

Using python logging module

When you need more sophisticated logging facilities that the print command can supply, there is a ready-to-use python module called logging. It allows logging on different levels and also to different destinations. The strengths of this module will show especially in case of larger projects.
The following two examples demonstrate this module's usage on a short code. The two examples are the same, the only difference is in the amount of data that is printed, which is controlled by logging.basicConfig(level=logging.LEVEL).
Expand/Shrink
Zdroj: (debug4-1.py)
  1   import logging
  2   
  3   total = 0
  4   used_numbers = 0
  5   input_data = [1,2,4,"6","7","X"]
  6   for a in input_data:
  7     logging.debug( "Processing value '%s'", a)
  8     if type(a) == int:
  9       logging.info( "Integer value %d", a)
 10       total += a
 11       used_numbers += 1
 12     else:
 13       try:
 14         b = int( a)
 15       except ValueError:
 16         logging.error( "Value '%s' is not convertible to int, skipping.", a)
 17       else:
 18         logging.warn( "Value '%s' was not integer, conversion was used", a)
 19         total += b
 20         used_numbers += 1
 21   logging.info( "%d out of %d values were used in computation",
 22                 used_numbers, len( input_data))
stderr:
WARNING:root:Value '6' was not integer, conversion was used
WARNING:root:Value '7' was not integer, conversion was used
ERROR:root:Value 'X' is not convertible to int, skipping.
Doba běhu: 27.4 ms
Expand/Shrink
Rozdíl proti: debug4-1.py
@@ -1,4 +1,5 @@
 import logging
+logging.basicConfig(level=logging.DEBUG) 
 total = 0
 used_numbers = 0
Zdroj: (debug4-2.py) +++
stderr:
DEBUG:root:Processing value '1'
INFO:root:Integer value 1
DEBUG:root:Processing value '2'
INFO:root:Integer value 2
DEBUG:root:Processing value '4'
INFO:root:Integer value 4
DEBUG:root:Processing value '6'
WARNING:root:Value '6' was not integer, conversion was used
DEBUG:root:Processing value '7'
WARNING:root:Value '7' was not integer, conversion was used
DEBUG:root:Processing value 'X'
ERROR:root:Value 'X' is not convertible to int, skipping.
INFO:root:5 out of 6 values were used in computation
Doba běhu: 26.6 ms
The small change introduced in the next example switches logging from stderr to a file.
Expand/Shrink
Rozdíl proti: debug4-2.py
@@ -1,5 +1,5 @@
 import logging
-logging.basicConfig(level=logging.DEBUG) 
+logging.basicConfig(level=logging.DEBUG, filename="log.log") 
 total = 0
 used_numbers = 0
Zdroj: (debug4-3.py) +++
DEBUG:root:Processing value '1'
INFO:root:Integer value 1
DEBUG:root:Processing value '2'
INFO:root:Integer value 2
DEBUG:root:Processing value '4'
INFO:root:Integer value 4
DEBUG:root:Processing value '6'
WARNING:root:Value '6' was not integer, conversion was used
DEBUG:root:Processing value '7'
WARNING:root:Value '7' was not integer, conversion was used
DEBUG:root:Processing value 'X'
ERROR:root:Value 'X' is not convertible to int, skipping.
INFO:root:5 out of 6 values were used in computation
Doba běhu: 26.6 ms