How to implement same customized log through differents files via logging module

Question:

I believe from other questions I have read that logging has a singleton pattern but I did not succeed to implement it right.

I have a logger.py file like this :

import logging
import os
from concurrent_log_handler import ConcurrentRotatingFileHandler


def create_file_handler(path, filename=None):
    if filename is None:
        filename = f"app_{os.getlogin()}"
    formatter = logging.Formatter('%(asctime)s |  %(name)s | %(funcName)s | %(levelname)s | %(message)s')
    file_handler = ConcurrentRotatingFileHandler(fr'{path}{filename}.log', mode='a', maxBytes=1024*1024*10, # 10 Mo per file
                                 backupCount=10)
    file_handler.setLevel(logging.INFO)
    file_handler.setFormatter(formatter)
    return file_handler

Let’s say to simplify that I have two other files, file_one.py (the file I will call first) which uses functions from file_two.py.

I would like a custom log file from file_one.py, so I have created log like this in it :

import logging
from logger import create_file_handler

file_handler = create_file_handler(r'C:custompath', filename="custom_filename")
le_log = logging.getLogger(__name__)
le_log.setLevel(logging.INFO)

le_log.addHandler(file_handler)

The problem is that in file_two.py I can’t put the custom path neither the custom_file_name in argument of create_file_handler because I don’t know in advance which file (there is not only file_one.py) will call the functions in file_two.py.
So how do I use the customized le_log from file_one.py when using functions from file_two.py ?

Currently I do a default logger creation in file_two.py because I need a le_log variable in the file.

Asked By: TmSmth

||

Answers:

I don’t know if it is the best practice but I finally succeed to do it.
In file_one.py i put :

import logging
from logs.logger import create_file_handler

file_handler = create_file_handler(r'C:custompath', filename="custom_filename")
logging.basicConfig(handlers=[file_handler])
le_log = logging.getLogger(__name__)
le_log.setLevel(logging.INFO)

I used the customized handler in the .basicConfig() method of logging instead of adding to le_log with .addHandler(). I guess it allows the logging class to spread through others files called in file_one.py. Then in file_two.py I put :

import logging
le_log = logging.getLogger(__name__)
le_log.setLevel(logging.INFO)
Answered By: TmSmth