简体   繁体   English

Google Colab 中的日志模块问题

[英]Problem with Logging Module in Google Colab

I have a python script with an error handling using the logging module.我有一个 python 脚本,使用日志记录模块进行错误处理。 Although this python script works when imported to google colab, it doesn't log the errors in the log file.尽管此 python 脚本在导入到 google colab 时有效,但它不会在日志文件中记录错误。

As an experiment, I tried this following script in google colab just to see if it writes log at all作为实验,我在 google colab 中尝试了以下脚本,只是为了查看它是否完全写入日志

import logging
logging.basicConfig(filename="log_file_test.log",
                            filemode='a',
                            format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
                            datefmt='%H:%M:%S',
                            level=logging.DEBUG)

logging.info("This is a test log ..")

To my dismay, it didn't even create a log file named log_file_test.log .令我沮丧的是,它甚至没有创建名为log_file_test.log的日志文件。 I tried running the same script locally and it did produce a file log_file_test.log with the following text我尝试在本地运行相同的脚本,它确实生成了一个包含以下文本的文件log_file_test.log

13:20:53,441 root INFO This is a test log.. 13:20:53,441 root INFO 这是一个测试日志..

What is it that I am missing here?我在这里想念什么? For the time being, I am replacing the error logs with print statements, but I assume that there must be a workaround to this.目前,我正在用打印语句替换错误日志,但我认为必须有解决方法。

Perhaps you've reconfigured your environment somehow? 也许您已经以某种方式重新配置了环境? (Try Runtime menu -> Reset all runtimes...) Your snippets works exactly as written for me -- (尝试运行时菜单->重置所有运行时...)您的代码段的工作方式与为我编写的完全一样-

在此处输入图片说明

logging.basicConfig can be run just once* logging.basicConfig可以只运行一次*

Any subsequent call to basicConfig is ignored.忽略对basicConfig的任何后续调用。

* unless you are in Python 3.8 and use the flag force=True *除非你在 Python 3.8 并使用标志force=True

logging.basicConfig(filename='app.log',
                    level=logging.DEBUG,
                    force=True, # Resets any previous configuration
                    )

Workarounds (2)解决方法 (2)

(1) You can easily reset the Colab workspace with this command (1)您可以使用此命令轻松重置 Colab 工作区

exit

Wait for it to come back and try your commands again.等待它回来并再次尝试您的命令。

(2) But, if you plan to do the reset more than once and/or are learning to use logging , maybe it is better to use %%python magic to run the entire cell in a subprocess. (2)但是,如果您计划多次重置和/或正在学习使用logging ,也许最好使用%%python magic在子进程中运行整个单元格。 See photo below.见下图。

在此处输入图像描述


What is it that I am missing here?我在这里想念什么?

Deeper understanding of how logging works.更深入地了解logging的工作原理。 It is a bit tricky, but there are many good webs explaining the gotchas.这有点棘手,但是有很多很好的网站可以解释陷阱。

[This answer][1] cover the issue. [这个答案][1] 涵盖了这个问题。 You have to:你必须:

  1. Clear your log handlers from the environment with logging.root.removeHandler使用 logging.root.removeHandler 从环境中清除日志处理程序
  2. Set log level with logging.getLogger('RootLogger').setLevel(logging.DEBUG).使用 logging.getLogger('RootLogger').setLevel(logging.DEBUG) 设置日志级别。

Setting level with logging.basicConfig only did not work for me.仅使用 logging.basicConfig 设置级别对我不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM