简体   繁体   中英

How to set up logging for a Python Pyramid Waitress server?

I am trying to setup logging for a Python Pyramid Waitress Server. I have followed the docs here: Pyramid logging and here: Pyramid PasteDeploy logging . I have tired both methods which have yield no logging results from waitress. My own logging works perfectly.

I have set Waitress logging level to DEBUG and I get nothing even I remove server files. Waitress fails server silently.

How do you set up logging for a Pyramid Waitress Server so I can see files be requested, missing file errors, etc?

Method 1: Setup from code:

import logging
logging.basicConfig()
logger = logging.getLogger('waitress')
logger.setLevel(logging.DEBUG)

Method 2: Starting the server with pserve development.ini where the development.ini file sets up the logging as below

[app:main]
use = egg:MyProject

pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
pyramid.includes =
pyramid_debugtoolbar

[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6543



[loggers]
keys = root, myproject, waitress

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = INFO
handlers = console

[logger_myproject]
level = DEBUG
handlers =
qualname = myproject


[logger_waitress]
level = DEBUG
handlers =
qualname = waitress


[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

The logging configuration actually works. Here I demonstrate a simple view to emit logging message for waitress logger

@view_config(route_name='hello_baby', 
             request_method='GET', 
             renderer='string')
def hello_baby(request):
    import logging
    logger = logging.getLogger('waitress')
    logger.info('Hello baby!')
    return 'Hi there'

You should be able to see the logging message when you hit the page. The reason you didn't see messages from waitress is - there is no logging messages are emitted for common routines in waitress . It only emits messages when something goes wrong, you can read the source code

For some other knowledge about Python logging, you can read my article : Good logging practice in Python

I got console logging (for all requests) to show up by using paste's translogger; A good example is at http://flask.pocoo.org/snippets/27/ .

Here's the relevant section of my .ini:

[app:main]
use = egg:${:app}
filter-with = translogger

[filter:translogger]
use = egg:Paste#translogger
# these are the option default values (see http://pythonpaste.org/modules/translogger.html)
# logger_name='wsgi'
# format=None
# logging_level=20
# setup_console_handler=True
# set_logger_level=10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM