2012-07-03 19 views
5

yo probamos este:¿Cómo depurar/registrar la aplicación wsgi python?

#!/usr/bin/python  

from wsgiref.simple_server import make_server 
from cgi import parse_qs, escape 
import logging 
import os 
import sys 

html = """ 
<html> 
<body> 
    <form method="post" action="parsing_post.wsgi"> 
     <p> 
     Age: <input type="text" name="age"> 
     </p> 
     <p> 
     Hobbies: 
     <input name="hobbies" type="checkbox" value="software"> Software 
     <input name="hobbies" type="checkbox" value="tunning"> Auto Tunning 
     </p> 
     <p> 
     <input type="submit" value="Submit"> 
     </p> 
     </form> 
    <p> 
     Age: %s<br> 
     Hobbies: %s 
     </p> 
    </body> 
</html> 
""" 

def application(environ, start_response): 

    # the environment variable CONTENT_LENGTH may be empty or missing 
    try: 
     request_body_size = int(environ.get('CONTENT_LENGTH', 0)) 
    except (ValueError): 
     request_body_size = 0 

    # When the method is POST the query string will be sent 
    # in the HTTP request body which is passed by the WSGI server 
    # in the file like wsgi.input environment variable. 
    logger = logging.getLogger(__name__) 
    request_body = environ['wsgi.input'].read(request_body_size) 
    d = parse_qs(request_body) 


    age = d.get('age', [''])[0] # Returns the first age value. 
    hobbies = d.get('hobbies', []) # Returns a list of hobbies. 

    # Always escape user input to avoid script injection 
    age = escape(age) 
    hobbies = [escape(hobby) for hobby in hobbies] 

    response_body = html % (age or 'Empty', 
       ', '.join(hobbies or ['No Hobbies'])) 

    status = '200 OK' 

    response_headers = [('Content-Type', 'text/html'), 
        ('Content-Length', str(len(response_body)))] 
    start_response(status, response_headers) 

    return [response_body] 

Pero yo no sé donde registra. Estoy tratando de mostrar/registrar el valor en la página web o en un archivo /var/log/apache2/myapp.log

¿Cuál es la mejor manera de hacerlo?

Cualquier respuesta será muy apreciada. Gracias de antemano.

+0

Consulte https://code.google.com/p/modwsgi/wiki/DebuggingTechniques –

Respuesta

3

Tenga en cuenta que el código anterior no producirá ningún registro ya que no llama a ninguna de las variantes logger.log(), pero supongo que ese no es el punto.

Si está ejecutando su código con apache/mod_wsgi, la solución más simple es configurar su registrador (es) para iniciar sesión en sys.stderr utilizando un StreamHandler (cf http://docs.python.org/howto/logging.html#configuring-logging) y definir la ruta del registro de errores, nombre y nivel en su conf de apache (cuidado, el comportamiento de apache predeterminado es solo registrar el mensaje de "nivel de error").

+2

y https://code.google.com/p/modwsgi/wiki/DebuggingTechniques le indica cómo aumentar el nivel de registro. –

Cuestiones relacionadas