⇤ ← Revision 1 as of 2006-08-28 05:23:05
Size: 7398
Comment: pylons: 交互式调试器
|
Size: 7374
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
[[TableOfContents]] | |
Line 5: | Line 6: |
Things break, and when they do, quickly pinpointing what went wrong and why makes a huge difference. By default, Pylons uses a customized version of `Ian Bicking's <http://blog.ianbicking.org/>`_ EvalException middleware that also includes full Myghty Traceback information. | 事情总会出错, 而当它们出错的时候, 快速地查清楚什么和为什么出错就显得很重要了. Pylons 默认的调试器是使用一个 [http://blog.ianbicking.org/ Ian Bicking] 编写地 {{{EvalException}}} 中间件的定制版本, 它还加入了完整的 Myghty Traceback 信息. |
Line 7: | Line 8: |
See the Enabling Debugging section of the `Getting Started <getting_started.html>`_ guide to enable the interactive debugging. | 关于如何启动交互性调试请看 [http://pylonshq.com/docs/0.9.1/getting_start.html getting_started.html] 文档的 {{{Enabling Debugging}}} 一节. |
Line 11: | Line 12: |
The debugging screen has three tabs at the top: | 调试界面的顶端包括三个标签: |
Line 13: | Line 14: |
``Traceback`` Provides the raw exception trace with the interactive debugger |
* {{{Traceback}}} 使用交互式调试器提供原始的异常跟踪信息 |
Line 16: | Line 17: |
``Extra Data`` Displays CGI, WSGI variables at the time of the exception, in addition to configuration information |
* {{{Extra Data}}} 显示异常时刻的 CGI, WSGI 变量, 和配置信息. |
Line 19: | Line 20: |
``Myghty`` Human friendly traceback for Myghty templates, and errors raised as a result of Myghty templates |
* {{{Myghty}}} Myghty 模板的人性化的 traceback 信息, 和 Myghty 模板以返回值的形式抛出的错误. |
Line 22: | Line 23: |
Since Myghty compiles its templates to Python modules, it can be difficult to accurately figure out what line of the template resulted in the error. The `Myghty` tab provides the full Myghty traceback which contains accurate line numbers for your templates, and where the error originated from. If your exception was triggered before a template was rendered, no Myghty information will be available in this section. | 既然 Myghty 会把模板编译成 Python 模块, 那么要精确地找出错误出在模板的哪一行是比较困难的. {{{Myghty}}} 标签提供了完整的 Myghty traceback, 它包含了精确的模板行号和错误起源. 如果你的错误是在模板渲染之前触发的, 那么这一节就没用任何 Myghty 信息了. |
Line 24: | Line 25: |
== 示例: 探索 Traceback | == 示例: 研究 Traceback == |
Line 26: | Line 27: |
Using the interactive debugger can also be useful to gain a deeper insight into objects present only during the web request like Myghty's ``m`` object, or the ``session`` and ``request`` objects. The thing to keep in mind with some of these objects is that you won't be able to explore them through the interactive debugger unless you first use a supplied Controller method to `bind them to your controller <class-pylons.controllers.Controller.html#_attach_locals>`_. | 使用交互调试器还有一个好处就是可以对那些只在请求过程中才存在的对象--比如 Myghty 的 {{{m}}} 对象, 或者是 {{{session}}} 和 {{{request}}} 对象--有更深入的认识. 对于有些对象需要注意的是, 除非你先调用一个 Controller 内置的方法把它们[http://pylonshq.com/docs/0.9.1/class-pylons.controllers.Controller.html#_attach_locals 绑定到你的控制器], 你将没法在交互式调试中研究它们. |
Line 28: | Line 29: |
To trigger an error so that we can explore what's happening just raise an exception inside an action you're curious about. In this example, we'll raise an error in the action that's used to display the page you're reading this on. Here's what the docs controller looks like: | 只要在你感兴趣的 action 里抛出一个异常, 就能触发一个错误, 这样我们就可以来研究研究里面究竟发生了什么. 在这个例子中, 我们会在一个用来显示你正在阅读的这个页面的 action 中抛出一个错误. 这个 docs 控制器内容如下: |
Line 38: | Line 39: |
Since we want to explore the ``session`` and ``request``, we'll need to bind them first. Here's what our action now looks like with the binding and raising an exception: | 既然我们要研究 {{{session}}} 和 {{{request}}}, 我们需要绑定它们先. 加上绑定和抛出异常的代码后我们的 action 是这个样子: |
Line 40: | Line 41: |
#!python | {{{#!python |
Line 49: | Line 50: |
Here's what exploring the Traceback from the above example looks like (Excerpt of the relevant portion): | 下面这个图是在上面这个例子中研究 Traceback 的一个截图 (只截取相关部分): |
Line 51: | Line 52: |
.. image:: /img/screenshots/doctraceback.gif |
[http://pylonshq.com/img/screenshots/doctraceback.gif] |
交互式调试器
交互式应用程序调试
事情总会出错, 而当它们出错的时候, 快速地查清楚什么和为什么出错就显得很重要了. Pylons 默认的调试器是使用一个 [http://blog.ianbicking.org/ Ian Bicking] 编写地 EvalException 中间件的定制版本, 它还加入了完整的 Myghty Traceback 信息.
关于如何启动交互性调试请看 [http://pylonshq.com/docs/0.9.1/getting_start.html getting_started.html] 文档的 Enabling Debugging 一节.
调试界面
调试界面的顶端包括三个标签:
Traceback
- 使用交互式调试器提供原始的异常跟踪信息
Extra Data
- 显示异常时刻的 CGI, WSGI 变量, 和配置信息.
Myghty
- Myghty 模板的人性化的 traceback 信息, 和 Myghty 模板以返回值的形式抛出的错误.
既然 Myghty 会把模板编译成 Python 模块, 那么要精确地找出错误出在模板的哪一行是比较困难的. Myghty 标签提供了完整的 Myghty traceback, 它包含了精确的模板行号和错误起源. 如果你的错误是在模板渲染之前触发的, 那么这一节就没用任何 Myghty 信息了.
示例: 研究 Traceback
使用交互调试器还有一个好处就是可以对那些只在请求过程中才存在的对象--比如 Myghty 的 m 对象, 或者是 session 和 request 对象--有更深入的认识. 对于有些对象需要注意的是, 除非你先调用一个 Controller 内置的方法把它们[http://pylonshq.com/docs/0.9.1/class-pylons.controllers.Controller.html#_attach_locals 绑定到你的控制器], 你将没法在交互式调试中研究它们.
只要在你感兴趣的 action 里抛出一个异常, 就能触发一个错误, 这样我们就可以来研究研究里面究竟发生了什么. 在这个例子中, 我们会在一个用来显示你正在阅读的这个页面的 action 中抛出一个错误. 这个 docs 控制器内容如下:
既然我们要研究 session 和 request, 我们需要绑定它们先. 加上绑定和抛出异常的代码后我们的 action 是这个样子:
下面这个图是在上面这个例子中研究 Traceback 的一个截图 (只截取相关部分):
[http://pylonshq.com/img/screenshots/doctraceback.gif]
Email 选项
You can make all sorts of changes to how the debugging works. For example if you disable the debug variable in the config file Pylons will email you an error report instead of displaying it as long as you provide your email address at the top of the config file:
error_email_from = [email protected]
This is very useful for a production site. Emails are sent via SMTP so you need to specify a valid SMTP server too.
修改调试器主题
If you are using Pylons in a commercial company it is useful to be able to change the theme of the debugger so that if an error occurs, a page with your company logo appears. You might also decide to remove the Pylons logo if you use the debugger a lot so that there is more space to view the traceback.
You can change the theme by creating a new template. For example, a very simple template might look like this:
1 my_error_template = '''
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5 <head>
6 <title>Server Error</title>
7 %(head)s
8 <body id="documentation">
9 %(extra_data)s
10 %(myghty_data)s
11 %(traceback_data)s
12 </body>
13 </html>
14 '''
The values are automatically substituted by the error middleware. You can also add %(prefix)s which is replaced by the path to your application so you can include CSS files or images. For example if your application had a file called style.css in a directory called css within your public directory, you could add the following line to your template to ensure that the CSS file was always correctly found:
<link rel="stylesheet" href="%(prefix)s/css/style.css" type="text/css" media="screen" />
If you want to retain the ability to switch between the different error displays you need a slightly more complicated example:
1 my_error_template = '''
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5 <head>
6 <title>Server Error</title>
7 %(head)s
8 <body id="documentation" onload="switch_display('%(set_tab)s')">
9 <ul id="navlist">
10 <li id='traceback_data_tab' class="active">
11 <a href="javascript:switch_display('traceback_data')" id='traceback_data_link'>Traceback</a>
12 </li>
13 <li id='extra_data_tab' class="">
14 <a href="javascript:switch_display('extra_data')" id='extra_data_link'>Extra Data</a>
15 </li>
16 <li id='myghty_data_tab'>
17 <a href="javascript:switch_display('myghty_data')" id='myghty_data_link'>Myghty</a>
18 </li>
19 </ul>
20 <div id="extra_data" class="hidden-data">
21 %(extra_data)s
22 </div>
23 <div id="myghty_data" class="hidden-data">
24 %(myghty_data)s
25 </div>
26 <div id="traceback_data">
27 %(traceback_data)s
28 </div>
29 </body>
30 </html>
31 '''
In this case when you click on a link the relevant tab is displayed. As long as you keep the same IDs and class names, you can specify your own styles and create a theme like the one used by Pylons by default.
Now that you have a template you need to use it in your application. In config/middleware.py change the following lines:
#!python
- # @@@ Error Handling @@@
app = ErrorHandler(app, global_conf, error_template=error_template, **config.errorware)
}}}
to use your template:
1 # @@@ Error Handling @@@
2 my_error_template = '''
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6 <head>
7 <title>Server Error</title>
8 %(head)s
9 <body id="documentation">
10 %(extra_data)s
11 %(myghty_data)s
12 %(traceback_data)s
13 </body>
14 </html>
15 '''
16 app = ErrorHandler(app, global_conf, error_template=my_error_template, **config.errorware)
Your interactive debugger will now be themed with the new template.