Python Tutorial: Logging Basics – Logging to Files, Setting Levels, and Formatting



In this Python Tutorial, we will be going over the basics of logging. We will learn how to switch out our print statements for logs, change the logging level, add logs to files, and also how to change the format of those logs. Let’s get started.

Advanced Logging Video –
Log Record Attribute Docs:

The code from this video can be found…

, , , , , , , ,

43 thoughts on “Python Tutorial: Logging Basics – Logging to Files, Setting Levels, and Formatting

  1. I'm trying to get this to work on python 3.6 and I'm getting errors…
    import logging

    logging.warning('Watch')
    …throws the error
    AttributeError: module 'logging' has no attribute 'warning'

    What is going on here? I'm using the python build environment. I also found that after importing logging, my print statements were printing to my screen twice. They printed only once after I commented the import logging statement. Does anyone have this issue?

  2. Thanks for the video! My problem is I tried this on Jupyter Notebook. One curious thing happened: after loading my notebook, logging.basicConfig(level=logging.DEBUG) works well for the first calling. Then if I call it again with another setting (level=logging.WARNING), it doesn´t work!

  3. Hello,
    I created the logs as you wrote but there is not any response in jupyter notebook or in commandline. Nothing is printed even if I put logging.warning() or create log file and set the level to logging.DEBUG with basicConfig. Does anybody know how I can fix this?

  4. Impossible to get the code working ! (even if I copy/paste from github !

    [ha@pc prog_python]$ python3 log-sample.py
    Traceback (most recent call last):
    File "log-sample.py", line 1, in <module>
    import logging
    File "/home/ha/Documents/prog_python/logging.py", line 22, in <module>
    logging.warning ('{} + {} = {}'.format(a, b, add_result))
    AttributeError: module 'logging' has no attribute 'warning'

Leave a Reply

Your email address will not be published. Required fields are marked *