亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 需要將每個腳本記錄到自己的日志中

Python 需要將每個腳本記錄到自己的日志中

繁星coding 2021-11-09 16:24:01
我有腳本 parent.py 和 child.py(許多孩子),我需要為每個腳本創建日志,因此 parent.py 中的任何日志記錄都應該在 parent.log 中,child.py 應該在 child.log 中我在每個腳本中都有以下內容,但我得到空日志......為什么?#main.pyimport childhandler = logging.FileHandler('logs/main.log')handler.setLevel(logging.DEBUG)formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - % (funcName)10s()] %(levelname)s: %(message)s")handler.setFormatter(formatter)logger = logging.getLogger(__name__)logger.addHandler(handler)child.child_func()logger.info('testing parent...')#child.pyhandler = logging.FileHandler('logs/child.log')handler.setLevel(logging.DEBUG)formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - % (funcName)10s()] %(levelname)s: %(message)s")handler.setFormatter(formatter)logger = logging.getLogger(__name__)logger.addHandler(handler)def child_func():    logger.info('testing child...')我需要的是#parent.log{format} testing parent...#child.log{format} testing child...
查看完整描述

2 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

上面的人對記錄器的默認級別是正確的。此外,我發現將日志配置整合到應用程序的早期階段更易于管理,而不是將您的配置分散到任何地方。請參閱下面的示例。


注意:我不希望這被選為答案。我只是想指出我認為組織代碼的更好方法。


主文件

import logging

import child



logger = logging.getLogger(__name__)



def setup_logging():

    main_handler = logging.FileHandler('logs/main.log')

    child_handler = logging.FileHandler('logs/child.log')


    # Note that you can re-use the same formatter for the handlers.

    formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - %(funcName)10s()] %(levelname)s: %(message)s")


    main_handler.setFormatter(formatter)

    child_handler.setFormatter(formatter)


    # By default, loggers inherit the level from their parent, so we only need

    # to set the level on the root logger if you want to have only one knob to

    # control the level.

    root_logger = logging.getLogger()

    root_logger.setLevel(logging.DEBUG)


    main_logger = logging.getLogger(__name__)

    child_logger = logging.getLogger('child')

    child_logger.propagate = False


    main_logger.addHandler(main_handler)

    child_logger.addHandler(child_handler)



def main():

    setup_logging()


    child.child_func()

    logger.info('testing parent...')



if __name__ == '__main__':

    main()

孩子.py

import logging



logger = logging.getLogger(__name__)



def child_func():

    logger.info('testing child...')

設置根記錄器和子記錄器(無主記錄器)

這是一個設置根記錄器以登錄到的示例logs/main.log,以及將子記錄器設置為轉到的示例logs/child.log


def setup_logging():

    root_handler = logging.FileHandler('logs/main.log')

    child_handler = logging.FileHandler('logs/child.log')


    # Note that you can re-use the same formatter for the handlers.

    formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - %(funcName)10s()] %(levelname)s: %(message)s")


    root_handler.setFormatter(formatter)

    child_handler.setFormatter(formatter)


    # By default, loggers inherit the level from their parent, so we only need

    # to set the level on the root logger if you want to have only one knob to

    # control the level.

    root_logger = logging.getLogger()

    root_logger.setLevel(logging.DEBUG)

    root_logger.addHandler(root_handler)


    child_logger = logging.getLogger('child')

    child_logger.propagate = False

    child_logger.addHandler(child_handler)


查看完整回答
反對 回復 2021-11-09
?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

您可以在處理程序和記錄器上設置嚴重性級別 - 我相信記錄器logging.WARNING默認設置為,因此您只能使用代碼獲得警告日志。

import logging

import child


handler = logging.FileHandler('logs/main.log')

formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - %(funcName)10s()] %(levelname)s: %(message)s")

handler.setFormatter(formatter)

logger = logging.getLogger(__name__)

logger.addHandler(handler)

logger.setLevel(logging.DEBUG)       # <-- changed 

child.child_func()

logger.info('testing parent...')

logger.warning('testing parent...')

logger.debug('testing parent...')


#child.py

import logging


handler = logging.FileHandler('logs/child.log')

formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - %(funcName)10s()] %(levelname)s: %(message)s")

handler.setFormatter(formatter)

logger = logging.getLogger(__name__)

logger.addHandler(handler)

logger.setLevel(logging.DEBUG)      # <-- changed

def child_func():

    logger.info('testing child...')

    logger.warning('testing child...')

    logger.debug('testing child...')


查看完整回答
反對 回復 2021-11-09
  • 2 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號