简体   繁体   中英

Where should I execute logging.basicConfig()?

I am using an application which can load up a python interpreter and run any python script. I'm creating processes in this application which run scripts to control an external piece of hardware. The hardware is listening for commands in the form of text over TCP/IP on a specific address and port, and sends responses over that same port.

I have SocketInterface.py which deals with encoding/decoding the bytes and takes care of all the socket stuff, so that other modules can just call SocketInterface.send() , .receive() , etc. One such module is a command-line-ish gui thing that just runs send() and receive() in a loop, reading commands from a text box and displaying the response in another.

Another module I'm working on is going to involve some wrapper functions that automatically construct the command in the proper format, send it, and wait to receive the appropriate response from the hardware. I'd like to incorporate logging into this. INFO level statements for the commands I sent and responses I received, DEBUG level statements for some of the TCP/IP stuff, etc.

Ultimately, I expect I will have multiple "scripts", one for each workflow, but I'd like all of them to write to the same log, say over the course of one day.

From my command wrapper module:

    def __init__(self):
        self.si = SocketInterface()
        self.logger = logging.getLogger(__name__)

        try:
            os.mkdir('logs')
        except FileExistsError:
            pass

        logging.basicConfig(
            filename='logs/{}.log'.format(time.strftime('%Y-%m-%d')),
            filemode='a', level=logging.DEBUG,
            format='%(asctime)s::%(name)s::%(levelname)s::%(message)s'
        )

With the way basicConfig() works, this code can execute as many times as it wants (although I'd maybe like to avoid having os.mkdir() try to run a bunch of times. I just don't really know where to put this code to make sure it actually does execute, without pasting it everywhere.

Is there a neater solution than putting it at the start of main() in every script I write?

You could put this after your imports in your __main__ file. It will execute as soon as the interpreter hits it. You can then grab your logger at the start of each file. If you are importing the file as a module it will be run on import, and if you are running it directly it will still run first. This is because the interpreter runs down each file looking for instructions and executing them in order. So it will import, run the config, get the logger, and then run your main.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM