Single log file from multiple modules with the same timestamp

I know that there are many other similar questions to mine, but I have not found one that deals specifically with a log file having a timestamp as a part of its name.

I have 4 modules (currently) that I want to implement logging within. I also have a base_logger.py script that I am using to create my logging object. The code is below:

import configparser
import logging
import sys
from datetime import datetime
from pathlib import Path


def write_default_config(path: Path):
    # This function creates a 'config.ini' if it does not already exist
    conf = configparser.ConfigParser()
    conf['logging'] = {'level': 'INFO'}
    with open(path, 'w') as configfile:
        conf.write(configfile)


def setup_config_and_logging():
    # Create a location for the log file to be saved to, and find the config file
    logs_dir_name = "logs"
    config_file_name = "config.ini"
    if getattr(sys, 'frozen', False):  # if running as .exe, save logs to folder in cwd
        script_dir = Path(sys.executable).parent.absolute()
        logs_dir = script_dir / logs_dir_name
        config_file_path = script_dir / config_file_name
    else:  # if running in IDE, save logs to folder at src level
        script_dir = Path(__file__).parent.absolute()
        logs_dir = script_dir.parent.parent / logs_dir_name
        config_file_path = script_dir.parent.parent / config_file_name
    logs_dir.mkdir(parents=True, exist_ok=True)
    if not config_file_path.exists():
        write_default_config(config_file_path)

    # Read the logging level
    config = configparser.ConfigParser()
    config.read(config_file_path)
    logging_level = config.get("logging", "level", fallback="DEBUG")

    # Path for the log file (name dependent on logging level)
    log_file_path = logs_dir / f"app-{logging_level}-{datetime.now().strftime('%Y%m%d-%H%M%S')}.log"

    # Setup logging with the specified level
    level = getattr(logging, logging_level.upper(), logging.DEBUG)  # DEBUG is the default value
    logging.basicConfig(level=level,
                        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                        handlers=[
                            logging.FileHandler(log_file_path),
                            logging.StreamHandler()
                        ])


def get_logger() -> logging.Logger:
    # Provide a logger instance for other modules to use
    return logging.getLogger()

As you can see, the filename for the log files (log_file_path) contains a timestamp for when the log file was created. However, this ends up creating 4 log files as the modules that import it all occur at different times (e.g. app-INFO-20240529-115900.log, app-INFO-20240529-115901.log, …).

If I were to remove the timestamp from the filename, only one file is created and it correctly contains the logged information from all of my modules.

Just to be clear, when importing the logging object into other modules for use, I use this code:

from base_logger import get_logger
log = get_logger()

I have previously tried to define a global variable initialised that would be checked within each import of my base_logger.py module, but this global value would be reset to False each time, hence my check to see whether it was True (i.e. if the logger had already been created) always turned up false, and hence the logger was recreated.

Any advice or solutions would be greatly appreciated!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật