2

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!

Brian
  • 115
  • 8
  • 1
    Your plan won't work as logger_setup.py modifies python variables. In your bash script you'd have to transfer these variables between the individual python calls. The next better (but still horrible) alternative would be to automatically run the setup for each python call. But why do you use bash at all? Couldn't you write a single python script that executes all the other python scripts? – Socowi Sep 22 '21 at 21:33
  • Thanks. There are other steps in the program for which I think it makes sense to use a bash script. Out of interest, wouldn't a unified python script require using `exec` throughout, which I thought was discouraged? – Brian Sep 23 '21 at 13:30

1 Answers1

2

As already stated in the comments you cannot use bash for this purpose (ok you can maybe source the setup file and merge it with a script to a temporary file that will be executed, but this would be a weird solution in my opinion).

I think the only way to reuse the code for logging setup, is treating logger_setup.py as a library:

#!/usr/bin/env python3

from loguru import logger

def loguru_setup(logfile = "alt_logger.log"):
    logger.remove()
    logger.add(
        logfile,
        colorize=True,
        format="{message}",
        level="INFO",
    )

    return logger

and loading this library in each script:

my_python_script.py:

#!/usr/bin/env python3

from logger_setup import loguru_setup

logger = loguru_setup()
logger.info(f"Here I am writing to log.")
Davide Madrisan
  • 1,969
  • 2
  • 14
  • 22