在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python  Linux  網(wǎng)絡(luò)安全/ shell 腳本中如何獲取 python logging 打印的信息?

shell 腳本中如何獲取 python logging 打印的信息?

在 shell 腳本中調(diào)用 python 的接口,但是 python API 是通過 python 的 logging 把相關(guān)信息打印到屏幕上的,不知道這種情況在 shell 中怎么獲取這些 logging 信息,請指教。
代碼中是這么寫的:

import logging

......

logger = logging.getLogger("123")

......

def debug(msg):
    logger.setLevel(logging.DEBUG)
    debughandler = logging.StreamHandler()
    debughandler.setLevel(logging.DEBUG)
    fmt = '%(asctime)s - %(levelname)s - %(message)s'
    formatter = logging.Formatter(fmt)
    debughandler.setFormatter(formatter)
    logger.addHandler(debughandler)
    logger.debug(msg)
    logger.removeHandler(debughandler)

通過 logger.debug(msg) 打印的信息怎么在 shell 中捕捉到?

回答
編輯回答
臭榴蓮

思路:
logger模塊里面有寫入文件的方法,將log的內(nèi)容寫入一個文件,然后shell腳本讀取log文件即可。

以下摘自: https://cuiqingcai.com/6080.html

import logging
 
logging.basicConfig(level=logging.DEBUG,
                    filename='output.log',
                    datefmt='%Y/%m/%d %H:%M:%S',
                    format='%(asctime)s - %(name)s - %(levelname)s - %(lineno)d - %(module)s - %(message)s')
logger = logging.getLogger(__name__)
 
logger.info('This is a log info')
logger.debug('Debugging')
logger.warning('Warning exists')
logger.info('Finish')

這里我們指定了輸出文件的名稱為 output.log,另外指定了日期的輸出格式,其中年月日的格式變成了 %Y/%m/%d,另外輸出的 format 格式增加了 lineno、module 這兩個信息,運行之后便會生成一個 output.log 的文件

2018年8月19日 03:14