Skip to content
>GLB_
Go back

Logging in a file to avoid print statements

A video that was enlightening

We need to avoid the misuse of the print statements once we master the basic tools and ideas about programming in particular and software developer in general. So, here there are some notes about loggers.

Loggers have five levels, by critically:

  1. Critical
  2. Error
  3. Warning
  4. Info
  5. Debug

By default we typically set info and above. This information, and other I’m going to write down comes from the following video:

https://www.youtube.com/watch?v=-7XO15S2HQE&ab\_channel=JoeFreeman

This is interesting because besides the fact that he encourages us to don’t use print statements. Something that it’s really difficult to eradicate from our, or at least my practices, he shows us a interesting way to log the information: With a logging config in a YAML file. You can read it directly on the Joe Freeman’s post entrance: Log! Don’t Print! Use the Python logging library

What the python documentation say about logging?

If you dig into the Python documentation you can see another interesting things besides the ones I talked here: Logger In python – First Approach

If you want to:

You can write the logs in a file:

import logginglogging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)logging.warning("Ey, this is a logging warning")logging.error("Ey, this is a logging error")logging.debug('This message should go to the log file')logging.info('So should this')logging.warning('And this, too')logging.error('And non-ASCII stuff, too, like Øresund and Malmö')

I’m following the Python’s documentation: Logging to a File


Share this post:

Previous Post
Basic concepts about Amazon Redshift
Next Post
Peter Norvig and the idea of test to drive design