簡體   English   中英

如何將日志消息從外部模塊打印到Python主模塊的終端窗口?

[英]How to print log messages from external modules to the main Python module's terminal window?

我正在編寫一個Python命令行程序。

有一個主要的Python腳本文件,作為入口點。 當用戶運行此腳本時,它將執行一些外部Python腳本文件。 外部Python腳本文件也可以執行其他外部Python腳本文件。 外部文件的數量是可變的。

Python腳本將使用以下命令執行外部Python腳本:

p = subprocess.Popen(args)

or

p = subprocess.call(args)

當我在終端窗口中運行主Python腳本時,它將在運行時在屏幕上打印實時日志消息。 現在,我想從主Python腳本調用的所有外部Python腳本中獲取所有日志消息,並將其打印到同一終端窗口(用於運行主腳本的終端窗口)上。

例如,以下是腳本執行的順序:

1.Main-script
    |
    2.Layer-1-script-1
        |
        3.Layer-2-script-1
        |
        4.Layer-2-script-2
        |
    5.Layer-1-script-2
    |
    6.Layer-1-script-3
    |
7.Main-script(continued)

在終端窗口中運行主腳本時,是否可以在終端窗口中獲​​取實時日志消息,如下所示?

[time-hh-mm-ss][log message from main script]Script is running..
[time-hh-mm-ss][log message from main script]Calling script layer-1-script-1..
[time-hh-mm-ss][log message from layer-1-script-1]Script is running..
[time-hh-mm-ss][log message from layer-1-script-1]Calling script layer-2-script-1..
[time-hh-mm-ss][log message from layer-2-script-1]Script is running..
[time-hh-mm-ss][log message from layer-2-script-1]Calling script layer-2-script-2..
[time-hh-mm-ss][log message from layer-2-script-2]Script is running..
[time-hh-mm-ss][log message from layer-2-script-2]Calling script layer-1-script-2..
[time-hh-mm-ss][log message from layer-1-script-2]Script is running..
[time-hh-mm-ss][log message from layer-1-script-2]Calling script layer-1-script-3..
[time-hh-mm-ss][log message from layer-2-script-3]Script is running..
[time-hh-mm-ss][log message from main script]Back to main script. Script is running..

是否有可能在終端窗口中獲​​得如上所述的real time log messages

如果您依賴於subprocess模塊,則只能通過subprocess.PIPE通信,以便從subprocess.PIPE中收集標准輸出和標准錯誤。 因此,使子進程將其日志記錄輸出寫入stdout / err並通過管道將子進程連接到父進程。 這樣,您基本上可以將子流程的標准輸出打印到父流程的標准輸出。

如果您可以控制所有Python腳本,則可以使用multiprocessing而非subprocess來執行以下操作:

test.py

import logging
import test2
import multiprocessing as mp

logger = mp.get_logger()

def main():
    logger.info('Script is running')
    logger.info('Calling script test2')
    proc = mp.Process(target = test2.main)
    proc.start()
    proc.join()

if __name__ == '__main__':
    formatter = logging.Formatter('[%(asctime)s] [%(filename)s]: %(message)s',
                                  datefmt = '%H:%M:%S')
    handler = logging.StreamHandler()
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(logging.INFO)    
    main()

test2.py:

import multiprocessing as mp

logger = mp.get_logger()

def main():
    logger.info('Script is running...')

運行test.py產生

[11:36:50] [test.py]: Script is running
[11:36:50] [test.py]: Calling script test2
[11:36:50] [util.py]: child process calling self.run()
[11:36:50] [test2.py]: Script is running...
[11:36:50] [util.py]: process shutting down
[11:36:50] [util.py]: process exiting with exitcode 0
[11:36:50] [util.py]: process shutting down

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM