美文网首页
Python隐藏warning和日志输出

Python隐藏warning和日志输出

作者: Max_7 | 来源:发表于2018-11-07 17:16 被阅读0次

最近跑程序时经常会出现一些warning或者第三方库的一些日志输出,这些东西有时会影响到其他程序对Python脚本的输出的读取。下面记录遇到的几种情况和对应的解决方案。

jieba 的日志输出

使用jieba的时候,会遇到以下情况:

Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\hello\AppData\Local\Temp\jieba.cache
Loading model cost 0.888 seconds.
Prefix dict has been built succesfully.

这种情况,把日志关闭就可以了。
logging日志有5种级别,
从低到高是

1 DEBUG
2 INFO
3 WARNING
4 ERROR
5 CRITICAL

这里只要比debug的级别高,就可以屏蔽debug级别的输出。

import logging
jieba.setLogLevel(logging.INFO)

gensim warning

使用gensim的时候遇到了以下两种警告

C:\Users\hello\AppData\Local\Programs\Python\Python36\lib\site-packages\gensim\utils.py:1212: UserWarning: detected Windows; aliasing chunkize to chunkize_serial
  warnings.warn("detected Windows; aliasing chunkize to chunkize_serial")
C:\Users\hello\AppData\Local\Programs\Python\Python36\lib\site-packages\gensim\matutils.py:718: FutureWarning: Conversion of the second argument of issubdtype from `int` to `np.signedinteger` is deprecated. In future, it will be treated as `np.int32 == np.dtype(int).type`.
  if np.issubdtype(vec.dtype, np.int):

解决方案:

warnings.filterwarnings(action='ignore', category=UserWarning, module='gensim')
warnings.filterwarnings(action='ignore', category=FutureWarning, module='gensim')

关于这个方法的官方文档:

tensorflow 警告

import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]='1'
# 默认的显示等级,显示所有信息
os.environ["TF_CPP_MIN_LOG_LEVEL"]='2'
# 只显示 warning 和 Error  
os.environ["TF_CPP_MIN_LOG_LEVEL"]='3'
# 只显示 Error 

通常把 level设成2,就会屏蔽警告信息了。

相关文章

  • Python隐藏warning和日志输出

    最近跑程序时经常会出现一些warning或者第三方库的一些日志输出,这些东西有时会影响到其他程序对Python脚本...

  • python日志管理

    python的logging如果不设置,那么系统会默认值输出warning和高于warning的日志信息,如图下:...

  • Python基础-----logging模块

    默认情况下Python的logging模块将日志打印到了标准输出中,且只显示了大于等于WARNING级别的日志,这...

  • Python日志输出

    简单的将日志打印到屏幕 输出:WARNING:root:warning messageERROR:root:err...

  • log4j文件的使用

    控制台日志 %d 输出时间 %t输出线程 %-5p输出的级别 debug info warning erro...

  • Python3 彩色日志包

    安装 使用 1 默认 logger 可以直接使用默认的logger实例输出日志,默认的日志等级是warning: ...

  • # Python—logging模块使用教程

    Python—logging模块使用教程 简单用法 日志等级 控制台输出日志 将日志保存到文件并且设置时间和输出格...

  • python 日志 logging 使用

    python logging 库用来在程序中添加日志输出,可以保存日志至文件,设定输出等级等。 导入模块和基础设置...

  • Python3:忽略警告

    python开发中经常遇到warning的情况,但是warning通常并不影响程序的运行,而且有时并不想看到其输出...

  • Python 命令输出重定向有缓存的问题

    将 Python 命令的输出重定向到日志文件的时候,发现输出并不会写到日志文件里 原因是 Python 做了缓存,...

网友评论

      本文标题:Python隐藏warning和日志输出

      本文链接:https://www.haomeiwen.com/subject/wfbvxqtx.html