开发者社区 问答 正文

python怎么打印日志文件

python怎么打印日志文件

展开
收起
云计算小粉 2018-05-10 20:09:57 1788 分享 版权
1 条回答
写回答
取消 提交回答
  • 阿里云ping https://ping.gaomeluo.com/aliyun/

    一. 利用sys.stdout将print行导向到你定义的日志文件中,例如:

    import sys

    make a copy of original stdout route

    stdout_backup = sys.stdout

    define the log file that receives your log info

    log_file = open("message.log", "w")

    redirect print output to log file

    sys.stdout = log_file

    print "Now all print info will be written to message.log"

    any command line that you will execute

    ...

    log_file.close()

    restore the output to initial pattern

    sys.stdout = stdout_backup

    print "Now this will be presented on screen"

    2019-07-17 22:19:51
    赞同 展开评论