Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 963
Afsan Abdulali Gujarati Avatar asked Feb 21 '26 08:02

Afsan Abdulali Gujarati


1 Answers

logging.basicConfig can be run just once*

Any subsequent call to basicConfig is ignored.

* unless you are in Python 3.8 and use the flag force=True

logging.basicConfig(filename='app.log',
                    level=logging.DEBUG,
                    force=True, # Resets any previous configuration
                    )

Workarounds (2)

(1) You can easily reset the Colab workspace with this command

exit

Wait for it to come back and try your commands again.

(2) But, if you plan to do the reset more than once and/or are learning to use logging, maybe it is better to use %%python magic to run the entire cell in a subprocess. See photo below.

enter image description here


What is it that I am missing here?

Deeper understanding of how logging works. It is a bit tricky, but there are many good webs explaining the gotchas.

  • In Colab
  • https://realpython.com/python-logging
like image 88
Rub Avatar answered Feb 24 '26 16:02

Rub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!