Want to get the origin where function is called, logger for Django as module

Viewed 43

I am trying to set the logger for the Django project.

This is my logger.py

import logging
import inspect
import os


class Trace():

    @classmethod
    def execution_location(self):
        frame = inspect.currentframe().f_back
        return "{}:{} {}".format(os.path.basename(frame.f_code.co_filename), frame.f_lineno, frame.f_code.co_name)
class Logger():
    def __init__(self):
        self.logger = logging.basicConfig(level=logging.DEBUG, format="[%(asctime)s] [%(process)d] [%(name)s] [%(levelname)s] %(message)s")

    def debug(self,log_message):
        execution_location = Trace.execution_location()
        self.logger = logging.getLogger(execution_location)
        self.logger.debug(log_message)

    def info(self,log_message):
        execution_location = Trace.execution_location()   
        self.logger = logging.getLogger(execution_location)
        self.logger.info(log_message)

    def warning(self,log_message):
        execution_location = Trace.execution_location()
        self.logger = logging.getLogger(execution_location)
        self.logger.warning(log_message)

    def error(self,log_message):
        execution_location = Trace.execution_location()
        self.logger = logging.getLogger(execution_location)
        self.logger.error(log_message)

Normally I use like this from another script

from myproj.helpers.MyLogger import Logger 
_logger = Logger()
_logger.debug("test logger")

However in this case

[2022-02-03 07:01:30,195] [55587] [MyLogger.py:30 info] [INFO] test logger

Everything is shown as MyLogger.py:30

However I want to show the place where this function is called.

Is it possible? , How can I do this?

0 Answers
Related