Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use module-level loggers #127

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion brubeck/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import functools
import logging

logger = logging.getLogger(__name__)


###
### Password Helpers
Expand Down Expand Up @@ -78,7 +80,7 @@ def wrapper(self, *args, **kwargs):
return self.redirect(self.application.login_url)
else:
error = 'web_authentication called with undefined <login_url>'
logging.error(error)
logger.error(error)
return self.render_error(self._AUTH_FAILURE)
return method(self, *args, **kwargs)
return wrapper
Expand Down
15 changes: 9 additions & 6 deletions brubeck/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import urlparse
import re

logger = logging.getLogger(__name__)


def parse_netstring(ns):
length, rest = ns.split(':', 1)
length = int(length)
Expand Down Expand Up @@ -70,7 +73,7 @@ def __init__(self, sender, conn_id, path, headers, body, url, *args, **kwargs):
self.files)
break
else:
logging.warning("Invalid multipart/form-data")
logger.warning("Invalid multipart/form-data")

def _parse_mime_body(self, boundary, data, arguments, files):
if boundary.startswith('"') and boundary.endswith('"'):
Expand All @@ -86,7 +89,7 @@ def _parse_mime_body(self, boundary, data, arguments, files):
continue
eoh = part.find("\r\n\r\n")
if eoh == -1:
logging.warning("multipart/form-data missing headers")
logger.warning("multipart/form-data missing headers")
continue
#headers = HTTPHeaders.parse(part[:eoh].decode("utf-8"))
header_string = part[:eoh].decode("utf-8")
Expand All @@ -105,11 +108,11 @@ def _parse_mime_body(self, boundary, data, arguments, files):
disp_header = headers.get("Content-Disposition", "")
disposition, disp_params = self._parse_header(disp_header)
if disposition != "form-data" or not part.endswith("\r\n"):
logging.warning("Invalid multipart/form-data")
logger.warning("Invalid multipart/form-data")
continue
value = part[eoh + 4:-2]
if not disp_params.get("name"):
logging.warning("multipart/form-data value missing name")
logger.warning("multipart/form-data value missing name")
continue
name = disp_params["name"]
if disp_params.get("filename"):
Expand Down Expand Up @@ -177,7 +180,7 @@ def cookies(self):
cookies = self.headers['cookie']
self._cookies.load(to_bytes(cookies))
except Exception, e:
logging.error('Failed to load cookies')
logger.error('Failed to load cookies')
self.clear_all_cookies()
return self._cookies

Expand Down Expand Up @@ -249,7 +252,7 @@ def parse_wsgi_request(environ):

def is_disconnect(self):
if self.headers.get('METHOD') == 'JSON':
logging.error('DISCONNECT')
logger.error('DISCONNECT')
return self.data.get('type') == 'disconnect'

def should_close(self):
Expand Down
27 changes: 11 additions & 16 deletions brubeck/request_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def coro_spawn(function, app, message, *a, **kw):
### Common helpers
###

logger = logging.getLogger(__name__)

HTTP_METHODS = ['get', 'post', 'put', 'delete',
'head', 'options', 'trace', 'connect']

Expand Down Expand Up @@ -338,10 +340,10 @@ def __call__(self):
rendered = fun(*self._url_args)

if rendered is None:
logging.debug('Handler had no return value: %s' % fun)
logger.debug('Handler had no return value: %s' % fun)
return ''
except Exception, e:
logging.error(e, exc_info=True)
logger.error(e, exc_info=True)
rendered = self.error(e)

self._finished = True
Expand Down Expand Up @@ -423,7 +425,7 @@ def error(self, err):
def redirect(self, url):
"""Clears the payload before rendering the error status
"""
logging.debug('Redirecting to url: %s' % url)
logger.debug('Redirecting to url: %s' % url)
self.clear_payload()
self._finished = True
msg = 'Page has moved to %s' % url
Expand Down Expand Up @@ -550,7 +552,7 @@ def render(self, status_code=None, http_200=False, **kwargs):

response = render(self.body, status_code, self.status_msg, self.headers)

logging.info('%s %s %s (%s)' % (status_code, self.message.method,
logger.info('%s %s %s (%s)' % (status_code, self.message.method,
self.message.path,
self.message.remote_addr))
return response
Expand Down Expand Up @@ -579,7 +581,7 @@ def render(self, status_code=None, hide_status=False, **kwargs):
response = render(body, self.status_code, self.status_msg,
self.headers)

logging.info('%s %s %s (%s)' % (self.status_code, self.message.method,
logger.info('%s %s %s (%s)' % (self.status_code, self.message.method,
self.message.path,
self.message.remote_addr))
return response
Expand Down Expand Up @@ -618,9 +620,8 @@ class Brubeck(object):

def __init__(self, msg_conn=None, handler_tuples=None, pool=None,
no_handler=None, base_handler=None, template_loader=None,
log_level=logging.INFO, login_url=None, db_conn=None,
cookie_secret=None, api_base_url=None,
*args, **kwargs):
login_url=None, db_conn=None, cookie_secret=None,
api_base_url=None, *args, **kwargs):
"""Brubeck is a class for managing connections to webservers. It
supports Mongrel2 and WSGI while providing an asynchronous system for
managing message handling.
Expand All @@ -640,21 +641,14 @@ def __init__(self, msg_conn=None, handler_tuples=None, pool=None,
`template_loader` is a function that builds the template loading
environment.

`log_level` is a log level mapping to Python's `logging` module's
levels.

`login_url` is the default URL for a login screen.

`db_conn` is a database connection to be shared in this process

`cookie_secret` is a string to use for signing secure cookies.
"""
# All output is sent via logging
# (while i figure out how to do a good abstraction via zmq)
logging.basicConfig(level=log_level)

# Log whether we're using eventlet or gevent.
logging.info('Using coroutine library: %s' % CORO_LIBRARY)
logger.info('Using coroutine library: %s' % CORO_LIBRARY)

# Attach the web server connection
if msg_conn is not None:
Expand Down Expand Up @@ -860,6 +854,7 @@ def run(self):
still getting the goodness of asynchronous and nonblocking I/O.
"""
greeting = 'Brubeck v%s online ]-----------------------------------'
logger.info(greeting, version)
print greeting % version

self.recv_forever_ever()