- Python Pyramid - Discussion
- Python Pyramid - Useful Resources
- Python Pyramid - Quick Guide
- Python Pyramid - Deployment
- Python Pyramid - Security
- Python Pyramid - Logging
- Python Pyramid - Testing
- Command Line Pyramid
- Creating A Project Manually
- Python Pyramid - Package Structure
- Python Pyramid - Project Structure
- Python Pyramid - Creating A Project
- Python Pyramid - Cookiecutter
- Pyramid - Using SQLAlchemy
- Python Pyramid - Message Flashing
- Python Pyramid - Events
- Python Pyramid - Sessions
- Python Pyramid - Response Object
- Python Pyramid - Request Object
- Python Pyramid - Static Assets
- Pyramid - HTML Form Template
- Python Pyramid - Templates
- Python Pyramid - Route Prefix
- Python Pyramid - View Configuration
- Python Pyramid - Url Routing
- Pyramid - Application Configuration
- Python Pyramid - Hello World
- Pyramid - Environment Setup
- Python Pyramid - Overview
- Python Pyramid - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Pyramid - Logging
In order to collect useful information about the apppcation, Pyramid uses the logging module from Python s standard pbrary. It proves useful in development as well as production mode to detect problems if any, during the running of the apppcation. The apppcation log can include your own messages integrated with messages from third-party modules.
The logged messages have following predefined types (in the order of decreasing severity) −
CRITICAL
ERROR
WARNING
INFO
DEBUG
NOTSET
By default, he logging messages are redirected to sys.stderr stream. To start collecting logging messages, we need to declare a Logger object.
import logging log = logging.getLogger(__name__)
Log messages can now be generated with logger methods corresponding to the desired logging levels. To generate a message which can prove useful for debugging the apppcation, use log.debug() message with appropriate message string.
A Pyramid apppcation based on PasteDeploy configuration makes it very easy to enable incorporate logging support. The PasteDEploy files (development.ini as well as production.ini) use the ConfigParser format used in the logging module s configuration parameters. The logging related sections in development.ini are passed to the logging module s configuration process when it is invoked by pserve command.
Various logger sections in the configuration file specify the keys, formats and the logger levels for the apppcation objects.
Following logging related sections are declared in a typical "development.ini" file −
# Begin logging configuration [loggers] keys = root, hello [logger_hello] level = DEBUG handlers = qualname = hello [handlers] keys = console [formatters] keys = generic [logger_root] #level = INFO level=DEBUG handlers = console [handler_console] class = StreamHandler args = (sys.stderr,) level = NOTSET formatter = generic [formatter_generic] format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s # End logging configuration
Let us add these sections in the development.ini file of our Hello apppcation in the previous chapter.
Example
Next, declare the Logger object and put a debug message in the hello_world() few function. Here s the __init__.py code −
from pyramid.config import Configurator from pyramid.response import Response from pyramid.view import view_config import logging log = logging.getLogger(__name__) from pyramid.renderers import render_to_response def hello_world(request): log.debug( In hello view ) return render_to_response( templates/hello.html , { name :request.matchdict[ name ]},request=request) def main(global_config, **settings): config = Configurator(settings=settings) config.include( pyramid_jinja2 ) config.add_jinja2_renderer(".html") config.add_route( hello , /{name} ) config.add_view(hello_world, route_name= hello ) return config.make_wsgi_app()
The hello_world() view renders the following hello.html template −
<html> <body> <h1>Hello, {{ name }}!</h1> </body> </html>
Run the apppcation as usual −
pserve development.ini
When http://localhost:6543/Tutorialpoint URL is entered in the browser, the command window echoes following debug message −
Starting monitor for PID 11176. Starting server in PID 8472. 2022-06-26 01:22:47,032 INFO [waitress][MainThread] Serving on http://[::1]:6543 2022-06-26 01:22:47,032 INFO [waitress][MainThread] Serving on http://127.0.0.1:6543 2022-06-26 01:22:47,418 DEBUG [hello][waitress-1] In hello view
Output
Since the debug toolbar is enabled in the configuration, it is displayed in the browser −
The debug message is also displayed on the logging tab of the debug toolbar as shown below −
Advertisements