• Latest
  • Trending
Python: Logging from multiple modules

Python: Logging from multiple modules

May 25, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Thursday, 16 July, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

Python: Logging from multiple modules

by ITECHNEWS
May 25, 2022
in Data Science, Leading Stories
0 0
0
Python: Logging from multiple modules

Hi, In one of my tasks at work I was designing a system where there would be one main module of my application and the application had several modules all of which did their part to make the application record success or failure.

However, at the time I did not know how to have python log the generated messages from multiple modules to the same logger object that was initiated in the main module. Hence this post.

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

Let us say your application looks like the below

The main application file should look as below:

import logging
#Function to initialize the logger, notice that it takes 2 arguments
#logger_name and logfile
def setup_logger(logger_name,logfile):
                logger = logging.getLogger(logger_name)
                logger.setLevel(logging.INFO)
                # create file handler which logs even debug messages
                fh = logging.FileHandler(logfile)
                fh.setLevel(logging.INFO)
                # create console handler with a higher log level
                ch = logging.StreamHandler()
                ch.setLevel(logging.INFO)
                # create formatter and add it to the handlers
                formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
                fh.setFormatter(formatter)
                ch.setFormatter(formatter)
                # add the handlers to the logger
                logger.addHandler(fh)
                logger.addHandler(ch)
                return logger
#setup the logger as below with mylogger being the name of the #logger and myloggerfile.log being the name of the logfile
mylogger=setup_logger('mylogger','myloggerfile.log')
mylogger.info("My Logger has been initialized")
##########Very important to import the module after the logger has #been created, if the module is imported before setting up the #logger the module wont log to the desired file
import mymodule
#Then you can continue the rest of the code.

The module file should look like below:

import logging
hostlogger = logging.getLogger('mylogger.module')
hostlogger.info("Logger has been initialised in the host module")

This should help you get logs from the modules of your applications into a single common file as below:

# cat myloggerfile.log
2022-05-05 17:06:10,024 - mylogger - INFO - My Logger has been initialized
2022-05-05 17:06:10,028 - mylogger.module - INFO - Logger has been initialised in the host module

Notice the name of the logger object, the message with the tag mylogger is coming from you main application and the message mylogger.module is coming from the module

This can also be tweaked to log to multiple files and have multiple logger objects created in the main application file and the modules can refer to them accordingly.

Source: SatyajeetD
Tags: Python: Logging from multiple modules
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version