如何记录在Python交互式Shell会话中发生的所有事件? [英] How to log everything that occurs in a Python interactive shell session?

查看:114
本文介绍了如何记录在Python交互式Shell会话中发生的所有事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实时访问解释器输入以及错误和标准输出.最好将此信息写入文件,以便在输入每个解释器命令后,我可以轮询文件以查找更改.例如,给定一个解释器会话:

I'd like to have real-time access to both interpreter input and error and standard output. Preferably this information would be written to a file, so that I can poll the file for changes after every interpreter command has been entered. For example, given an interpreter session:

>>> 5 * 7
35
>>> print("Hello, world!")
Hello, world!
>>> "Hello, world!"
'Hello, world!'

我想在日志文件中看到以下内容:

I'd like to see the following in a log file:

> 5 * 7
35
> print("Hello, world!")
Hello, world!
> "Hello, world!"
'Hello, world!'

格式不重要;重要的是我可以在文件中搜索关键字以触发会话期间的交互式事件.

The formatting is not important; what is important is that I can search the file for key words to trigger interactive events during the session.

到目前为止,我为完成此任务所学到的知识:

What I have learned so far trying to accomplish this:

Python的code模块允许我创建一个InteractiveConsole对象,可以重新定义其raw_input方法以登录到文件,如下所示:

Python's code module allows me to create an InteractiveConsole object, the raw_input method of which I can redefine to log to a file, like so:

import code
class LoggedConsole(code.InteractiveConsole):
  def __init__(self, locals):
    super(LoggedConsole, self).__init__(locals)
    self.file = open('consolelog.dat', 'a')

  def __del__(self):
    self.file.close()

  def raw_input(self, prompt=""):
    data = input(prompt)
    self.file.write(data+'\n')
    return data

此外,InteractiveConsole使用内置的write方法记录错误,我可以将其重新定义为:

Furthermore, InteractiveConsole uses a built-in write method to log errors, which I can redefine to:

def write(self, data):
  sys.stderr.write(data)
  self.file.write(data+'\n')

我还了解到以下代码段将记录所有标准输出:

I've also learned that the following snippet will log all stdout:

class Tee(object):
  def __init__(self):
    self.file = open('consolelog.dat', 'a')
    self.stdout = sys.stdout

  def __del__(self):
    sys.stdout = self.stdout
    self.file.close()

  def write(self, data):
    self.file.write(data)
    self.stdout.write(data)

sys.stdout = Tee()

我(试图)将所有这些结合在一起的尝试是然后创建一个LoggedConsole对象,并在本地人中将其传递给Tee.

My (broken) attempt to bring this all together was to then create a LoggedConsole object, and pass it Tee in locals.

console = LoggedConsole(locals={sys.stdout:LoggedExec()})
console.interact()

(我以前没有通过本地人,所以也许我在这里做错了,但是我没有收到错误.)

(I've not passed locals before, so perhaps I'm doing it incorrectly here, but I don't receive an error.)

无论如何,这将打开一个新的Interactive Console,并记录(关闭后)所有输入和错误,但不记录输出.我已经为此撞了一段时间了,我觉得自己已经接近了,但也许还没有.

Anyways, this will open a new Interactive Console, and will log (after closing) all input and errors, but not output. I've been banging my head against this for a while, and I feel like I'm close, but maybe not even.

还有,在会话期间 发生所有这些事情的方法吗?当前,所有日志记录都在会话关闭后进行.

Also, is there a way for all of this to occur during the session? Currently all logging takes place once the session is closed.

感谢您的时间,对不起您的留言.

Thanks for your time, sorry for the wall of text.

我希望能够在标准Python解释器中实现此目的,以实现可移植性.

edit: I'd like to be able to accomplish this in the standard Python interpreter for portability purposes.

edit2: Jaime的代码片段非常适合记录我需要的所有内容.但是,有什么办法可以让我实时执行此操作,而不是等待会话关闭?

edit2: Jaime's snippet works very well for logging everything I need. Any way, though, that I can have it do so in real time, instead of waiting for the session to close?

edit3: 弄清楚了:).最后一个有效的代码段:

import code
import sys

class Tee(object):
  def __init__(self, log_fname, mode='a'):
    self.log = open(log_fname, mode)

  def __del__(self):
    # Restore sin, so, se
    sys.stdout = sys.__stdout__
    sys.stdir = sys.__stdin__
    sys.stderr = sys.__stderr__
    self.log.close()

  def write(self, data):
    self.log.write(data)
    self.log.flush()
    sys.__stdout__.write(data)
    sys.__stdout__.flush()

  def readline(self):
    s = sys.__stdin__.readline()
    sys.__stdin__.flush()
    self.log.write(s)
    self.log.flush()
    return s

  def flush(foo):
    return

sys.stdout = sys.stderr = sys.stdin = Tee('consolelog.dat', 'w')

console = code.InteractiveConsole()
console.interact()

推荐答案

我仅在python2.7中对此进行了测试.我没有三个方便的人.

I've only tested this in python2.7. I don't have 3 handy.

import code
import sys

class Tee(object):

  def __init__(self, log_fname, mode='a'):
    self.log = open(log_fname, mode)

  def __del__(self):
    # Restore sin, so, se
    sys.stdout = sys.__stdout__
    sys.stdir = sys.__stdin__
    sys.stderr = sys.__stderr__
    self.log.close()

  def write(self, data):
    self.log.write(data)
    sys.__stdout__.write(data)

  def readline(self):
    s = sys.__stdin__.readline()
    self.log.write(s)
    return s

# Tie the ins and outs to Tee.
sys.stdout = sys.stderr = sys.stdin = Tee('consolelog.dat', 'w')

console = code.InteractiveConsole()
console.interact()

这篇关于如何记录在Python交互式Shell会话中发生的所有事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆