Python需要将每个脚本记录到自己的日志中

时间:2019-02-15 00:11:53

标签: python python-2.7

我有脚本parent.py和child.py(许多子项),每个脚本都需要有日志,因此parent.py中的所有日志记录都应位于parent.log中,而child.py中的记录应位于child.log中< / p>

我在每个脚本中都有以下内容,但是我得到了空日志...为什么?

var json = [
  "1":{
    {"0":"1863707152859270"},
    {"1":"Exercise to lose weight"},
    {"2":"289169006"},
    {"3":"Reduce 20 pounds"},
    {"4":"Walk daily for one hour. Have a light dinner."},
    {"5":"5/10/2013 12:00:00 AM"},
    {"6":"1/21/2019 4:25:52 PM"},
    {"7":"Y"},
    {"8":"Frank the Tank"},
    {"9":"1/22/2019 8:50:02 AM"},
    {"10":"1/22/2019 8:50:02 AM"},
    {"11":"Frank the Tank"},
    {"12":"Abnormal LGSIL"},
    {"13":"1848065703239670"},
    {"14":"1863707006859070"},
  },
  "2":{
    {"0":"1863707152859270"},
    {"1":"Exercise to lose weight"},
    {"2":"289169006"},
    {"3":"Reduce 20 pounds"},
    {"4":"Walk daily for one hour. Have a light dinner."},
    {"5":"5/10/2013 12:00:00 AM"},
    {"6":"1/21/2019 4:25:52 PM"},
    {"7":"Y"},
    {"8":"Frank the Tank"},
    {"9":"1/22/2019 8:50:02 AM"},
    {"10":"1/22/2019 8:50:02 AM"},
    {"11":"Frank the Tank"},
    {"12":"Abnormal LGSIL"},
    {"13":"1848065703239670"},
    {"14":"1863707006859070"},
  }
];

我需要拥有的是

#main.py
import child

handler = 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.py

handler = 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...')

2 个答案:

答案 0 :(得分:3)

以上人员对记录器的默认级别是正确的。而且,与其将您的配置分散到各处,我还发现将日志记录配置整合到应用程序的早期阶段是更易于管理的。请参见下面的示例。

注意:我不希望将其选作答案。我只想指出我认为是组织代码的更好方法。

main.py

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()

child.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)

答案 1 :(得分:1)

您可以在处理程序和记录器上都设置严重性级别-我相信记录器默认情况下设置为logging.WARNING,因此您只会使用代码获得警告日志。

您可以在此线程中阅读更多内容:What is the point of setLevel in a python logging handler?

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...')