python的logging无法输出到文件?
迷茫
迷茫 2017-04-18 10:07:05
0
3
870

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的时候,目录下无日志文件生成。但当在控制台中运行的时候,是可以正常运行的,有知道这种现象如何解决的么? 望告知一下,谢谢。

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(3)
小葫芦

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

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,

伊谢尔伦

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template