Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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_Stdout - Fatal编程技术网

Python:在个人标准文本上编辑文本-加倍文本

Python:在个人标准文本上编辑文本-加倍文本,python,logging,stdout,Python,Logging,Stdout,我需要将控制台中显示的日志保存到文件中,以便编辑sys.stdout。(使用来自的代码) 但当我试图在write函数中编辑文本时,问题就出现了,在文本之前添加了一些内容。结果,“[stack]”在文本变量之前和之后添加 import sys class Logger(object): def __init__(self): self.terminal = sys.stdout self.file = open("log.txt", "a") de

我需要将控制台中显示的日志保存到文件中,以便编辑sys.stdout。(使用来自的代码)

但当我试图在write函数中编辑文本时,问题就出现了,在文本之前添加了一些内容。结果,“[stack]”在文本变量之前和之后添加

import sys

class Logger(object):
    def __init__(self):
        self.terminal = sys.stdout
        self.file = open("log.txt", "a")
    def flush(self):
        self.terminal.flush()
        self.file.flush()
    def write(self, text):
        self.terminal.write("[stack]" + text)
        self.file.write(text)
        self.flush();

sys.stdout = Logger()

print "Test log"
print "Another test log"
结果:

[stack]Test log[stack]
[stack]Another test log[stack]

如果您使用的是Python3,则可以重载内置
print
,以自定义输出。下面的代码为您提供了预期的输出:

import sys
import builtins

class Logger(object):
    def __init__(self):
        self.terminal = sys.stdout
        self.file = open("log.txt", "a")
    def flush(self):
        self.terminal.flush()
        self.file.flush()
    def write(self, text):
        self.terminal.write(text)
        self.file.write(text)
        self.flush();

def myprint(string):
  builtins.print("%s%s" % ("[stack]", string))

print = myprint

sys.stdout = Logger()

我一直在琢磨这个问题,直到我想我应该通过一个调试器来运行它。出现这种情况的原因是,当您使用
print
时,它将尝试写入文本正文以及配置的
end
,默认情况下,这是一个换行符

因此,每个
print
语句都会导致对
记录器的两个单独调用。write
,一个是(例如)
测试日志
,第二个是
\n
。这将导致输出
[stack]测试日志[stack]\n

下面是一个正确的实现:

import sys


class Logger(object):
    def __init__(self, stream, default_sep=' ', default_end='\n'):
        self.terminal = stream
        self.default_sep = default_sep
        self.default_end = default_end
        self.continuing_same_print = False
        self.file = open("log.txt", "a")

    def flush(self):
        self.terminal.flush()
        self.file.flush()

    def write(self, text):
        if text is self.default_end:
            self.continuing_same_print = False
        elif text is self.default_sep:
            self.continuing_same_print = True

        new_text = text
        if text in {self.default_sep, self.default_end}:
            pass
        elif self.continuing_same_print:
            pass
        else:
            new_text = '[stack]' + new_text

        self.terminal.write(new_text)
        self.file.write(text)
        self.flush()


sys.stdout = Logger(sys.stdout)

print("Test", "log")
print("Another test log")
print()
输出

[stack]Test log
[stack]Another test log
编辑

[stack]Test log
[stack]Another test log

更新了实现以支持在打印语句中打印多个对象。

print“Test log”
write(“Test log”)
write(“\n”)
的形式执行,因此您可以得到
write(“[stack]”“+”Test log”)
write(“[stack]”“+”\n”)
。尝试使用逗号
print“Test”,“log”
,你会得到更多
[stack]
-
[stack]Test[stack][stack]log[stack]
:)尝试
print(“Test”,“log”)
你会得到
[stack]Test[stack][stack]log
)它仍然需要一些工作。@Fura很好,我在上面。所以。。。祝你好运@福拉斯,我想我知道了。