Re: [pylons-devel] Setting translogger format via ini

2016-03-11 Thread Mike Orr
Here's the code. You can see it's short and simple; half the code is
just to make it more configurable. Usage:

pyramid_includes = accesslog

# Settings, all optional
access.format = {response.status_int} {request.method} {request.path_qs}
access.ignore =  /fanstatic /_debug_toolbar
access.ignore_usual = true



On Thu, Mar 10, 2016 at 6:46 PM, Mike Orr  wrote:
> On Thu, Mar 10, 2016 at 3:32 AM, Zsolt Ero  wrote:
>> Hi,
>>
>> My first mailing list post, as this really puzzles me. I'd like to use a
>> short format for displaying HTTP requests in development mode.
>>
>> Before, I used to have this snippet in my Pyramid app's __init__.py:
>>
>> from paste.translogger import TransLogger
>> format = '%(status)s %(REQUEST_METHOD)s %(REQUEST_URI)s'
>> app = TransLogger(app, format=format)
>> return app
>>
>>
>> I'd like to turn this into using an .ini file, however, I cannot specify the
>> format, as the .ini file syntax needs some kind of escaping, which I cannot
>> figure out.
>>
>> Here is a try on setting translogger format via .ini file:
>>
>> [filter:translogger]
>> use = egg:Paste#translogger
>> setup_console_handler = False
>> format = %%(status)s %%(REQUEST_METHOD)s %%(REQUEST_URI)s
>>
>>
>>
>> It results in an error:
>>
>> ConfigParser.InterpolationMissingOptionError: Error in file
>> .../development.ini: Bad value substitution:
>>  section: [filter:translogger]
>>  option : format
>>  key : status
>>  rawval : %%(status)s
>>
>>
>> What is the right escaping method to set translogger's format via .ini file?
>>
>> Or if it's not possible to escape it, then I don't get it. Why does
>> translogger has a format option, if you cannot actually enter anything there
>> via ini?
>>
>> Alembic, for example supports escaping perfectly fine, for example:
>>
>> file_template =
>> %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d_%%(minute).2d_%%(second).2d_%%(rev)s
>>
>>
>> Here is this 4 year old issue in PasteDeploy, I think it's related:
>>
>> https://bitbucket.org/ianb/pastedeploy/issues/11/there-is-no-way-to-escape-character-in-the
>>
>> Any ideas how to solve this?
>
> I didn't like using middleware so I made a Pyramid tween that logs
> requests. I don't have the code with me but I can get it tomorrow.
> After calling the applicaiton I make a log message based on the
> response and request. I probably bypassed the % parsing problem by
> using str.format instead, so that I could override the format. My
> logger is called 'access'.
>
>
> --
> Mike Orr 



-- 
Mike Orr 

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-devel+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/pylons-devel.
For more options, visit https://groups.google.com/d/optout.
import logging

from pyramid.settings import asbool, aslist

FORMAT = "{response.status_int} {request.method} {request.path_qs}"
IGNORE_USUAL = [
"/_debug_toolbar",
"/favicon.ico",
"/robots.txt",
"/static",
"/w3c",
]

def includeme(config):
settings = config.registry.settings
if asbool(settings.get("access.enabled", True)):
config.add_tween("accesslog.accesslog_tween_factory")

def accesslog_tween_factory(handler, registry):
settings = registry.settings
log = logging.getLogger("access")
fmt = settings.get("access.format", FORMAT)
ignore = aslist(settings.get("access.ignore", []))
if asbool(settings.get("access.ignore_usual", True)):
ignore.extend(IGNORE_USUAL)
log.debug("Enabling access log ({}).".format(__name__))
if ignore:
log.debug("Ignoring prefixes: {}".format(ignore))
def accesslog_tween(request):
response = handler(request)   # Raises exceptions.
for prefix in ignore:
if request.path.startswith(prefix):
break
else:
msg = fmt.format(request=request, response=response)
log.info(msg)
return response
return accesslog_tween



Re: [pylons-devel] Setting translogger format via ini

2016-03-10 Thread Mike Orr
On Thu, Mar 10, 2016 at 3:32 AM, Zsolt Ero  wrote:
> Hi,
>
> My first mailing list post, as this really puzzles me. I'd like to use a
> short format for displaying HTTP requests in development mode.
>
> Before, I used to have this snippet in my Pyramid app's __init__.py:
>
> from paste.translogger import TransLogger
> format = '%(status)s %(REQUEST_METHOD)s %(REQUEST_URI)s'
> app = TransLogger(app, format=format)
> return app
>
>
> I'd like to turn this into using an .ini file, however, I cannot specify the
> format, as the .ini file syntax needs some kind of escaping, which I cannot
> figure out.
>
> Here is a try on setting translogger format via .ini file:
>
> [filter:translogger]
> use = egg:Paste#translogger
> setup_console_handler = False
> format = %%(status)s %%(REQUEST_METHOD)s %%(REQUEST_URI)s
>
>
>
> It results in an error:
>
> ConfigParser.InterpolationMissingOptionError: Error in file
> .../development.ini: Bad value substitution:
>  section: [filter:translogger]
>  option : format
>  key : status
>  rawval : %%(status)s
>
>
> What is the right escaping method to set translogger's format via .ini file?
>
> Or if it's not possible to escape it, then I don't get it. Why does
> translogger has a format option, if you cannot actually enter anything there
> via ini?
>
> Alembic, for example supports escaping perfectly fine, for example:
>
> file_template =
> %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d_%%(minute).2d_%%(second).2d_%%(rev)s
>
>
> Here is this 4 year old issue in PasteDeploy, I think it's related:
>
> https://bitbucket.org/ianb/pastedeploy/issues/11/there-is-no-way-to-escape-character-in-the
>
> Any ideas how to solve this?

I didn't like using middleware so I made a Pyramid tween that logs
requests. I don't have the code with me but I can get it tomorrow.
After calling the applicaiton I make a log message based on the
response and request. I probably bypassed the % parsing problem by
using str.format instead, so that I could override the format. My
logger is called 'access'.


-- 
Mike Orr 

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-devel+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/pylons-devel.
For more options, visit https://groups.google.com/d/optout.