How do I show debugging information?

(Part of ZopeInAnger)

ZOPE writes to a log file in ./log. When you get a 'SYSTEM ERROR' you can find error message and the Python stack trace there. Often it is useful to send runtime information to the logs. That is described in the second section.

Use the Python debugger

If you want to use ZOPE in a debugger from the Python command line see the description in DEBUG.txt (http://svn.zope.org/Zope3/trunk/doc/DEBUG.txt) and

IPython? should work as well, see the ZOPE2 tutorial

http://wiki.zope.org/zope2/DebuggingWithIPythonAndOtherTips

and change:

IPython.Shell.IPShell(user_ns=locals()).mainloop(sys_exit=1)

to:

IPython.Shell.IPShell(user_ns={'root': root, 'app': app}).mainloop(sys_exit=1)

Sending information to the log file

Use the standard Python logging module. With ZOPE3 it gets written to both stdout and the file $instance/log/z3.log:

import logging
logger = logging.getLogger('mymodule')

logger.debug("Loading my project")

class Project(BTreeContainer):
  implements(IProject)

  def getPeriodContainer(self):
    """
      Return the period container
    """
    for item in self.items():
      if IPeriodContainer.providedBy(item[1]):
        return item[1]
    logger.info('There is no periods container in project %s!' % self.__name__)

    return None

To initialize the logger using an environment variable you can use something like this in the module's __init__.py:

import os
import logging

logger = logging.getLogger('mymodule')
if os.environ.get('MY_LOG_SEVERITY') == 'DEBUG':
  logger.info("Setting logger to DEBUG")
  logger.setLevel(logging.DEBUG)
else:
  logger.info("MY_LOG_SEVERITY='%s'"%os.environ.get('MY_LOG_SEVERITY'))
  logger.setLevel(logging.INFO)
  logger.info('Setting logger to INFO')

In principle allowing you to use a DEBUG mode and a production mode using the environment variable MY_LOG_SEVERITY. For ZOPE there is also a special environment variable - but in reality you should not need that.


Note: if you feel you have a better solution please edit this page.



( 96 subscribers )