Change the request ids to use urns. Add the logger name to the log formatter.

This commit is contained in:
Jake Moshenko 2014-05-02 13:43:57 -04:00
parent 4da49da730
commit 4556ff52b3
3 changed files with 18 additions and 3 deletions

View file

@ -4,6 +4,7 @@ import uuid
from app import app as application
from flask import request, Request
from util.names import urn_generator
from data.model import db as model_db
@ -33,9 +34,11 @@ application.register_blueprint(webhooks, url_prefix='/webhooks')
application.register_blueprint(realtime, url_prefix='/realtime')
class RequestWithId(Request):
request_gen = staticmethod(urn_generator(['request']))
def __init__(self, *args, **kwargs):
super(RequestWithId, self).__init__(*args, **kwargs)
self.request_id = uuid.uuid4()
self.request_id = self.request_gen()
@application.before_request
def _request_start():
@ -49,7 +52,7 @@ def _request_end(r):
class InjectingFilter(logging.Filter):
def filter(self, record):
record.msg = '[request_id = %s] %s' % (request.request_id, record.msg)
record.msg = '[%s] %s' % (request.request_id, record.msg)
return True
profile.addFilter(InjectingFilter())

View file

@ -35,5 +35,5 @@ formatter=generic
args=(sys.stdout, )
[formatter_generic]
format=%(asctime)s [%(process)d] [%(levelname)s] %(message)s
format=%(asctime)s [%(process)d] [%(levelname)s] [%(name)s] %(message)s
class=logging.Formatter

View file

@ -1,6 +1,7 @@
import urllib
from functools import wraps
from uuid import uuid4
def parse_namespace_repository(repository):
@ -25,8 +26,19 @@ def parse_repository_name(f):
def format_robot_username(parent_username, robot_shortname):
return '%s+%s' % (parent_username, robot_shortname)
def parse_robot_username(robot_username):
if not '+' in robot_username:
return None
return robot_username.split('+', 2)
uuid_generator = lambda: str(uuid4())
def urn_generator(namespace_portions, id_generator=uuid_generator):
prefix = 'urn:%s:' % ':'.join(namespace_portions)
def generate_urn():
return prefix + id_generator()
return generate_urn