DEV Community

Cover image for logs in python
bluepaperbirds
bluepaperbirds

Posted on

logs in python

You can log in Python with the logger module. A log file can display or store messages while the program is running.

Logging is very different than just relying on Python errors or try-except cases. It is much more broad way to find the bugs in your program.

This is especially useful when your program crashes: it gives you another point of entry to finding the bug. With a large program, you save a lot of time on finding a bug / debugging.

Logs

The example below sends a log message to the console.

import logging
logging.warning('this is a warning message')

If you run the program it shows:

➜ python3 example.py 
WARNING:root:this is a warning message
➜   

For a slightly larger program:

import logging

x = 2
logging.warning('x associated with value')

y = 3
logging.warning('y associated with value')

z = x / y
logging.warning('calculation complete')

Upon running you'll see:

➜  python3 example.py 
WARNING:root:x associated with value
WARNING:root:y associated with value
WARNING:root:calculation complete

If you set y to zero, you'll see where it stops executing the program:

WARNING:root:x associated with value
WARNING:root:y associated with value
Traceback (most recent call last):
  File "example.py", line 9, in <module>
    z = x / y
ZeroDivisionError: integer division or modulo by zero

Once your program is finished, you can hide logging messages but turn them on when needed by changing the severity level. There are different ways to log messages, like saving the output into a file or adding a timestamp.

Related links:

Top comments (0)