Question

The following dev enviroment is to be considered.

Small number of python modules, each contanining one or more classes. A main module, that calls those classes. A custom logging module called logger with a class called Logger. Assume that I call the main execution class with a logging level of debug. How may I make this sufficient to be that log level inherited to every other call including the rest of the classes, methods in those classes, functions in the main module and so forth...

The Logger objects are called like log=Logger(0) for example (logging level is an int, to use the same syntax that we use in other scripts (shell scripts, not python).

My final picture is to have the code filled with log.log_debug('debug message') and log.log_error('error message') but only actually printing a message when the right log_level is choosen. And if possible just one

from logger import Logger

call within the main module.

Thanks for your time.

==================================

Edit

In the main execution module there is a main() function in which a parser.parse_args() object is returned with an argument --log_level to globally define (at least is my intention) the log_level. There is a default log_level handling (i.e, it is always defined)

I will try to mock a minimum example

import argparse
from logfile from Logfile
from logger import Logger


def argument_parser():

    parser=argparse.ArgumentParser()

    stuff

    return parser.parse_args()

def log_file_stuff():

    log_file=Logfile()
    log_file.methods() [*]

def main():

    args=argument_parser()
    # Here log_level is defined with args.log_level
    global log
    log=log(log_level)
    log_file_stuff()

main()

[*] One of those methods may call a Logger object, which I want to be exactly the same as the one defined in the main() function the question is how may I achieve this? without a log return and argument waterfall

Was it helpful?

Solution

Well to answer my own question... I actually implemented a cross module variable dictionary to use it as settings. Those settings have the log_level variable son only with

from main import settings
from logger import Logger

log=Logger(settings['log_level'])

then I have my custom logging class with the user input log_level. Of course settings is constructed with argparse, mainly as vars(args) following the notation in the question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top