开发者社区> 问答> 正文

python怎么打印日志文件

python怎么打印日志文件

展开
收起
云计算小粉 2018-05-10 20:09:57 1633 0
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
    赞同 展开评论 打赏
问答分类:
问答地址:
相关产品:
问答排行榜
最热
最新

相关电子书

更多
PostgresChina2018_赖思超_PostgreSQL10_hash索引的WAL日志修改版final 立即下载
Kubernetes下日志实时采集、存储与计算实践 立即下载
日志数据采集与分析对接 立即下载