本文将带您了解关于PySide和python日志记录的新内容,同时我们还将为您解释python日志记录的相关知识,另外,我们还将为您提供关于Docker中的Python日志记录类:日志消失了、Pyth
本文将带您了解关于PySide和python日志记录的新内容,同时我们还将为您解释python 日志记录的相关知识,另外,我们还将为您提供关于Docker中的Python日志记录类:日志消失了、Python日志记录、Python日志记录-检查日志文件的位置?、Python日志记录-禁用导入模块的日志记录的实用信息。
本文目录一览:- PySide和python日志记录(python 日志记录)
- Docker中的Python日志记录类:日志消失了
- Python日志记录
- Python日志记录-检查日志文件的位置?
- Python日志记录-禁用导入模块的日志记录
PySide和python日志记录(python 日志记录)
我想使用PySide创建一个简单的应用程序,仅用于从python日志记录中输出。
def mpm_print():
print 'OK'
def mpm_log():
log.info('OK')
class LabWindow(QtGui.QMainWindow):
def __init__(self):
super(LabWindow,self).__init__()
self.initUI()
mpm_print()
mpm_log()
def initUI(self):
font = QtGui.QFont()
font.setFamily("Courier")
font.setFixedPitch(True)
font.setPointSize(10)
self.qtxt = QtGui.QTextEdit(self)
self.qtxt.resize(self.size())
self.qtxt.setReadOnly(True)
self.qtxt.setFont(font)
self.resize(640,512)
self.setWindowTitle('Efficient Algorithms Lab')
self.show()
我想知道:
如何重定向stdout写入QTextEdit?
如何编写logging.Handler记录到QTextEdit?
谢谢
Docker中的Python日志记录类:日志消失了
几年来,我以相同的方式使用Python的日志记录类:
def get_module_logger(mod_name):
"""
To use this,do logger = get_module_logger(__name__)
"""
logger = logging.getLogger(mod_name)
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s [%(name)-12s] %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
然后在某些模块中
logger = get_module_logger(__name__)
现在,我正在运行一个在Docker容器中使用此功能的Python应用程序.我正在使用-d -i -t运行容器.当我在docker exec -it containername / bin / bash之后进入容器时,如果我在生成日志的python脚本中执行命令,则可以看到日志.但是,从外部来看,泊坞窗日志容器名从不显示任何内容.我已经尝试通过一些Web帖子以PYTHONUNBUFFERED = 0运行我的容器,但这也没有帮助.使用docker logs -f containername尾部也不会显示任何内容.所以我所有的日志,stderr和stdout都是空的.我也尝试过logging.StreamHandler(sys.stdout),但无济于事.
怎么了?我需要更改处理程序中的某些内容吗?
编辑:我的Dockerfile非常简单:
FROM python:3.5.1
MAINTAINER tommy@...
ADD . /tmp
#need pip > 8 to have internal pypi repo in requirements.txt
RUN pip install --upgrade pip
#do the install
RUN pip install /tmp/py/
CMD myservice
编辑2:
~ docker --version
Docker version 1.11.0,build 4dc5990
我创建了一个名为tommy.py的文件:
import logging
def get_module_logger(mod_name):
"""
To use this,do logger = get_module_logger(__name__)
"""
logger = logging.getLogger(mod_name)
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s [%(name)-12s] %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
if __name__ == "__main__":
get_module_logger(__name__).info("Hello World!")
运行以下命令:
docker run -d -v /tmp/tommy.py:/opt/tommy.py python:3.5 python /opt/tommy.py
看到了:
$docker logs -f sleepy_poincare
2016-08-30 17:01:36,026 [__main__ ] INFO Hello World!
编辑:
这是我的Docker版本:
$docker --version
Docker version 1.12.0,build 8eab29e
Python日志记录
官方文档:https://docs.python.org/2/library/logging.html logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有 filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。 filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。 format:指定handler使用的日志显示格式。 datefmt:指定日期时间格式。 level:设置rootlogger; 默认的日志级别设置为WARNING(日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET) stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。
只打印到控制台
# coding:utf-8
import logging
import sys
# 日志格式化方式
#LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
LOG_FORMAT = "%(asctime)s\tFile \"%(filename)s\",LINE %(lineno)-4d : %(levelname)-8s %(message)s"
# 日期格式化方式
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
def initLogging(logFilename):
#logging.basicConfig(filename=logFilename, level=logging.DEBUG, format=LOG_FORMAT)
#formatter = logging.Formatter(LOG_FORMAT);
#handler=logging.FileHandler(logFilename)
#handler.setLevel(logging.DEBUG)
#handler.setFormatter(formatter)
#logging.getLogger('''').addHandler(handler);
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
#console = logging.StreamHandler();
#console.setLevel(logging.INFO);
#console.setFormatter(formatter);
#logging.getLogger('''').addHandler(console);
initLogging("mylog.txt")
logging.info("Hello world")
只打印到文件
# coding:utf-8
import logging
import sys
# 日志格式化方式
#LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
LOG_FORMAT = "%(asctime)s\tFile \"%(filename)s\",LINE %(lineno)-4d : %(levelname)-8s %(message)s"
# 日期格式化方式
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
def initLogging(logFilename):
logging.basicConfig(filename=logFilename, level=logging.DEBUG, format=LOG_FORMAT)
#formatter = logging.Formatter(LOG_FORMAT);
#handler=logging.FileHandler(logFilename)
#handler.setLevel(logging.DEBUG)
#handler.setFormatter(formatter)
#logging.getLogger('''').addHandler(handler);
#logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
#console = logging.StreamHandler();
#console.setLevel(logging.INFO);
#console.setFormatter(formatter);
#logging.getLogger('''').addHandler(console);
initLogging("mylog.txt")
logging.info("Hello world")
同时打印到控制台和文件
# coding:utf-8
import logging
import sys
# 日志格式化方式
#LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
LOG_FORMAT = "%(asctime)s\tFile \"%(filename)s\",LINE %(lineno)-4d : %(levelname)-8s %(message)s"
# 日期格式化方式
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
def initLogging(logFilename):
logging.basicConfig(filename=logFilename, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
formatter = logging.Formatter(LOG_FORMAT);
#handler=logging.FileHandler(logFilename)
#handler.setLevel(logging.DEBUG)
#handler.setFormatter(formatter)
#logging.getLogger('''').addHandler(handler);
#logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
console = logging.StreamHandler();
console.setLevel(logging.INFO);
console.setFormatter(formatter);
logging.getLogger('''').addHandler(console);
initLogging("mylog.txt")
logging.info("Hello world")
https://www.cnblogs.com/heenhui2016/p/11424154.html
https://blog.csdn.net/energysober/article/details/53263295
Python日志记录-检查日志文件的位置?
如何解决Python日志记录-检查日志文件的位置??
该logging
模块使用附加到记录器的处理程序来决定最终存储或显示消息的方式,位置或方式。您可以logging
默认配置为也写入文件。您应该真正阅读过文档,但是如果您将要写入消息的文件的名称调用到logging.basicConfig(filename=log_file_name)
哪里log_file_name
(请注意,您必须在logging
调用其他任何文件之前先执行此操作),然后所有消息都会记录到所有记录器中(除非稍后会进行一些进一步的重新配置)。请注意记录器设置为什么级别;如果有内存,info
则低于默认日志级别,因此您还必须level=logging.INFO
将参数包含在内,以使basicConfig
消息最终出现在文件中。
至于问题的另一部分,则logging.getLogger(some_string)
返回一个Logger
对象,该对象从root记录器插入到层次结构中的正确位置,名称为的值some_string
。调用时不带参数,它将返回根记录器。__name__
返回当前模块的名称,因此logging.getLogger(__name__)
返回Logger
名称设置为当前模块名称的对象。这是与一起使用的常见模式logging
,因为它会使记录器结构镜像您代码的模块结构,这通常使调试时记录消息更加有用。
解决方法
知道Python日志语句存储在哪里的方法是什么?
即如果我这样做:
import logging
log = logging.getLogger(__name__)
log.info(''Test'')
在哪里可以找到日志文件?另外,当我打电话时:
logging.getLogger(__name__)
这与记录器的行为/保存方式有某种关系吗?
Python日志记录-禁用导入模块的日志记录
我正在使用Python日志记录模块,并且想禁用由导入的第三方模块打印的日志消息。例如,我正在使用类似以下内容的东西:
logger = logging.getLogger()
logger.setLevel(level=logging.DEBUG)
fh = logging.StreamHandler()
fh_formatter = logging.Formatter('%(asctime)s %(levelname)s %(lineno)d:%(filename)s(%(process)d) - %(message)s')
fh.setFormatter(fh_formatter)
logger.addHandler(fh)
当执行logger.debug(“ my
message!”)时,此命令会打印出调试消息,但也会从我导入的任何模块(例如请求和许多其他内容)中打印出调试消息。
我只想查看我感兴趣的模块中的日志消息。是否可以使日志记录模块执行此操作?
理想情况下,我希望能够告诉记录器打印来自“ ModuleX,ModuleY”的消息,而忽略所有其他消息。
我们今天的关于PySide和python日志记录和python 日志记录的分享已经告一段落,感谢您的关注,如果您想了解更多关于Docker中的Python日志记录类:日志消失了、Python日志记录、Python日志记录-检查日志文件的位置?、Python日志记录-禁用导入模块的日志记录的相关信息,请在本站查询。
本文标签: