在这篇文章中,我们将为您详细介绍python日志记录字符串格式的内容,并且讨论关于python日志记录的相关问题。此外,我们还会涉及一些关于Docker中的Python日志记录类:日志消失了、PySi
在这篇文章中,我们将为您详细介绍python日志记录字符串格式的内容,并且讨论关于python 日志记录的相关问题。此外,我们还会涉及一些关于Docker中的Python日志记录类:日志消失了、PySide和python日志记录、Python - 文件中文本字符串的字符串格式或字符串插值、Python日志记录的知识,以帮助您更全面地了解这个主题。
本文目录一览:- python日志记录字符串格式(python 日志记录)
- Docker中的Python日志记录类:日志消失了
- PySide和python日志记录
- Python - 文件中文本字符串的字符串格式或字符串插值
- Python日志记录
python日志记录字符串格式(python 日志记录)
我正在使用python的日志格式化程序来格式化日志记录,并且我的fmt值为
fmt = "[%(filename)s:%(lineno)s] %(message)s"
我想要的是将[[file.py:20]”扩展到10个字符的宽度(例如)。如果这是一个简单的值,但是有什么办法可以将整个结构拉伸到指定的长度?我想要类似的东西:
tmp = "[%(filename)s:%(lineno)s]"fmt = "%(tmp)10s %(message)s"
我想知道是否可以使用字符串格式设置,或者是否可以以某种方式欺骗python的格式化程序以获取我想要的。
答案1
小编典典选项1
从这里开始:http :
//docs.python.org/library/logging.html#formatter-
objects
您将创建自己的自定义子类,Formatter
该子类将提供自己的唯一format
方法。
然后,您必须确保setFormatter()
每个人都调用,Handlers
以便他们使用新的格式化程序。
选项2
使用其他属性创建您自己的LogRecord子类。
子类Logger
和重写makeRecord
以创建的新子类LogRecord
。
提供使用此新属性值的自定义格式。
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
PySide和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?
谢谢
Python - 文件中文本字符串的字符串格式或字符串插值
嗯,问题很明显是我对 .format()
的知识很懒惰,因为 f-strings 看起来容易多了。解决方法很简单:
使用上面相同的 greeting.sql
文件,
py_name = 'Bob'
print(greeting.format(name = py_name))
做工精美
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 日志记录的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Docker中的Python日志记录类:日志消失了、PySide和python日志记录、Python - 文件中文本字符串的字符串格式或字符串插值、Python日志记录的相关知识,请在本站寻找。
本文标签: