How to setup a 'catch all' view in Python Pyramid to log incoming requests for static files?
Question: For debugging purposes I'm trying to see what static files(css, js, jpg, etc)
files are being requested from my html files. I read the docs here: [enter
link description
here](http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/assets.html)
I have set up my configuration like so:

    
    
    config.add_route('catchall_static', 'static/*subpath')
    

and my view as:

    
    
    @view_config(route_name='catchall_static')
    path_info = request._headers.environ['PATH_INFO']
    log.debug('path info = {0} {1}'.format(path_info, query_string))
    return request.response
    

With this code there are several things going on.

1) Although the static files come thru the view they don't get actually loaded
to the browser 2) When the static files come thru the view my code works at
least for the logging, but 50% of the time I get this error:

Traceback: path_info = request._headers.environ['PATH_INFO'] AttributeError:
'NoneType' object has no attribute 'environ'

The errors seem to be concentrated on the eariler 's in my html file such as
css files while the .js file at the bottom of my html file sometimes work.

So basically I have no idea if I'm close or totally went in the wrong
direction to solve the problem. Does anybody know the correct way of doing it?


Answer: Are you sure, this route servers any request successfully at all?
Possibilities to achieve your goal are: A WSGI middleware, a Pyramid Tween or
a custom event subscriber.

The custom subscriber would probably be the most easy one. This subscriber is
called before the response is created, so it can't know the response code,
content etc. To accomplish this you can either add a request finished callback
or use one of the other methods.

    
    
    from pyramid.events import NewRequest
    from pyramid.events import subscriber
    
    @subscriber(NewRequest)
    def static_logger(event):
        logger = logging.getLogger('static')
        request = event.request
    
        if request.path_info.startswith('/static'):
            logger.info('static request: {} {}'.format(request.path_info,
                                                       request.query_string))
    

And activate the logger in development.ini:

    
    
    [loggers]
    keys = root, static
    
    
    [logger_static]
    level = DEBUG
    handlers = console
    qualname = static
    

