I've a generic file log.py
in which i've imported python logging and used all functions in it.
now i need to format log such that
(current_script_name) [levelname] message
log.py
import logging
import inspect
import sys
class log(object):
#file_name = sys.argv[0] #this will give b.py instead of a.py
file_name = str(inspect.getfile(inspect.currentframe())).split("/")[-1]
logging.basicConfig(level=logging.INFO, format="("+file_name+") [%(levelname)s] %(message)s")
def __init__(self):
pass
def info(msg):
logging.info(msg)
def error(msg):
logging.error(msg)
def warning(msg):
logging.warning(msg)
def debug(msg):
logging.debug(msg)
Now, i've another file called a.py
where i have imported above file and used like
import log
def some():
log.info("Hello this is information")
This will give an output below when i call some()
in b.py
(log.py) [INFO] Hello this is information
but i expect below output because log.info()
code is used in a.py
(a.py) [INFO] Hello this is information
Note: I shouldn't pass any argument for
log.info(msg)
line ina.py