python - 如何创建一个日志文件来记录 python 中的每个步骤?

标签 python linux unix

我正在运行一个 python 脚本,我想记录 python 程序的每个步骤。

例如,假设我有一个程序

def main()
    if do:
       do the work
       if yes:
          do the work
          for list os.listdir(dir):
              sys.system("python " + dirOfPython + " " + dirOflists)

我想记录我的程序在哪里……以及它们当前正在做什么。

在我的日志中我想要类似的东西

Inside of main()
inside of if do:
doing work
inside of if yes:
doing work
inside of for each files/dirs calling list
calling sys.system python
executing python with give dir path

不完全是上面的内容..但是某种日志来查看程序正在做什么,如果它失败了,这个日志将包含如果失败的日志和错误消息

我只是想要一个正式的日志文件

最佳答案

你知道,有一个 logging模块!

import logging
import os

logging.basicConfig(filename='tmp.log',
                    format='%(levelname)s %(asctime)s :: %(message)s',
                    level=logging.DEBUG)
# format is a formatter string, level shows what level of logs it will record
# in this case it is everything!
# Levels are as follows from most to least critical
#   CRITICAL
#   ERROR
#   WARNING
#   INFO
#   DEBUG

do = True
yes = True
do_the_work = lambda: None

def main():
    logging.debug("Inside of main()")
    if do:
        logging.debug("Inside of if do:")
        do_the_work()
        logging.debug("doing work")
        if yes:
            logging.debug("inside of if yes:")
            do_the_work()
            logging.debug("doing work")
            for list in os.listdir('.'): # there were three files in my folder
                logging.debug("inside of for each files/dirs calling list")
                print('python')
                logging.debug("calling sys.system python")
                logging.debug("executing python with give dir path")

这将产生以下输出:

DEBUG 2015-03-18 12:26:59,272 :: Inside of main()
DEBUG 2015-03-18 12:26:59,272 :: Inside of if do:
DEBUG 2015-03-18 12:26:59,272 :: doing work
DEBUG 2015-03-18 12:26:59,272 :: inside of if yes:
DEBUG 2015-03-18 12:26:59,272 :: doing work
DEBUG 2015-03-18 12:26:59,272 :: inside of for each files/dirs calling list
DEBUG 2015-03-18 12:26:59,272 :: calling sys.system python
DEBUG 2015-03-18 12:26:59,272 :: executing python with give dir path
DEBUG 2015-03-18 12:26:59,272 :: inside of for each files/dirs calling list
DEBUG 2015-03-18 12:26:59,272 :: calling sys.system python
DEBUG 2015-03-18 12:26:59,272 :: executing python with give dir path
DEBUG 2015-03-18 12:26:59,272 :: inside of for each files/dirs calling list
DEBUG 2015-03-18 12:26:59,272 :: calling sys.system python
DEBUG 2015-03-18 12:26:59,272 :: executing python with give dir path

您可以轻松捕获异常并让它们抛出更多关键事件。

try:
    really_important_method()
except EndOfTheWorldError:
    logging.critical("Duck and cover boys, it's gonna blow.")

关于python - 如何创建一个日志文件来记录 python 中的每个步骤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29129462/

相关文章:

python - Twisted - 将 Deferred 用于另一个 sql 查询

linux - 使用 .NET Core 在 Linux 上更改文件所有者

python - 如何从 Django 模板中的 staticfiles_dirs 访问文件?

python - 仅在我的两个相同网站之一上出现导入错误

python - Django:通过注释字段的总和来排序查询集?

linux - 从 bash 脚本中的字符串中提取模式

linux - 如何在 Shell 脚本中执行 For 循环而不是 While 循环

unix - 不能使用 '>' 或 '>>' 从 log.Println() 和 log.Printf() 写入文件

python - 如何检查文件是否写入终端?

linux - awk 打印一个模式