Google Code offered in: English - Español - 日本語 - 한국어 - Português - Pусский - 中文(简体) - 中文(繁體)
This article was written and submitted by an external contributor. The Google App Engine team thanks Antonin Hildebrand for his time and expertise.
Antonin HildebrandLogging events is a helpful technique for application development. If you are new to App Engine logging, you should read a detailed article about logging events in your Application. Sometimes it is useful to see log events in realtime. A quick solution is to use javascript logging console available in App Engine by simply adding ?debug parameter to the current url.
Unfortunately this method has some drawbacks:
My motivation was to solve the issues with the javascript console. I prefer to use Firefox and Firebug as a development platform for client-side code. My inspiration was the FirePHP project by Christoph Dorn. I've decided to write FirePython similar way.
FirePython is a server-side Python library, which hooks into standard Python logging module and sends logging events to a client-side using response HTTP headers. On the client-side there must be a browser extension which is capable to present incoming logging events to the user. For this occasion I've written an extension for Firefox called FireLogger (integrated into Firebug).
Although FirePython was originally created for App Engine, it has no App Engine specific dependencies. It may be used with almost any Python server-side web framework which is capable to emit response headers.
Note: this setup instructions were valid for FirePython 0.4 and may be outdated. For latest details, please refer to the FirePython documentation.
Prerequisities:
sys.path
For WSGI you need to wrap your WSGI application with FirePythonWSGI:
Fixfrom firepython.middleware import FirePythonWSGI
run_wsgi_app(FirePythonWSGI(Application()))
from firepython.middleware import FirePythonWSGI run_wsgi_app(FirePythonWSGI(Application()))End Fix
For Django you need to enable FirePythonDjango middleware:
MIDDLEWARE_CLASSES: firepython.middleware.FirePythonDjango
In case you use other framework, you basically have to:
FirePython emits buffered logs as HTTP response headers using provided add_header function (this depends on your framework and for inspiration you may look at FirePythonWSGI class).
Note: usually you want to enable DEBUG level logging in development mode:
logging.getLogger().setLevel(logging.DEBUG)
FireLogger properties menu is available under green bug icon on Firebug Toolbar.
You may set password to protect production site logs. Logs are sent
only when FireLogger is present on the client side and password matches
on both sides. Password is sent as a md5 hash in request headers, so
this is not very safe unless you are using https connection.
Alternatively, you may use App Engine's Users API to send logs only
when there is project admin logged in (see example in Bloog project).
Firebug has a feature called "External Editors" which enables you to open source files in your favorite text editor. When you click on a timestamp field of a log record, FireLogger takes the first configured Firebug editor and tries to open original source file on the particular line.
For example you may configure TextMate editor like this:
Note: On the production site, paths to source files usually do not match paths on development machine. For example App Engine production server stores uploaded projects under generated folder names. You may want to use Rewrites preferences pane to setup a remapping from production paths to development paths. You may setup regular expression which strips production site prefix and replaces it with development machine root folder path.
You may send profiling graphs from server-side to client and open them in .dot viewer.
By default all log messages are logged using a default root logger. You may create more loggers and use logger names to categorize log events into related groups (see log record's right side in top screenshot). To rename the root logger:
logging.getLogger().name = "my logger"
Refer to the standard Python logging module for more details.
This is an advanced feature, which enables you to republish FireLogger logs coming from other servers on server-side. My scenario was following:
The goal was to see log events from both servers. I've configured classic server to republish logs from API server to correctly propagate them to the client side.
If you are interested in details, you may read more about FireLogger protocol.
In production there are limits on maximum HTTP response size and
FirePython output may get truncated if gets too large. You may set firepython.DEEP_LOCALS = False
if you want to reduce response sizes on production. Also decreasing firepython.JSONPICKLE_DEPTH
reduces occurrence of this problem.
http://firepython.binaryage.com
FirePython and FireLogger are open-source projects. You are welcome to fork and contribute. Also thanks to all past contributors.
Antonin is a freelance developer from Czech Republic.
He works in BinaryAge and his personal mission is to help developers with their tools.
As a web developer, you may be interested in some other open-source projects created by Antonin: