Latest Version: 0.9.6.2
  Dashboard > Pylons Cookbook > ... > Recipes > Using colors to enhance readability of server output
  Pylons Cookbook Log In | Sign Up   View a printable version of the current page.  
  Using colors to enhance readability of server output
Added by chrysn, last edited by chrysn on Sep 25, 2007
Labels: 
(None)

Reading the default output of logged data from pylons serve --reload development.ini can be difficult because the data is visually monotonous:

Starting subprocess with file monitor
Starting server in PID 4615.
serving on 0.0.0.0:5000 view at http://127.0.0.1:5000
2007-09-25 15:53:43,465 INFO sqlalchemy.engine.base.Engine.0x..cc BEGIN
15:53:43,465 INFO [sqlalchemy.engine.base.Engine.0x..cc] BEGIN
2007-09-25 15:53:43,466 INFO sqlalchemy.engine.base.Engine.0x..cc SELECT users.id AS users_id, users.auth_type AS users_auth_type, users.auth_data AS users_auth_data
FROM users ORDER BY users.oid
15:53:43,466 INFO [sqlalchemy.engine.base.Engine.0x..cc] SELECT users.id AS users_id, users.auth_type AS users_auth_type, users.auth_data AS users_auth_data
FROM users ORDER BY users.oid
2007-09-25 15:53:43,467 INFO sqlalchemy.engine.base.Engine.0x..cc []
15:53:43,467 INFO [sqlalchemy.engine.base.Engine.0x..cc] []

On ANSI compatible terminals, the following should work:

How to do it

  1. Open development.ini and look for your default formatter (usually formatter_generic at the end of the file).
  2. Take a look at the format = }} line; it should say something like {{%(asctime)s,%(msecs)03d %(levelname)-5.5s [%(name)s] %(message)s
  3. Insert escape sequences. Escape sequences are codes starting with the escape character (visualized here by ␛; copy/paste will not work!), followed by a "[" and some other characters. On command line editors, you can usually insert the escape character by pressing Ctrl-V, Escape. Graphical editors have different ways of inputting this character or lack the functionality all together. Place them as follows:
    1. ␛[0;1m before %(asctime)s (bold)
    2. ␛[1;31m before %(levelname)s (red)
    3. ␛[1;34m before [%(name)s] (blue)
    4. ␛[0m before %(message)s (normal mode)
  4. Run the server again. Your output will look like this:

Starting subprocess with file monitor
Starting server in PID 4615.
serving on 0.0.0.0:5000 view at http://127.0.0.1:5000
2007-09-25 15:53:43,465 INFO sqlalchemy.engine.base.Engine.0x..cc BEGIN
15:53:43,465 INFO [sqlalchemy.engine.base.Engine.0x..cc] BEGIN
2007-09-25 15:53:43,466 INFO sqlalchemy.engine.base.Engine.0x..cc SELECT users.id AS users_id, users.auth_type AS users_auth_type, users.auth_data AS users_auth_data
FROM users ORDER BY users.oid
15:53:43,466 INFO [sqlalchemy.engine.base.Engine.0x..cc] SELECT users.id AS users_id, users.auth_type AS users_auth_type, users.auth_data AS users_auth_data
FROM users ORDER BY users.oid
2007-09-25 15:53:43,467 INFO sqlalchemy.engine.base.Engine.0x..cc []
15:53:43,467 INFO [sqlalchemy.engine.base.Engine.0x..cc] []

How it works

The escape characters are not handled by Python or any Python program, but passed on to the outputting device. According to the ISO/IEC 6429 standard, most terminals interpret the information between the escape character and the semicolon as control sequences. The sequences used here (␛[1;31m etc) indicate color change.

See http://en.wikipedia.org/wiki/ANSI_escape_code for more information.

ToDo

  • Document escape input methods for popular editors.
  • Find a way to change the colors according to the loglevels. (This will be more complicated than just inserting escape sequences in the config file.)

Site running on a free Atlassian Confluence Open Source Project License granted to Pylons. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.3.3 Build:#645 Feb 13, 2007) - Bug/feature request - Contact Administrators
Top