How to Quickly Log into A File with Python

1. DEFAULTS

  • start with
import logging
  • the most recognizable/typical feature for the language of logging are 5 severity levels
1. DEBUG
2. INFO
3. WARNING
4. ERROR
5. CRITICAL
  • by default, the logging module logs the messages with a severity level of WARNING or above
  • logger is the feature that does the writing and — importantly — it can/should have a custom name (not here)
  • the default logger is called root
  • the default syntax for the call is
<!-- DEFAULT CALL SYNTAX/EXAMPLE -->
logging.<SEVERITY LEVEL>('<MESSAGE>')
logging.critical('Server DOWN')

<!-- DEFAULT LOG SYNTAX/EXAMPLE -->
<SEVERITY>:<LOGGER>:<MESSAGE>
CRITICAL:root:Server DOWN

2. DEFINE

  • In my case I am setting up the basicConfig in a dedicated module as follows

2.1. FORMAT DETAILS

<!-- FORMAT ATTRIBUTES DEFINED -->
format='%(asctime)s | %(levelname)s | %(message)s'
<!-- LOG EXAMPLE -->
2022-03-10 14:53:37 | INFO | Create: New Doc...

3. CALL

  • You call the defined logger when/where/as many times as you need it
  • The utilization of f-strings — introduced in Python 3.6 — is optimal for message formatting
  • I import the module with from logger import logging and call it as follows with the default call syntax
logging.<SEVERITY LEVEL>('<MESSAGE>')
  • My example including the import of a dedicated logger module (used across my app)
  • kb.log is populated with something like
2022-03-05 08:24:31 | INFO | Create: New Doc | 01.01 test-ignore | pavol kutaj | ZD URL not available

4. SHUTDOWN

  • After the logging is done include
logging.shutdown()
  • I need this because I am logging locally and I push logs to a GitHub repo during the execution.
  • Without that, the file handler is opened and git does not detect the change to logs

5. LINKS

--

--

--

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Learning Algorithms with Python: Insertion Sort

Journey to the Docker side: Introduction to Docker (Part 1)

WSO2 API Cloud: Spring Boot REST API for Active Directory User Validation

This Week In TurtleCoin (June 26, 2019)

Delegate vs. Reassign Differences with Examples

Managing States in Entity-Component-System (aka Finite-State-Machine) - part 1

Why Should You Hire a Ruby on Rails Developer?

Foundational Learning Core Concepts for Oracle Cloud Infrastructure — Part 2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

More from Medium

How I set up a testing environment using pytest

[Python] Decorator

Python Pytest Behaviour Driven Framework — pytest-bdd

Abstract Base Classes ( ABC ) in Python