代码之家  ›  专栏  ›  技术社区  ›  Bug Raptor

python日志:当脚本从/etc/rc.local自动启动时,不记录调试级日志。

  •  0
  • Bug Raptor  · 技术社区  · 6 年前

    在我的Raspberry Pi(RasBibe)上,下面的代码(Test.Py)在从控制台启动的日志文件'/home /pI/Test/Test.log中记录日志中的调试信息和信息日志消息,但在启动时(由root用户)从/ETC/RC本地文件中记录信息日志消息。

    import logging
    
    logging.basicConfig(filename='/home/pi/test/test.log',level=logging.DEBUG,\
          format='%(asctime)s -- %(module)s -- %(levelname)s -- %(message)s')
    
    logging.debug('Debug error')
    logging.info('INFO ERROR')
    

    为什么?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Sunitha    6 年前

    指定的代码块应该可以正常工作,调试消息应该转到指定的文件

    但是记住你可以 logging.basicConfig Python进程只有一次。如果你已经做了 日志.basicconfig 从另一个模块,或者如果您以其他方式配置了根记录器,那么您的配置将不会有任何效果

    这是一个来自 help(logging.basicConfig)

    基本配置(**Kwargs) 对日志系统进行基本配置。

    This function does nothing if the root logger already has handlers
    configured. It is a convenience method intended for use by simple scripts
    to do one-shot configuration of the logging package.