Logging in Python

Logging in Python   Free Tutorial Download

Logging in Python

Learn about the fundamentals of Logging in Python.

If you are just getting started in Python and would like to learn more, take DataCamp’s Introduction to Data Science in Python course.

In general, logging is a way of keeping a log generated from a computer program. A log file usually has a set of records of events that occur in your code ( for example, a python code ) or an operating system. These events or messages are written to a log file, a message, or an event can also be a communication between different users.

It usually depends on the developers on how they would like to generate logs. Maybe they would like to log every operation of an application they developed, log only when an error occurs, or monitor essential operations done by a user and log them. An essential operation could be to check the application or the server was accessed by which end-user or IP address, and in case it was an unsuccessful attempt, the developer then could seek help from the logs and provide better insights than a mere stack trace.

A nice example would be when you try to do an installation on Linux as a sudo user when you are not a sudo user. You see a message “Xyz is not in the sudoers file. This incident will be reported”, which is written in a log file.

The good news is that Python comes with a built-in logging module, and you can use it off the shelf without any installation.

How logging is different from a Print Function?

Logging is similar in spirit to a Print Function. However, a Print function lacks a lot of information that might be useful to a developer. Logging, on the other hand, can log the timestamps and line number at which the error occurred. It can log errors or any information to files, sockets, etc., and it offers you five types of severity based on which you can differentiate your logging.

Five Levels of Logging as shown here, are:

  • Debug (10): Useful for diagnosing issues in the code.
  • Info (20): It can act as an acknowledgment that there are no bugs in the code. One good use-case of Info level logging is the progress of training a machine learning model.
  • Warning (30): Indicative of a problem that could occur in the future. For example, a warning of a module that might be discontinued in the future or low-ram warning.
  • Error (40): A serious bug in the code, could be a syntax error, out of memory error, exceptions.
  • Critical (50): An error due to which the program might stop functioning or might exit abruptly.

Enough of theory, right?

Let’s now understand how you can implement logging in a Python code.

But before that, let’s see the logging attribute table, you would be using some of the below logging attributes in today’s tutorial.

Download  Logging in Python  Free


Password : freetuts.download

Related Articles

Leave a Reply

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

Back to top button