I have this Python code in main.py:
parser = argparse.ArgumentParser()
parser.add_argument("subname", help="Name of subreddit")
args = parser.parse_args()
Then in another file speciallogger.py:
import logging
import time
from logging.handlers import TimedRotatingFileHandler
path='logs/log_SUBNAMEHERE_logger.txt'
logmsg = logging.getLogger("Rotating Log")
fmt = u'%(asctime)s\t%(levelname)s\t%(filename)s:%(lineno)d\t%(message)s'
logmsg.setLevel(logging.INFO)
handler = TimedRotatingFileHandler(path, when="d", interval=1, backupCount=14)
handler.setFormatter(logging.Formatter(fmt))
logmsg.addHandler(handler)
The script (starts with main.py) executes via a cron using an argument.
I need to pass that argument to speciallogger.py so I can name the filename what subname is, along with ensuring logmsg can be used across all modules without the log file being recreated each import during a script using multiple modules.
How can I do this?