Skip to content
Advertisement

Problem with Logging Module in Google Colab

I have a python script with an error handling using the logging module. Although this python script works when imported to google colab, it doesn’t log the errors in the log file.

As an experiment, I tried this following script in google colab just to see if it writes log at all

import logging
logging.basicConfig(filename="log_file_test.log",
                            filemode='a',
                            format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
                            datefmt='%H:%M:%S',
                            level=logging.DEBUG)

logging.info("This is a test log ..")

To my dismay, it didn’t even create a log file named log_file_test.log. I tried running the same script locally and it did produce a file log_file_test.log with the following text

13:20:53,441 root INFO This is a test log ..

What is it that I am missing here? For the time being, I am replacing the error logs with print statements, but I assume that there must be a workaround to this.

Advertisement

Answer

Perhaps you’ve reconfigured your environment somehow? (Try Runtime menu -> Reset all runtimes…) Your snippets works exactly as written for me —

enter image description here

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement