Python如何将控制台输出另存为日志文件

 更新时间:2023年05月08日 11:00:02   作者:渣渣的夏天  
这篇文章主要介绍了Python如何将控制台输出另存为日志文件问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Python将控制台输出另存为日志文件

需求  

在 PyCharm 中或者说运行 python 程序时会使用 print 输出些过程信息、 traceback 异常信息 到控制台,但是程序运行结束后记录就没有了,所以想着每次运行将信息显示在控制台的同时记录到文件中。

方法一:使用 Logger 类(推荐)

自定义创建 Logger 类,结合 sys 进行记录控制台输出信息

demo.py

import sys
import os
import time
# 控制台输出记录到文件
class Logger(object):
    def __init__(self, file_name="Default.log", stream=sys.stdout):
        self.terminal = stream
        self.log = open(file_name, "a")
    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)
    def flush(self):
        pass
if __name__ == '__main__':
    # 自定义目录存放日志文件
    log_path = './Logs/'
    if not os.path.exists(log_path):
        os.makedirs(log_path)
    # 日志文件名按照程序运行时间设置
    log_file_name = log_path + 'log-' + time.strftime("%Y%m%d-%H%M%S", time.localtime()) + '.log'
    # 记录正常的 print 信息
    sys.stdout = Logger(log_file_name)
    # 记录 traceback 异常信息
    sys.stderr = Logger(log_file_name)
    print(5555)
    print(2/0)

./Logs/log-20210103-140231.log

5555
Traceback (most recent call last):
  File "G:\Codes\demo.py", line 33, in <module>
    print(2/0)
ZeroDivisionError: division by zero

方法二:仅使用 sys

将所有输出全部直接保存到文件中,不再显示到控制台

demo.py

import sys
log_print = open('Defalust.log', 'w')
sys.stdout = log_print
sys.stderr = log_print
if __name__ == '__main__':
    print(555)
    print(2/0)

Default.log

555
Traceback (most recent call last):
  File "G:\Codes\demo.py", line 9, in <module>
    print(2/0)
ZeroDivisionError: division by zero

方法三:使用 logging 模块

功能更加全面,主要用于输出运行日志、设置输出日志的等级、日志保存路径等等

必须放到 try……catch…… 里面才能保存 traceback 的错误的信息,然后不能保存 print (如果要保存可以参考方法二,但是这样控制台就没有 print 了)

demo.py

import logging
import os
import time
import traceback
import sys
# 创建一个 logger
logger = logging.getLogger(__name__)
# logger 的等级
logger.setLevel(level=logging.INFO)
# 创建一个 handler,写入日志文件
log_path = './Logs/'
if not os.path.exists(log_path):
    os.makedirs(log_path)
log_file_name = log_path + 'log-' + time.strftime("%Y%m%d-%H%M%S", time.localtime()) + '.log'
logfile = log_file_name
handler = logging.FileHandler(logfile, mode='a+')
# 输入到日志文件中的日志等级
handler.setLevel(logging.DEBUG)
# 设置 handler 中日志记录格式
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# 将 handler 添加到 logger 里面
logger.addHandler(handler)
# 将日志输出到控制台,默认 sys.stderr
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.info("Start print log")
if __name__ == '__main__':
    try:
        print(5555555555)
        print(5/0)
    except Exception as e:
        logger.error(str(traceback.format_exc()))

log-20210103-151751.log

2021-01-03 15:17:51,597 - __main__ - INFO - Start print log
2021-01-03 15:17:51,597 - __main__ - ERROR - Traceback (most recent call last):
  File "G:\Codes\demo.py", line 34, in <module>
    print(5/0)
ZeroDivisionError: division by zero

Python记录日志,保存控制台输出

首先,保存控制台的信息不等于保存代码中的输出print的内容。控制台上的信息不仅仅只有代码中print的信息,区分控制台重定向和标准输出重定向。

1.仅保存代码中print的信息。即重定向标准输出。

定义日志类:

class Logger(object):
    def __init__(self, filename='default.log', stream=sys.stdout):
        self.terminal = stream
        self.log = open(filename, 'a')
    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)
        self.terminal.flush()  # 不启动缓冲,实时输出
        self.log.flush()
    def flush(self):
        pass

在main函数开头启动日志

sys.stdout = Logger('./log.log', sys.stdout)
sys.stderr = Logger('./log.log', sys.stderr)

例子:

import tensorflow as tf
import os, sys
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
class Logger(object):
    def __init__(self, filename='default.log', stream=sys.stdout):
        self.terminal = stream
        self.log = open(filename, 'a')
    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)
        self.terminal.flush()  # 不启动缓冲,实时输出
        self.log.flush()
    def flush(self):
        pass
sys.stdout = Logger('./log.log', sys.stdout)
sys.stderr = Logger('./log.log', sys.stderr)
logit = tf.constant(1)
tf_config = tf.ConfigProto(log_device_placement=True)
sess = tf.Session(config=tf_config)
print(sess.run(logit))

此时log.log中只有内容“1”,没有log_device的信息,因为其不属于stdout/stderr,尽管控制台上有这些信息,

2.保存控制台上的所有信息。即控制台重定向。

测试代码:

# 控制台重定向
import tensorflow as tf
import os, time
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
logit = tf.constant(1)
tf_config = tf.ConfigProto(log_device_placement=True)
sess = tf.Session(config=tf_config)
print(sess.run(logit))

Linux下:

python3 -u train.py >> ./log.log 2>&1

nohup python3 -u train.py >> ./log.log 2>&1 &,不挂起后台运行

Windows下:

python -u test_gpu.py >> ./log.log 2>&1

start /min python -u test_gpu.py >> ./log.log 2>&1 &,这条命令多出了黑窗

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python实战项目scrapy管道学习爬取在行高手数据

    python实战项目scrapy管道学习爬取在行高手数据

    这篇文章主要为介绍了python实战项目scrapy管道学习拿在行练手爬虫项目,爬取在行高手数据,本篇博客的重点为scrapy管道pipelines的应用,学习时请重点关注
    2021-11-11
  • Anaconda3中的Jupyter notebook添加目录插件的实现

    Anaconda3中的Jupyter notebook添加目录插件的实现

    这篇文章主要介绍了Anaconda3中的Jupyter notebook添加目录插件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • 在Python中操作文件之seek()方法的使用教程

    在Python中操作文件之seek()方法的使用教程

    这篇文章主要介绍了在Python中操作文件之seek()方法的使用教程,是Python入门学习中的基础知识,需要的朋友可以参考下
    2015-05-05
  • numpy如何获取array中数组元素的索引位置

    numpy如何获取array中数组元素的索引位置

    这篇文章主要介绍了numpy获取array中数组元素的索引位置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-01-01
  • 关于Python解包知识点总结

    关于Python解包知识点总结

    在本篇文章里小编给各位分享的是关于Python解包知识点总结,有兴趣的朋友们可以学习参考下。
    2020-05-05
  • Python使用Pandas对csv文件进行数据处理的方法

    Python使用Pandas对csv文件进行数据处理的方法

    这篇文章主要介绍了Python使用Pandas对csv文件进行数据处理的方法,本文通过实例代码相结合给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • Python实现简易五子棋游戏

    Python实现简易五子棋游戏

    这篇文章主要为大家详细介绍了Python实现简易五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • python实现根据主机名字获得所有ip地址的方法

    python实现根据主机名字获得所有ip地址的方法

    这篇文章主要介绍了python实现根据主机名字获得所有ip地址的方法,涉及Python解析IP地址的相关技巧,需要的朋友可以参考下
    2015-06-06
  • 详解Python NumPy中矩阵和通用函数的使用

    详解Python NumPy中矩阵和通用函数的使用

    在NumPy中,矩阵是ndarray的子类,与数学概念中的矩阵一样,NumPy中的矩阵也是二维的,可以使用 mat 、 matrix 以及 bmat 函数来创建矩阵。本文将详细讲解NumPy中矩阵和通用函数的使用,感兴趣的可以了解一下
    2022-06-06
  • 使用python自动追踪你的快递(物流推送邮箱)

    使用python自动追踪你的快递(物流推送邮箱)

    本文讲解如何让 python自动为你查询快递信息 ,并在物流发生更新或者到达指定地点时第一时间将 物流推送至你的邮箱,本文通过实例代码截图的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2020-03-03

最新评论