Logging

Python provides logging APIs. Web2py provides a mechanism to configure it so that apps can use it.

In your application, you can create a logger, for example in a model:

  1. import logging
  2. logger = logging.getLogger("web2py.app.myapp")
  3. logger.setLevel(logging.DEBUG)

and you can use it to log messages of various importance

  1. logger.debug("Just checking that %s", details)
  2. logger.info("You ought to know that %s", details)
  3. logger.warn("Mind that %s", details)
  4. logger.error("Oops, something bad happened %s", details)

logging is a standard Python module described at https://docs.python.org/2/library/logging.html (Python 2.x) and https://docs.python.org/3/library/logging.html (Python 3.x)

The string “web2py.app.myapp” defines an app-level logger.

For this to work properly, you need a configuration file for the logger. One is provided by web2py in the “examples” folder “logging.example.conf”. You need to copy the file to web2py’s directory and rename the file to “logging.conf” and customize it as necessary.

This file is self documenting, so you should open it and read it.

To create a configurable logger for application “myapp”, you must add myapp to the [loggers] keys list:

  1. [loggers]
  2. keys=root,rocket,markdown,web2py,rewrite,app,welcome,myapp

and you must add a [logger_myapp] section, using [logger_welcome] as a starting point.

  1. [logger_myapp]
  2. level=WARNING
  3. qualname=web2py.app.myapp
  4. handlers=consoleHandler
  5. propagate=0

The “handlers” directive specifies the type of logging and here it is logging “myapp” to the console.