Loguru setup for use in bash script for use with several Python scripts

407 Views Asked by At

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!

1

There are 1 best solutions below

0
Davide Madrisan On

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.")