有没有办法使用logging模块自动将事物输出到stdout 以及它们应该去的日志文件?例如,我想所有呼叫logger.warning,logger.critical,logger.error去他们预期的地方,但除了总是被复制到stdout.这是为了避免重复消息,如:
mylogger.critical("something failed")
print "something failed"
Run Code Online (Sandbox Code Playgroud) 有没有办法将所有打印输出保存到python中的txt文件?让我说我的代码中有这两行,我想将打印输出保存到一个名为的文件output.txt.
print ("Hello stackoverflow!")
print ("I have a question.")
Run Code Online (Sandbox Code Playgroud)
我希望output.txt文件包含
Hello stackoverflow!
I have a question.
Run Code Online (Sandbox Code Playgroud) 我在python中相当新,并开始进入日志记录模块.我想将消息记录到日志文件中并输出到控制台.下面的代码将消息输出到控制台但是如何让所有消息都登录到文件中?
Logger对象没有用于记录到文件的函数调用(basicConfig(filename =)).如何添加此功能?
我在这里先向您的帮助表示感谢.
import logging
# create logger
logger = logging.getLogger(_name_)
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
Run Code Online (Sandbox Code Playgroud) 最新的Windows 10更新包括对 conhost.exe中的ANSI转义序列的支持.
我已经能够确认在cmd.exe中正确地获取了转义序列,所以我有必要的更新.特别是,我尝试输入prompt $e[?25l,隐藏光标,然后prompt $e[?25h再次显示光标.
但是,如果我启动Python解释器,然后执行以下操作:
>>> import sys
>>> sys.stdout.write("\033[?25l")
Run Code Online (Sandbox Code Playgroud)
好吧,光标没有隐藏.如何以正确的方式设置,以便控制台能够从Python获取转义序列?
我为python安装了colorama.我按如下方式导入了模块:
import colorama
from colorama import init
init()
from colorama import Fore, Back, Style
print Fore.RED + "My Text is Red"
Run Code Online (Sandbox Code Playgroud)
它返回ANSI字符....
esc[31mMy Text is Red
Run Code Online (Sandbox Code Playgroud)
这不是我的预期.难道我做错了什么.
谢谢.
我想在运行python脚本时在控制台中强制使用QuickEdit Mode,然后在终止之前禁用它.有没有办法做到这一点?
显然,我不应该再使用 ScrapyFileLogObserver ( http://doc.scrapy.org/en/1.0/topics/logging.html )。但是我仍然希望能够将我的日志消息保存到文件中,并且我仍然希望所有标准的 Scrapy 控制台信息也保存到文件中。
通过阅读如何使用日志记录模块,这是我尝试使用的代码:
class BlahSpider(CrawlSpider):
name = 'blah'
allowed_domains = ['blah.com']
start_urls = ['https://www.blah.com/blahblahblah']
rules = (
Rule(SgmlLinkExtractor(allow=r'whatever'), callback='parse_item', follow=True),
)
def __init__(self):
CrawlSpider.__init__(self)
self.logger = logging.getLogger()
self.logger.setLevel(logging.DEBUG)
logging.basicConfig(filename='debug_log.txt', filemode='w', format='%(asctime)s %(levelname)s: %(message)s',
level=logging.DEBUG)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
simple_format = logging.Formatter('%(levelname)s: %(message)s')
console.setFormatter(simple_format)
self.logger.addHandler(console)
self.logger.info("Something")
def parse_item(self):
i = BlahItem()
return i
Run Code Online (Sandbox Code Playgroud)
它运行良好,并将“Something”保存到文件中。但是,我在命令提示符窗口中看到的所有内容,以前使用 ScrapyFileLogObserver 时保存到文件中的所有内容,现在都没有保存。
我认为我的带有“logging.StreamHandler()”的“控制台”处理程序应该处理这个问题,但这只是我读过的,我并不真正理解它是如何工作的。
谁能指出我遗漏了什么或我哪里出错了?
谢谢你。
python ×7
logging ×3
windows ×2
ansi ×1
cmd ×1
console ×1
prompt ×1
python-3.x ×1
scrapy ×1
windows-10 ×1