search

Home  >  Q&A  >  body text

python的logging无法输出到文件?

logging配置如下:

logging.basicConfig(level=logging.DEBUG,\

format="%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s",\
datefmt="%a, %d %b %Y %H:%M:%S",\
filename="i2_insert_equipments.log",\
filemode="w")

但当在代码中使用logging.info的时候,目录下无日志文件生成。但当在控制台中运行的时候,是可以正常运行的,有知道这种现象如何解决的么? 望告知一下,谢谢。

迷茫迷茫2803 days ago880

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-04-18 10:09:05

    If you don’t know how to use it, I’ll give you an example

    #! /usr/bin/env python
    
    import sys
    import logging
    
    def add_log_file(infile=None):
        logger = logging.getLogger()
        if infile is not None:
            handler = logging.FileHandler(infile)
        else:
            handler = logging.StreamHandler()
        logger.handlers.append(handler)
        return
    
    def main():
        for c in sys.argv[1:]:
            add_log_file(c)
        for c in sys.argv[1:]:
            logging.error('%s'%(c))
    
    
    main()

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:09:05

    The problem with filename is that you specify an absolute path and then try again, filemode="w". If you do this, a new file will be generated every time,

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:09:05

    The environment under the interactive command line is different, especially for certain operations involving files and systems. For example, you can try creating a new process under the interactive command line.

    reply
    0
  • Cancelreply