I have a program which runs a series of Python scripts through a Bash script.
I am using loguru within the Python scripts, and I set up log files via a separate logger_setup.py
.
This works okay, but I need to implement the setup in each Python script via exec
, which has become cumbersome for refactoring, for example when the Python scripts are moved to a subfolder.
Is there a way to run the logger setup once, within the bash script, and to be recognised by all subsequent calls to Python?
Minimum Working Example
my_bash_script.sh
:
#!/bin/bash
rm alt_logger.log
# python logger_setup.py # <-- this is the desired step, but does not work
python my_python_script.py
python another_python_script.py
my_python_script.py
:
from loguru import logger
exec(open("logger_setup.py").read()) # <-- I want to remove this
logger.info(f"Here I am writing to log.")
another_python_script.py
:
from loguru import logger
exec(open("logger_setup.py").read()) # <-- I want to remove this
logger.info(f"Another message to log.")
logger_setup.py
:
from loguru import logger
if __name__ == "__main__":
log_file = "alt_logger.log"
print(f"Setting up logger to stream to {log_file}")
logger.remove()
logger.add(
log_file,
colorize=True,
format="message}",
level="INFO",
)
Thanks for any help!