G. Macia
G. Macia

Reputation: 1521

Python Logging module not working in Google Colab

I will start by saying the I found a similar question and none of the suggested solutions worked for me at the end of 2022.

I do not manage to do simple things like changing the severity level of the logging on Colab:

enter image description here

Or writing the logs to a file:

import logging

logging.basicConfig(filename='app.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s', force=True)
logging.warning('This will get logged to a file')

As Colab is very popular and so it is the logging module, I thought it makes sense to ask again.

Upvotes: 1

Views: 1636

Answers (1)

AlexK
AlexK

Reputation: 3011

For the first problem, the debug message does not print because the lowest severity of the root logger is set to WARNING. You can change it to DEBUG with

import logging

logging.getLogger('RootLogger').setLevel(logging.DEBUG)

For the second problem, as Colab is still on Python 3.7 and not 3.8, the force= parameter does not work yet. So you just need to manually delete the default StreamHandler that sends logging output to sys.stderr before carrying out your configuration. (code source: https://stackoverflow.com/a/49202811/9987623)

import logging

# Check current handlers
print(logging.root.handlers)
# [<StreamHandler stderr (NOTSET)>]

# Remove all handlers associated with the root logger object.
for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)
print(logging.root.handlers)
# []

Now, you can run your code. Here is an example:

from pathlib import Path

fmt = '%(name)s - %(levelname)s - %(message)s'
log_dir = Path.cwd().joinpath("logs")
path = Path(log_dir)
path.mkdir(exist_ok=True)
log_fname = "app.log"
log_path = log_dir.joinpath(log_fname)

logging.basicConfig(
    level=logging.DEBUG,
    filemode="w",
    format=fmt,
    filename=log_path,
)
logging.warning("this is a warning")

Next cell:

!cat ./logs/app.log
# root - WARNING - this is a warning

Next cell:

logging.debug('this is a debug message')

Next cell:

!cat ./logs/app.log
# root - WARNING - this is a warning
# root - DEBUG - this is a debug message

Upvotes: 3

Related Questions