简体   繁体   English

如何将日志消息从外部模块打印到Python主模块的终端窗口?

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

I am writing a Python command line program. 我正在编写一个Python命令行程序。

There is a main Python script file, acting as the entry point. 有一个主要的Python脚本文件,作为入口点。 When user run this script, it will execute a few external Python script files. 当用户运行此脚本时,它将执行一些外部Python脚本文件。 The external Python script files may also execute other external Python script files. 外部Python脚本文件也可以执行其他外部Python脚本文件。 The number of external files is variable. 外部文件的数量是可变的。

The Python script will execute external Python scripts using: Python脚本将使用以下命令执行外部Python脚本:

p = subprocess.Popen(args)

or

p = subprocess.call(args)

When I run the main Python script in a terminal window, it will print real time log messages on the screen when it is running. 当我在终端窗口中运行主Python脚本时,它将在运行时在屏幕上打印实时日志消息。 Now, I would like to get all log messages from all external Python scripts called by the main Python script and print it onto the same terminal window (the terminal window that I use to run the main script). 现在,我想从主Python脚本调用的所有外部Python脚本中获取所有日志消息,并将其打印到同一终端窗口(用于运行主脚本的终端窗口)上。

For example, below are the sequence of script execution: 例如,以下是脚本执行的顺序:

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)

When I run the main script in a terminal window, is it possible that I can get real time log messages on my terminal window like below? 在终端窗口中运行主脚本时,是否可以在终端窗口中获​​取实时日志消息,如下所示?

[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..

Is it possible that I can get a real time log messages like above in the terminal window? 是否有可能在终端窗口中获​​得如上所述的real time log messages

If you rely on the subprocess module, then you can only communicate via subprocess.PIPE s in order to collect the standard output and standard error from the subprocesses. 如果您依赖于subprocess模块,则只能通过subprocess.PIPE通信,以便从subprocess.PIPE中收集标准输出和标准错误。 Hence, make the subprocesses write their logging output to stdout/err and connect the subprocesses via pipes to the parent process. 因此,使子进程将其日志记录输出写入stdout / err并通过管道将子进程连接到父进程。 By doing so you can basically make the subprocess' stdout being printed to the parent process' stdout. 这样,您基本上可以将子流程的标准输出打印到父流程的标准输出。

If you have control over all the Python scripts, then you could use multiprocessing instead of subprocess with which you could do something like this: 如果您可以控制所有Python脚本,则可以使用multiprocessing而非subprocess来执行以下操作:

test.py : 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: test2.py:

import multiprocessing as mp

logger = mp.get_logger()

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

Running test.py yields 运行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