Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python日志:如何在日志文件中保存类属性值_Python_Logging - Fatal编程技术网

Python日志:如何在日志文件中保存类属性值

Python日志:如何在日志文件中保存类属性值,python,logging,Python,Logging,我是python的新手,除了日志文件中长管道的结果外,我还想保存管道中创建的一些实例的参数/类属性 理想情况下,这不会对实现类的代码造成太多污染 如果解决方案只考虑类的实例,并将其属性写入日志文件,而不涉及任何类实现,则更好 有什么建议,或是上帝的建议吗 ---编辑: 我所能想到的最明显的是一个未经修饰和简化的初始尝试(如注释中所述),它包括添加一个方法,该方法在调用该方法时以字符串形式查询类的属性: 在包含两个模块的python包中,main.py和a_class.py编写如下: >&g

我是python的新手,除了日志文件中长管道的结果外,我还想保存管道中创建的一些实例的参数/类属性

理想情况下,这不会对实现类的代码造成太多污染

如果解决方案只考虑类的实例,并将其属性写入日志文件,而不涉及任何类实现,则更好

有什么建议,或是上帝的建议吗

---编辑:

我所能想到的最明显的是一个未经修饰和简化的初始尝试(如注释中所述),它包括添加一个方法,该方法在调用该方法时以字符串形式查询类的属性:

在包含两个模块的python包中,
main.py
a_class.py
编写如下:

>> cat main.py

import logging
from a_class import MyClass


logging.basicConfig(filename='example.log',level=logging.DEBUG)

logging.warning('Print this to the console and save it to the log')
logging.info('Print this to the console')

o = MyClass()
o.attribute_1 = 1
o.attribute_2 = 3
o.attribute_3 = 'Spam'

logging.info(o.print_attributes())

example.log包含我想要的内容,即:

WARNING:root:Print this to the console and save it to the log
INFO:root:Print this to the console
INFO:root:
class.attribute_1 1
class.attribute_2 3
class.attribute_3 Spam
在重新表述这个问题时,是否有一种方法可以对类的属性执行相同的查询并将其发送到日志,而无需在类本身中添加任何类型的
print\u attributes
方法?

我建议这样做,以便它可以很好地显示所有显著的属性值

然后您可以将实例记录为简单的值:
log.info(“现在foo是%s”,foo\u实例)。

一个完整的例子:

class Donut(object):
    def __init__(self, filling, icing):
        self.filling = filling
        self.icing = icing

    def __repr__(self):
        return 'Donut(filling=%r, icing=%r)' % (self.filling, self.icing)


donut = Donut('jelly', 'glaze')


import logging

logging.basicConfig()

logging.getLogger().warn('Carbs overload: one %s too much', donut)
输出:

2017-10-25 10:59:05,302 9265 WARNING Carbs overload: one Donut(filling='jelly', icing='glaze') too much

使用内置的
命令

class MyClass():
    def __init__(self):
        self.attribute_1 = 0
        self.attribute_2 = 0
        self.attribute_3 = 0

o = MyClass()
print o.__dict__
产出:

{'attribute_2': 0, 'attribute_3': 0, 'attribute_1': 0}

您可以在日志中随意使用它。

我同意@Iguananaut的说法,没有神奇的方法可以做到这一点。然而,下面的方法可能会奏效。IMO,它比您编写的
print\u attributes
方法要好

import logging

logging.basicConfig()
logger = logging.getLogger('ddd')

logger.setLevel(logging.DEBUG)

class A(object):
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def __str__(self):
        return "\n".join(["{} is {}".format(k, v) 
                         for k, v in self.__dict__.iteritems()])


a = A(1, 2, 3)

logger.debug(a)
结果如下所示-

{12:43}~ ➭ python logging_attrib.py
DEBUG:ddd:a is 1
c is 3
b is 2

请告诉我你的想法

你试过什么吗?您是否有一些示例代码来显示您正在尝试做什么?一般来说,为了使用日志记录来插入某些方法调用,没有简单的方法可以让它神奇地发生。您可以做的一件事是创建一个decorator,它将方法调用包装为对日志的写入,但随后您必须将这个decorator显式地放在您想要记录的每个方法上。或者,您可以编写一个类装饰器,自动将其添加到类上的每个方法,甚至元类中,但这会带来麻烦。听起来你主要是想让
\uuuu init\uuuu
方法使用它。@SRC请查看编辑。你能详细说明你提到的问题吗?那会有帮助的。谢谢。添加了一个工作示例。如果需要的话,请随意将您的
\uuuuuurepr\uuuuuuu
变得更加详细。IIRC,
\uuuuuuuurepr\uuuuu
的实现方式应确保
eval(repr(x))==x
,而
\uuuuuuuu str\uuuuuuu
应该是更“人类可读”的形式。也就是说,你的
\uuuu repr\uuuuu
应该是
\uuu str\uuuu
,并且
\uuuu repr\uuuu
应该实际返回
甜甜圈(%s,%s)%(自填充,自结冰)
@mkrieger1:确实!更新答案。您想要的也被称为。
{12:43}~ ➭ python logging_attrib.py
DEBUG:ddd:a is 1
c is 3
b is 2