Python logging format date

Basic to Advanced Logging with Python in 10 Minutes

Logging crash course with common logging issues addressed

In a Python project, it is easy to track the code progress and debug using print statements and stack trace. However, logging provides additional features of tracking and debugging with timestamps, file name, the line number of code, and more, differentiating between different types of print statements, and even the option to save print statements to a log file or other locations rather than only viewing it on a console or command line!

This article will introduce the components of logging, how to perform logging within a file, and how to use configuration to perform logging across multiple files in a reusable and scalable manner.

Update: This article is part of a series. Check out other “in 10 Minutes” topics here!

Table of Contents

Components of Logging

Before diving into the code implementation, a few components are integral to using logging effectively.

Log File — Where to Log?

It is advisable to save logs to a log file rather than viewing them on the console or command line as the information disappears once the console or terminal closes. We can specify the relative path to the project where we want to store our logs.

You can also define the save mode. By default, logs are saved on append mode but it can be changed to write mode to overwrite previous logs.

Читайте также:  Can you convert pdf to html

Log Levels — When to Log?

Depending on the task and severity of events tracked, there are 5 levels of logging. In increasing order of severity,

  • DEBUG : Logs detailed information, used when diagnosing problems i.e., fault investigation
  • INFO : Logs detailed information, used during normal…

Источник

Оцените статью