Merge "Use oslo.log in wsgi.py"

This commit is contained in:
Jenkins 2015-03-25 12:54:31 +00:00 committed by Gerrit Code Review
commit ede071510b
2 changed files with 24 additions and 33 deletions

View File

@ -53,7 +53,6 @@ if __name__ == '__main__':
version = version.version_info.version_string() version = version.version_info.version_string()
cfg.CONF(project='heat', prog='heat-api', version=version) cfg.CONF(project='heat', prog='heat-api', version=version)
logging.setup(cfg.CONF, 'heat-api') logging.setup(cfg.CONF, 'heat-api')
logging.set_defaults()
messaging.setup() messaging.setup()
app = config.load_paste_app() app = config.load_paste_app()

View File

@ -22,7 +22,6 @@ Utility methods for working with WSGI servers
import abc import abc
import errno import errno
import logging
import os import os
import signal import signal
import sys import sys
@ -35,6 +34,8 @@ import eventlet.greenio
import eventlet.wsgi import eventlet.wsgi
from oslo_config import cfg from oslo_config import cfg
import oslo_i18n as i18n import oslo_i18n as i18n
from oslo_log import log as logging
from oslo_log import loggers
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
from oslo_utils import importutils from oslo_utils import importutils
from paste import deploy from paste import deploy
@ -53,6 +54,7 @@ from heat.common.i18n import _LW
from heat.common import serializers from heat.common import serializers
LOG = logging.getLogger(__name__)
URL_LENGTH_LIMIT = 50000 URL_LENGTH_LIMIT = 50000
api_opts = [ api_opts = [
@ -171,17 +173,6 @@ def list_opts():
yield 'heat_api_cloudwatch', api_cw_opts yield 'heat_api_cloudwatch', api_cw_opts
class WritableLogger(object):
"""A thin wrapper that responds to `write` and logs."""
def __init__(self, LOG, level=logging.DEBUG):
self.LOG = LOG
self.level = level
def write(self, msg):
self.LOG.log(self.level, msg.strip("\n"))
def get_bind_addr(conf, default_port=None): def get_bind_addr(conf, default_port=None):
"""Return the host and port to bind to.""" """Return the host and port to bind to."""
return (conf.bind_host, conf.bind_port or default_port) return (conf.bind_host, conf.bind_port or default_port)
@ -262,7 +253,7 @@ class Server(object):
""" """
def kill_children(*args): def kill_children(*args):
"""Kills the entire process group.""" """Kills the entire process group."""
self.LOG.error(_LE('SIGTERM received')) LOG.error(_LE('SIGTERM received'))
signal.signal(signal.SIGTERM, signal.SIG_IGN) signal.signal(signal.SIGTERM, signal.SIG_IGN)
self.running = False self.running = False
os.killpg(0, signal.SIGTERM) os.killpg(0, signal.SIGTERM)
@ -271,7 +262,7 @@ class Server(object):
""" """
Shuts down the server(s), but allows running requests to complete Shuts down the server(s), but allows running requests to complete
""" """
self.LOG.error(_LE('SIGHUP received')) LOG.error(_LE('SIGHUP received'))
signal.signal(signal.SIGHUP, signal.SIG_IGN) signal.signal(signal.SIGHUP, signal.SIG_IGN)
os.killpg(0, signal.SIGHUP) os.killpg(0, signal.SIGHUP)
signal.signal(signal.SIGHUP, hup) signal.signal(signal.SIGHUP, hup)
@ -280,7 +271,9 @@ class Server(object):
self.application = application self.application = application
self.sock = get_socket(conf, default_port) self.sock = get_socket(conf, default_port)
self.LOG = logging.getLogger('eventlet.wsgi.server') os.umask(0o27) # ensure files are created with the correct privileges
self._logger = logging.getLogger("eventlet.wsgi.server")
self._wsgi_logger = loggers.WritableLogger(self._logger)
if conf.workers == 0: if conf.workers == 0:
# Useful for profiling, test, debug etc. # Useful for profiling, test, debug etc.
@ -288,7 +281,7 @@ class Server(object):
self.pool.spawn_n(self._single_run, application, self.sock) self.pool.spawn_n(self._single_run, application, self.sock)
return return
self.LOG.info(_LI("Starting %d workers") % conf.workers) LOG.info(_LI("Starting %d workers") % conf.workers)
signal.signal(signal.SIGTERM, kill_children) signal.signal(signal.SIGTERM, kill_children)
signal.signal(signal.SIGHUP, hup) signal.signal(signal.SIGHUP, hup)
while len(self.children) < conf.workers: while len(self.children) < conf.workers:
@ -299,19 +292,19 @@ class Server(object):
try: try:
pid, status = os.wait() pid, status = os.wait()
if os.WIFEXITED(status) or os.WIFSIGNALED(status): if os.WIFEXITED(status) or os.WIFSIGNALED(status):
self.LOG.error(_LE('Removing dead child %s') % pid) LOG.error(_LE('Removing dead child %s') % pid)
self.children.remove(pid) self.children.remove(pid)
self.run_child() self.run_child()
except OSError as err: except OSError as err:
if err.errno not in (errno.EINTR, errno.ECHILD): if err.errno not in (errno.EINTR, errno.ECHILD):
raise raise
except KeyboardInterrupt: except KeyboardInterrupt:
self.LOG.info(_LI('Caught keyboard interrupt. Exiting.')) LOG.info(_LI('Caught keyboard interrupt. Exiting.'))
os.killpg(0, signal.SIGTERM) os.killpg(0, signal.SIGTERM)
break break
eventlet.greenio.shutdown_safe(self.sock) eventlet.greenio.shutdown_safe(self.sock)
self.sock.close() self.sock.close()
self.LOG.debug('Exited') LOG.debug('Exited')
def wait(self): def wait(self):
"""Wait until all servers have completed running.""" """Wait until all servers have completed running."""
@ -329,10 +322,10 @@ class Server(object):
signal.signal(signal.SIGHUP, signal.SIG_DFL) signal.signal(signal.SIGHUP, signal.SIG_DFL)
signal.signal(signal.SIGTERM, signal.SIG_DFL) signal.signal(signal.SIGTERM, signal.SIG_DFL)
self.run_server() self.run_server()
self.LOG.info(_LI('Child %d exiting normally') % os.getpid()) LOG.info(_LI('Child %d exiting normally') % os.getpid())
return return
else: else:
self.LOG.info(_LI('Started child %s') % pid) LOG.info(_LI('Started child %s') % pid)
self.children.append(pid) self.children.append(pid)
def run_server(self): def run_server(self):
@ -346,7 +339,7 @@ class Server(object):
self.application, self.application,
custom_pool=self.pool, custom_pool=self.pool,
url_length_limit=URL_LENGTH_LIMIT, url_length_limit=URL_LENGTH_LIMIT,
log=WritableLogger(self.LOG), log=self._wsgi_logger,
debug=cfg.CONF.debug) debug=cfg.CONF.debug)
except socket.error as err: except socket.error as err:
if err[0] != errno.EINVAL: if err[0] != errno.EINVAL:
@ -355,11 +348,11 @@ class Server(object):
def _single_run(self, application, sock): def _single_run(self, application, sock):
"""Start a WSGI server in a new green thread.""" """Start a WSGI server in a new green thread."""
self.LOG.info(_LI("Starting single process server")) LOG.info(_LI("Starting single process server"))
eventlet.wsgi.server(sock, application, eventlet.wsgi.server(sock, application,
custom_pool=self.pool, custom_pool=self.pool,
url_length_limit=URL_LENGTH_LIMIT, url_length_limit=URL_LENGTH_LIMIT,
log=WritableLogger(self.LOG), log=self._wsgi_logger,
debug=cfg.CONF.debug) debug=cfg.CONF.debug)
@ -650,14 +643,13 @@ class Resource(object):
action, request) action, request)
action_args.update(deserialized_request) action_args.update(deserialized_request)
logging.debug( LOG.debug(('Calling %(controller)s : %(action)s'),
('Calling %(controller)s : %(action)s'), {'controller': self.controller, 'action': action})
{'controller': self.controller, 'action': action})
action_result = self.dispatch(self.controller, action, action_result = self.dispatch(self.controller, action,
request, **action_args) request, **action_args)
except TypeError as err: except TypeError as err:
logging.error(_LE('Exception handling resource: %s') % err) LOG.error(_LE('Exception handling resource: %s') % err)
msg = _('The server could not comply with the request since ' msg = _('The server could not comply with the request since '
'it is either malformed or otherwise incorrect.') 'it is either malformed or otherwise incorrect.')
err = webob.exc.HTTPBadRequest(msg) err = webob.exc.HTTPBadRequest(msg)
@ -678,7 +670,7 @@ class Resource(object):
# error log, disguise or translate those # error log, disguise or translate those
raise raise
if isinstance(err, webob.exc.HTTPServerError): if isinstance(err, webob.exc.HTTPServerError):
logging.error( LOG.error(
_LE("Returning %(code)s to user: %(explanation)s"), _LE("Returning %(code)s to user: %(explanation)s"),
{'code': err.code, 'explanation': err.explanation}) {'code': err.code, 'explanation': err.explanation})
http_exc = translate_exception(err, request.best_match_language()) http_exc = translate_exception(err, request.best_match_language())
@ -718,7 +710,7 @@ class Resource(object):
err_body = action_result.get_unserialized_body() err_body = action_result.get_unserialized_body()
serializer.default(action_result, err_body) serializer.default(action_result, err_body)
except Exception: except Exception:
logging.warning(_LW("Unable to serialize exception " LOG.warning(_LW("Unable to serialize exception "
"response")) "response"))
return action_result return action_result
@ -753,8 +745,8 @@ class Resource(object):
def log_exception(err, exc_info): def log_exception(err, exc_info):
args = {'exc_info': exc_info} if cfg.CONF.verbose or cfg.CONF.debug else {} args = {'exc_info': exc_info} if cfg.CONF.verbose or cfg.CONF.debug else {}
logging.error(_LE("Unexpected error occurred serving API: %s") % err, LOG.error(_LE("Unexpected error occurred serving API: %s") % err,
**args) **args)
def translate_exception(exc, locale): def translate_exception(exc, locale):