Switch to common logging.

I only just moved logging from nova to common, so behavior should remain the same.

Change-Id: I1d7304ca200f9d024bb7244d25be2f9a670318fb
This commit is contained in:
Andrew Bogott
2012-06-28 15:59:23 -05:00
parent 1b8ea1ee86
commit c9a090af26
50 changed files with 71 additions and 740 deletions

View File

@@ -41,8 +41,8 @@ if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")):
from nova import flags
from nova import log as logging
from nova.objectstore import s3server
from nova.openstack.common import log as logging
from nova import service
from nova import utils
from nova.vnc import xvp_proxy
@@ -52,7 +52,7 @@ LOG = logging.getLogger('nova.all')
if __name__ == '__main__':
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
utils.monkey_patch()
launcher = service.ProcessLauncher()

View File

@@ -37,13 +37,13 @@ if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")):
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import service
from nova import utils
if __name__ == '__main__':
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
utils.monkey_patch()
launcher = service.ProcessLauncher()
for api in flags.FLAGS.enabled_apis:

View File

@@ -33,13 +33,13 @@ if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")):
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import service
from nova import utils
if __name__ == '__main__':
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
utils.monkey_patch()
server = service.WSGIService('ec2')
service.serve(server, workers=server.workers)

View File

@@ -33,13 +33,13 @@ if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")):
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import service
from nova import utils
if __name__ == '__main__':
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
utils.monkey_patch()
server = service.WSGIService('metadata')
service.serve(server, workers=server.workers)

View File

@@ -33,13 +33,13 @@ if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")):
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import service
from nova import utils
if __name__ == '__main__':
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
utils.monkey_patch()
server = service.WSGIService('osapi_compute')
service.serve(server, workers=server.workers)

View File

@@ -33,13 +33,13 @@ if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")):
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import service
from nova import utils
if __name__ == '__main__':
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
utils.monkey_patch()
server = service.WSGIService('osapi_volume')
service.serve(server, workers=server.workers)

View File

@@ -33,13 +33,13 @@ if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'nova', '__init__.py')):
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import service
from nova import utils
if __name__ == '__main__':
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
utils.monkey_patch()
server = service.Service.create(binary='nova-cert')
service.serve(server)

View File

@@ -43,8 +43,8 @@ gettext.install('nova', unicode=1)
from nova import context
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import cfg
from nova.openstack.common import log as logging
from nova.openstack.common import rpc
@@ -70,7 +70,7 @@ def delete_queues(queues):
if __name__ == '__main__':
args = flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
delete_queues(args[1:])
if FLAGS.delete_exchange:
delete_exchange(FLAGS.control_exchange)

View File

@@ -35,13 +35,13 @@ if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'nova', '__init__.py')):
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import service
from nova import utils
if __name__ == '__main__':
flags.parse_args(sys.argv)
logging.setup()
logging.setup('nova')
utils.monkey_patch()
server = service.Service.create(binary='nova-compute')
service.serve(server)

View File

@@ -34,12 +34,12 @@ if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import service
if __name__ == '__main__':
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
server = service.Service.create(binary='nova-console')
service.serve(server)
service.wait()

View File

@@ -33,13 +33,13 @@ if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
from nova.consoleauth import manager
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import service
if __name__ == "__main__":
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
server = service.Service.create(binary='nova-consoleauth')
service.serve(server)

View File

@@ -38,9 +38,9 @@ gettext.install('nova', unicode=1)
from nova import context
from nova import db
from nova import flags
from nova import log as logging
from nova.network import linux_net
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
from nova.openstack.common import rpc
from nova import utils
@@ -97,7 +97,7 @@ def main():
"""Parse environment and arguments and call the approproate action."""
flagfile = os.environ.get('FLAGFILE', FLAGS.dhcpbridge_flagfile)
argv = flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
if int(os.environ.get('TESTING', '0')):
from nova.tests import fake_flags

View File

@@ -55,7 +55,7 @@ from nova import context
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova.openstack.common import rpc
from nova import utils
@@ -65,7 +65,7 @@ FLAGS = flags.FLAGS
if __name__ == '__main__':
admin_context = context.get_admin_context()
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
begin, end = utils.last_completed_audit_period()
print "Starting instance usage audit"
print "Creating usages for %s until %s" % (str(begin), str(end))

View File

@@ -86,10 +86,10 @@ from nova import db
from nova.db import migration
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import cfg
from nova.openstack.common import importutils
from nova.openstack.common import jsonutils
from nova.openstack.common import log as logging
from nova.openstack.common import rpc
from nova.openstack.common import timeutils
from nova import quota
@@ -1667,7 +1667,7 @@ def main():
try:
argv = flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
except cfg.ConfigFilesNotFoundError:
cfgfile = FLAGS.config_file[-1] if FLAGS.config_file else None
if cfgfile and not os.access(cfgfile, os.R_OK):

View File

@@ -35,13 +35,13 @@ if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import service
from nova import utils
if __name__ == '__main__':
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
utils.monkey_patch()
server = service.Service.create(binary='nova-network')
service.serve(server)

View File

@@ -35,15 +35,15 @@ if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
from nova import flags
from nova import log as logging
from nova.objectstore import s3server
from nova.openstack.common import log as logging
from nova import service
from nova import utils
if __name__ == '__main__':
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
utils.monkey_patch()
server = s3server.get_wsgi_server()
service.serve(server)

View File

@@ -45,7 +45,7 @@ CONF = cfg.CONF
def main():
CONF.register_opts(rpc.rpc_opts)
impl_zmq.register_opts(CONF)
logging.setup()
logging.setup("nova")
utils.monkey_patch()
ipc_dir = CONF.rpc_zmq_ipc_dir

View File

@@ -37,13 +37,13 @@ if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
gettext.install('nova', unicode=1)
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import service
from nova import utils
if __name__ == '__main__':
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
utils.monkey_patch()
server = service.Service.create(binary='nova-scheduler')
service.serve(server)

View File

@@ -35,13 +35,13 @@ if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import service
from nova import utils
if __name__ == '__main__':
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
utils.monkey_patch()
server = service.Service.create(binary='nova-volume')
service.serve(server)

View File

@@ -54,7 +54,7 @@ from nova import context
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova.openstack.common import rpc
from nova import utils
from nova.volume import utils as volume_utils
@@ -65,7 +65,7 @@ if __name__ == '__main__':
admin_context = context.get_admin_context()
utils.default_cfgfile()
flags.FLAGS(sys.argv)
logging.setup()
logging.setup("nova")
begin, end = utils.last_completed_audit_period()
print "Starting volume usage audit"
print "Creating usages for %s until %s" % (str(begin), str(end))

View File

@@ -32,7 +32,7 @@ if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova.openstack.common import rpc
from nova import service
from nova.vnc import xvp_proxy
@@ -41,7 +41,7 @@ FLAGS = flags.FLAGS
if __name__ == "__main__":
flags.parse_args(sys.argv)
logging.setup()
logging.setup("nova")
wsgi_server = xvp_proxy.get_wsgi_server()
service.serve(wsgi_server)

View File

@@ -29,8 +29,8 @@ import sys
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import cfg
from nova.openstack.common import log as logging
ldap_opts = [

View File

@@ -34,9 +34,9 @@ from nova import crypto
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import cfg
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
from nova import utils

View File

@@ -59,7 +59,7 @@ except ImportError:
boto = None
from nova import exception
from nova import log as logging
from nova.openstack.common import log as logging
LOG = logging.getLogger(__name__)

View File

@@ -16,8 +16,8 @@
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import cfg
from nova.openstack.common import log as logging
LOG = logging.getLogger(__name__)

View File

@@ -73,18 +73,6 @@ def _get_my_ip():
return "127.0.0.1"
log_opts = [
cfg.StrOpt('logdir',
default=None,
help='Log output to a per-service log file in named directory'),
cfg.StrOpt('logfile',
default=None,
help='Log output to a named file'),
cfg.BoolOpt('use_stderr',
default=True,
help='Log output to standard error'),
]
core_opts = [
cfg.StrOpt('connection_type',
default=None,
@@ -125,7 +113,6 @@ debug_opts = [
help='Add python stack traces to SQL as comment strings'),
]
FLAGS.register_cli_opts(log_opts)
FLAGS.register_cli_opts(core_opts)
FLAGS.register_cli_opts(debug_opts)
@@ -273,9 +260,6 @@ global_opts = [
cfg.IntOpt('auth_token_ttl',
default=3600,
help='Seconds for auth tokens to linger'),
cfg.StrOpt('logfile_mode',
default='0644',
help='Default file mode used when creating log files'),
cfg.StrOpt('sqlite_db',
default='nova.sqlite',
help='the filename to use with sqlite'),

View File

@@ -1,436 +0,0 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 OpenStack LLC.
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Nova logging handler.
This module adds to logging functionality by adding the option to specify
a context object when calling the various log methods. If the context object
is not specified, default formatting is used. Additionally, an instance uuid
may be passed as part of the log message, which is intended to make it easier
for admins to find messages related to a specific instance.
It also allows setting of formatting information through flags.
"""
import cStringIO
import inspect
import itertools
import logging
import logging.config
import logging.handlers
import os
import stat
import sys
import traceback
import nova
from nova import flags
from nova.openstack.common import cfg
from nova.openstack.common import jsonutils
from nova.openstack.common import local
from nova import version
log_opts = [
cfg.StrOpt('logging_context_format_string',
default='%(asctime)s %(levelname)s %(name)s [%(request_id)s '
'%(user_id)s %(project_id)s] %(instance)s'
'%(message)s',
help='format string to use for log messages with context'),
cfg.StrOpt('logging_default_format_string',
default='%(asctime)s %(levelname)s %(name)s [-] %(instance)s'
'%(message)s',
help='format string to use for log messages without context'),
cfg.StrOpt('logging_debug_format_suffix',
default='from (pid=%(process)d) %(funcName)s '
'%(pathname)s:%(lineno)d',
help='data to append to log format when level is DEBUG'),
cfg.StrOpt('logging_exception_prefix',
default='%(asctime)s TRACE %(name)s %(instance)s',
help='prefix each line of exception output with this format'),
cfg.ListOpt('default_log_levels',
default=[
'amqplib=WARN',
'sqlalchemy=WARN',
'boto=WARN',
'suds=INFO',
'keystone=INFO',
'eventlet.wsgi.server=WARN'
],
help='list of logger=LEVEL pairs'),
cfg.BoolOpt('publish_errors',
default=False,
help='publish error events'),
# NOTE(mikal): there are two options here because sometimes we are handed
# a full instance (and could include more information), and other times we
# are just handed a UUID for the instance.
cfg.StrOpt('instance_format',
default='[instance: %(uuid)s] ',
help='If an instance is passed with the log message, format '
'it like this'),
cfg.StrOpt('instance_uuid_format',
default='[instance: %(uuid)s] ',
help='If an instance UUID is passed with the log message, '
'format it like this'),
]
FLAGS = flags.FLAGS
FLAGS.register_opts(log_opts)
# our new audit level
# NOTE(jkoelker) Since we synthesized an audit level, make the logging
# module aware of it so it acts like other levels.
logging.AUDIT = logging.INFO + 1
logging.addLevelName(logging.AUDIT, 'AUDIT')
try:
NullHandler = logging.NullHandler
except AttributeError: # NOTE(jkoelker) NullHandler added in Python 2.7
class NullHandler(logging.Handler):
def handle(self, record):
pass
def emit(self, record):
pass
def createLock(self):
self.lock = None
def _dictify_context(context):
if context is None:
return None
if not isinstance(context, dict) and getattr(context, 'to_dict', None):
context = context.to_dict()
return context
def _get_binary_name():
return os.path.basename(inspect.stack()[-1][1])
def _get_log_file_path(binary=None):
logfile = FLAGS.log_file or FLAGS.logfile
logdir = FLAGS.log_dir or FLAGS.logdir
if logfile and not logdir:
return logfile
if logfile and logdir:
return os.path.join(logdir, logfile)
if logdir:
binary = binary or _get_binary_name()
return '%s.log' % (os.path.join(logdir, binary),)
class NovaContextAdapter(logging.LoggerAdapter):
warn = logging.LoggerAdapter.warning
def __init__(self, logger):
self.logger = logger
def audit(self, msg, *args, **kwargs):
self.log(logging.AUDIT, msg, *args, **kwargs)
def process(self, msg, kwargs):
if 'extra' not in kwargs:
kwargs['extra'] = {}
extra = kwargs['extra']
context = kwargs.pop('context', None)
if not context:
context = getattr(local.store, 'context', None)
if context:
extra.update(_dictify_context(context))
instance = kwargs.pop('instance', None)
instance_extra = ''
if instance:
instance_extra = FLAGS.instance_format % instance
else:
instance_uuid = kwargs.pop('instance_uuid', None)
if instance_uuid:
instance_extra = (FLAGS.instance_uuid_format
% {'uuid': instance_uuid})
extra.update({'instance': instance_extra})
extra.update({"nova_version": version.version_string_with_vcs()})
extra['extra'] = extra.copy()
return msg, kwargs
class JSONFormatter(logging.Formatter):
def __init__(self, fmt=None, datefmt=None):
# NOTE(jkoelker) we ignore the fmt argument, but its still there
# since logging.config.fileConfig passes it.
self.datefmt = datefmt
def formatException(self, ei, strip_newlines=True):
lines = traceback.format_exception(*ei)
if strip_newlines:
lines = [itertools.ifilter(lambda x: x,
line.rstrip().splitlines())
for line in lines]
lines = list(itertools.chain(*lines))
return lines
def format(self, record):
message = {'message': record.getMessage(),
'asctime': self.formatTime(record, self.datefmt),
'name': record.name,
'msg': record.msg,
'args': record.args,
'levelname': record.levelname,
'levelno': record.levelno,
'pathname': record.pathname,
'filename': record.filename,
'module': record.module,
'lineno': record.lineno,
'funcname': record.funcName,
'created': record.created,
'msecs': record.msecs,
'relative_created': record.relativeCreated,
'thread': record.thread,
'thread_name': record.threadName,
'process_name': record.processName,
'process': record.process,
'traceback': None}
if hasattr(record, 'extra'):
message['extra'] = record.extra
if record.exc_info:
message['traceback'] = self.formatException(record.exc_info)
return jsonutils.dumps(message)
class LegacyNovaFormatter(logging.Formatter):
"""A nova.context.RequestContext aware formatter configured through flags.
The flags used to set format strings are: logging_context_format_string
and logging_default_format_string. You can also specify
logging_debug_format_suffix to append extra formatting if the log level is
debug.
For information about what variables are available for the formatter see:
http://docs.python.org/library/logging.html#formatter
"""
def format(self, record):
"""Uses contextstring if request_id is set, otherwise default."""
if 'instance' not in record.__dict__:
record.__dict__['instance'] = ''
if record.__dict__.get('request_id', None):
self._fmt = FLAGS.logging_context_format_string
else:
self._fmt = FLAGS.logging_default_format_string
if (record.levelno == logging.DEBUG and
FLAGS.logging_debug_format_suffix):
self._fmt += " " + FLAGS.logging_debug_format_suffix
# Cache this on the record, Logger will respect our formated copy
if record.exc_info:
record.exc_text = self.formatException(record.exc_info, record)
return logging.Formatter.format(self, record)
def formatException(self, exc_info, record=None):
"""Format exception output with FLAGS.logging_exception_prefix."""
if not record:
return logging.Formatter.formatException(self, exc_info)
stringbuffer = cStringIO.StringIO()
traceback.print_exception(exc_info[0], exc_info[1], exc_info[2],
None, stringbuffer)
lines = stringbuffer.getvalue().split('\n')
stringbuffer.close()
if FLAGS.logging_exception_prefix.find('%(asctime)') != -1:
record.asctime = self.formatTime(record, self.datefmt)
formatted_lines = []
for line in lines:
pl = FLAGS.logging_exception_prefix % record.__dict__
fl = '%s%s' % (pl, line)
formatted_lines.append(fl)
return '\n'.join(formatted_lines)
class NovaColorHandler(logging.StreamHandler):
LEVEL_COLORS = {
logging.DEBUG: '\033[00;32m', # GREEN
logging.INFO: '\033[00;36m', # CYAN
logging.AUDIT: '\033[01;36m', # BOLD CYAN
logging.WARN: '\033[01;33m', # BOLD YELLOW
logging.ERROR: '\033[01;31m', # BOLD RED
logging.CRITICAL: '\033[01;31m', # BOLD RED
}
def format(self, record):
record.color = self.LEVEL_COLORS[record.levelno]
return logging.StreamHandler.format(self, record)
class PublishErrorsHandler(logging.Handler):
def emit(self, record):
if 'list_notifier_drivers' in FLAGS:
if 'nova.notifier.log_notifier' in FLAGS.list_notifier_drivers:
return
nova.notifier.api.notify(None, 'nova.error.publisher',
'error_notification',
nova.notifier.api.ERROR,
dict(error=record.msg))
def handle_exception(type, value, tb):
extra = {}
if FLAGS.verbose:
extra['exc_info'] = (type, value, tb)
getLogger().critical(str(value), **extra)
def setup():
"""Setup nova logging."""
sys.excepthook = handle_exception
if FLAGS.log_config:
try:
logging.config.fileConfig(FLAGS.log_config)
except Exception:
traceback.print_exc()
raise
else:
_setup_logging_from_flags()
def _find_facility_from_flags():
facility_names = logging.handlers.SysLogHandler.facility_names
facility = getattr(logging.handlers.SysLogHandler,
FLAGS.syslog_log_facility,
None)
if facility is None and FLAGS.syslog_log_facility in facility_names:
facility = facility_names.get(FLAGS.syslog_log_facility)
if facility is None:
valid_facilities = facility_names.keys()
consts = ['LOG_AUTH', 'LOG_AUTHPRIV', 'LOG_CRON', 'LOG_DAEMON',
'LOG_FTP', 'LOG_KERN', 'LOG_LPR', 'LOG_MAIL', 'LOG_NEWS',
'LOG_AUTH', 'LOG_SYSLOG', 'LOG_USER', 'LOG_UUCP',
'LOG_LOCAL0', 'LOG_LOCAL1', 'LOG_LOCAL2', 'LOG_LOCAL3',
'LOG_LOCAL4', 'LOG_LOCAL5', 'LOG_LOCAL6', 'LOG_LOCAL7']
valid_facilities.extend(consts)
raise TypeError(_('syslog facility must be one of: %s') %
', '.join("'%s'" % fac
for fac in valid_facilities))
return facility
def _setup_logging_from_flags():
nova_root = getLogger().logger
for handler in nova_root.handlers:
nova_root.removeHandler(handler)
if FLAGS.use_syslog:
facility = _find_facility_from_flags()
syslog = logging.handlers.SysLogHandler(address='/dev/log',
facility=facility)
nova_root.addHandler(syslog)
logpath = _get_log_file_path()
if logpath:
filelog = logging.handlers.WatchedFileHandler(logpath)
nova_root.addHandler(filelog)
mode = int(FLAGS.logfile_mode, 8)
st = os.stat(logpath)
if st.st_mode != (stat.S_IFREG | mode):
os.chmod(logpath, mode)
if FLAGS.use_stderr:
streamlog = NovaColorHandler()
nova_root.addHandler(streamlog)
elif not FLAGS.log_file:
# pass sys.stdout as a positional argument
# python2.6 calls the argument strm, in 2.7 it's stream
streamlog = logging.StreamHandler(sys.stdout)
nova_root.addHandler(streamlog)
if FLAGS.publish_errors:
nova_root.addHandler(PublishErrorsHandler(logging.ERROR))
for handler in nova_root.handlers:
datefmt = FLAGS.log_date_format
if FLAGS.log_format:
handler.setFormatter(logging.Formatter(fmt=FLAGS.log_format,
datefmt=datefmt))
handler.setFormatter(LegacyNovaFormatter(datefmt=datefmt))
if FLAGS.verbose or FLAGS.debug:
nova_root.setLevel(logging.DEBUG)
else:
nova_root.setLevel(logging.INFO)
level = logging.NOTSET
for pair in FLAGS.default_log_levels:
mod, _sep, level_name = pair.partition('=')
level = logging.getLevelName(level_name)
logger = logging.getLogger(mod)
logger.setLevel(level)
for handler in nova_root.handlers:
logger.addHandler(handler)
# NOTE(jkoelker) Clear the handlers for the root logger that was setup
# by basicConfig in nova/__init__.py and install the
# NullHandler.
root = logging.getLogger()
for handler in root.handlers:
root.removeHandler(handler)
handler = NullHandler()
handler.setFormatter(logging.Formatter())
root.addHandler(handler)
_loggers = {}
def getLogger(name='nova'):
if name not in _loggers:
_loggers[name] = NovaContextAdapter(logging.getLogger(name))
return _loggers[name]
class WritableLogger(object):
"""A thin wrapper that responds to `write` and logs."""
def __init__(self, logger, level=logging.INFO):
self.logger = logger
self.level = level
def write(self, msg):
self.logger.log(self.level, msg)

View File

@@ -17,10 +17,10 @@ import uuid
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import cfg
from nova.openstack.common import importutils
from nova.openstack.common import jsonutils
from nova.openstack.common import log as logging
from nova.openstack.common import timeutils

View File

@@ -15,7 +15,7 @@
from nova import context
from nova import db
from nova import log as logging
from nova.openstack.common import log as logging
LOG = logging.getLogger(__name__)

View File

@@ -14,9 +14,9 @@
# under the License.
from nova import flags
from nova import log as logging
from nova.openstack.common import cfg
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
list_notifier_drivers_opt = cfg.MultiStrOpt('list_notifier_drivers',

View File

@@ -14,8 +14,8 @@
# under the License.
from nova import flags
from nova import log as logging
from nova.openstack.common import jsonutils
from nova.openstack.common import log as logging
FLAGS = flags.FLAGS

View File

@@ -17,8 +17,8 @@
import nova.context
from nova import flags
from nova import log as logging
from nova.openstack.common import cfg
from nova.openstack.common import log as logging
from nova.openstack.common import rpc
LOG = logging.getLogger(__name__)

View File

@@ -23,8 +23,8 @@ is then selected for provisioning.
"""
from nova import flags
from nova import log as logging
from nova.openstack.common import cfg
from nova.openstack.common import log as logging
LOG = logging.getLogger(__name__)

View File

@@ -14,11 +14,11 @@
# under the License.
import nova
from nova import log as logging
import nova.notifier.api
from nova.notifier import list_notifier
import nova.notifier.log_notifier
import nova.notifier.no_op_notifier
from nova.openstack.common import log as logging
from nova import test

View File

@@ -16,7 +16,7 @@
# under the License.
from nova import exception
from nova import log as logging
from nova.openstack.common import log as logging
from nova import test
from nova.volume import san

View File

@@ -22,7 +22,7 @@ from nova.auth import fakeldap
from nova.auth import manager
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import test
FLAGS = flags.FLAGS

View File

@@ -22,9 +22,9 @@ from nova.compute import utils as compute_utils
from nova import context
from nova import db
from nova import flags
from nova import log as logging
from nova.notifier import test_notifier
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
from nova import test
from nova.tests import fake_network
import nova.tests.image.fake

View File

@@ -29,7 +29,7 @@ from nova import test
from nova.compute import vm_states
from nova import db
from nova import flags
from nova import log
from nova.openstack.common import log
from nova import utils
from nova.virt.libvirt import imagecache
from nova.virt.libvirt import utils as virtutils
@@ -335,10 +335,10 @@ class ImageCacheManagerTestCase(test.TestCase):
@contextlib.contextmanager
def _intercept_log_messages(self):
try:
mylog = log.getLogger()
mylog = log.getLogger('nova')
stream = cStringIO.StringIO()
handler = logging.StreamHandler(stream)
handler.setFormatter(log.LegacyNovaFormatter())
handler.setFormatter(log.LegacyFormatter())
mylog.logger.addHandler(handler)
yield stream
finally:

View File

@@ -24,7 +24,7 @@ from nova.db.sqlalchemy import models
from nova.db.sqlalchemy import session as sql_session
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import test
FLAGS = flags.FLAGS

View File

@@ -34,9 +34,9 @@ from nova import context
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import importutils
from nova.openstack.common import jsonutils
from nova.openstack.common import log as logging
from nova import test
from nova.tests import fake_libvirt_utils
from nova.tests import fake_network

View File

@@ -1,217 +0,0 @@
import cStringIO
import logging
from nova import context
from nova import flags
from nova import log
from nova.notifier import api as notifier
from nova.openstack.common import jsonutils
from nova import test
FLAGS = flags.FLAGS
flags.DECLARE('list_notifier_drivers',
'nova.notifier.list_notifier')
def _fake_context():
return context.RequestContext(1, 1)
class LoggerTestCase(test.TestCase):
def setUp(self):
super(LoggerTestCase, self).setUp()
self.log = log.getLogger()
def test_handlers_have_nova_formatter(self):
formatters = []
for h in self.log.logger.handlers:
f = h.formatter
if isinstance(f, log.LegacyNovaFormatter):
formatters.append(f)
self.assert_(formatters)
self.assertEqual(len(formatters), len(self.log.logger.handlers))
def test_handles_context_kwarg(self):
self.log.info("foo", context=_fake_context())
self.assert_(True) # didn't raise exception
def test_audit_handles_context_arg(self):
self.log.audit("foo", context=_fake_context())
self.assert_(True) # didn't raise exception
def test_will_be_verbose_if_verbose_flag_set(self):
self.flags(verbose=True)
log.setup()
self.assertEqual(logging.DEBUG, self.log.logger.getEffectiveLevel())
def test_will_not_be_verbose_if_verbose_flag_not_set(self):
self.flags(verbose=False)
log.setup()
self.assertEqual(logging.INFO, self.log.logger.getEffectiveLevel())
def test_no_logging_via_module(self):
for func in ('critical', 'error', 'exception', 'warning', 'warn',
'info', 'debug', 'log', 'audit'):
self.assertRaises(AttributeError, getattr, log, func)
class LogHandlerTestCase(test.TestCase):
def test_log_path_logdir(self):
self.flags(logdir='/some/path', logfile=None)
self.assertEquals(log._get_log_file_path(binary='foo-bar'),
'/some/path/foo-bar.log')
def test_log_path_logfile(self):
self.flags(logfile='/some/path/foo-bar.log')
self.assertEquals(log._get_log_file_path(binary='foo-bar'),
'/some/path/foo-bar.log')
def test_log_path_none(self):
self.flags(logdir=None, logfile=None)
self.assertTrue(log._get_log_file_path(binary='foo-bar') is None)
def test_log_path_logfile_overrides_logdir(self):
self.flags(logdir='/some/other/path',
logfile='/some/path/foo-bar.log')
self.assertEquals(log._get_log_file_path(binary='foo-bar'),
'/some/path/foo-bar.log')
class PublishErrorsHandlerTestCase(test.TestCase):
"""Tests for nova.log.PublishErrorsHandler"""
def setUp(self):
super(PublishErrorsHandlerTestCase, self).setUp()
self.publiserrorshandler = log.PublishErrorsHandler(logging.ERROR)
def test_emit_cfg_list_notifier_drivers_in_flags(self):
self.stub_flg = False
def fake_notifier(*args, **kwargs):
self.stub_flg = True
self.stubs.Set(notifier, 'notify', fake_notifier)
logrecord = logging.LogRecord('name', 'WARN', '/tmp', 1,
'Message', None, None)
self.publiserrorshandler.emit(logrecord)
self.assertTrue(self.stub_flg)
def test_emit_cfg_log_notifier_in_list_notifier_drivers(self):
self.flags(list_notifier_drivers=['nova.notifier.rabbit_notifier',
'nova.notifier.log_notifier'])
self.stub_flg = True
def fake_notifier(*args, **kwargs):
self.stub_flg = False
self.stubs.Set(notifier, 'notify', fake_notifier)
logrecord = logging.LogRecord('name', 'WARN', '/tmp', 1,
'Message', None, None)
self.publiserrorshandler.emit(logrecord)
self.assertTrue(self.stub_flg)
class NovaFormatterTestCase(test.TestCase):
def setUp(self):
super(NovaFormatterTestCase, self).setUp()
self.flags(logging_context_format_string="HAS CONTEXT "
"[%(request_id)s]: "
"%(message)s",
logging_default_format_string="NOCTXT: %(message)s",
logging_debug_format_suffix="--DBG")
self.log = log.getLogger()
self.stream = cStringIO.StringIO()
self.handler = logging.StreamHandler(self.stream)
self.handler.setFormatter(log.LegacyNovaFormatter())
self.log.logger.addHandler(self.handler)
self.level = self.log.logger.getEffectiveLevel()
self.log.logger.setLevel(logging.DEBUG)
def tearDown(self):
self.log.logger.setLevel(self.level)
self.log.logger.removeHandler(self.handler)
super(NovaFormatterTestCase, self).tearDown()
def test_uncontextualized_log(self):
self.log.info("foo")
self.assertEqual("NOCTXT: foo\n", self.stream.getvalue())
def test_contextualized_log(self):
ctxt = _fake_context()
self.log.info("bar", context=ctxt)
expected = "HAS CONTEXT [%s]: bar\n" % ctxt.request_id
self.assertEqual(expected, self.stream.getvalue())
def test_debugging_log(self):
self.log.debug("baz")
self.assertEqual("NOCTXT: baz --DBG\n", self.stream.getvalue())
class NovaLoggerTestCase(test.TestCase):
def setUp(self):
super(NovaLoggerTestCase, self).setUp()
levels = FLAGS.default_log_levels
levels.append("nova-test=AUDIT")
self.flags(default_log_levels=levels,
verbose=True)
log.setup()
self.log = log.getLogger('nova-test')
def test_has_level_from_flags(self):
self.assertEqual(logging.AUDIT, self.log.logger.getEffectiveLevel())
def test_child_log_has_level_of_parent_flag(self):
l = log.getLogger('nova-test.foo')
self.assertEqual(logging.AUDIT, l.logger.getEffectiveLevel())
class JSONFormatterTestCase(test.TestCase):
def setUp(self):
super(JSONFormatterTestCase, self).setUp()
self.log = log.getLogger('test-json')
self.stream = cStringIO.StringIO()
handler = logging.StreamHandler(self.stream)
handler.setFormatter(log.JSONFormatter())
self.log.logger.addHandler(handler)
self.log.logger.setLevel(logging.DEBUG)
def test_json(self):
test_msg = 'This is a %(test)s line'
test_data = {'test': 'log'}
self.log.debug(test_msg, test_data)
data = jsonutils.loads(self.stream.getvalue())
self.assertTrue(data)
self.assertTrue('extra' in data)
self.assertEqual('test-json', data['name'])
self.assertEqual(test_msg % test_data, data['message'])
self.assertEqual(test_msg, data['msg'])
self.assertEqual(test_data, data['args'])
self.assertEqual('test_log.py', data['filename'])
self.assertEqual('test_json', data['funcname'])
self.assertEqual('DEBUG', data['levelname'])
self.assertEqual(logging.DEBUG, data['levelno'])
self.assertFalse(data['traceback'])
def test_json_exception(self):
test_msg = 'This is %s'
test_data = 'exceptional'
try:
raise Exception('This is exceptional')
except Exception:
self.log.exception(test_msg, test_data)
data = jsonutils.loads(self.stream.getvalue())
self.assertTrue(data)
self.assertTrue('extra' in data)
self.assertEqual('test-json', data['name'])
self.assertEqual(test_msg % test_data, data['message'])
self.assertEqual(test_msg, data['msg'])
self.assertEqual([test_data], data['args'])
self.assertEqual('ERROR', data['levelname'])
self.assertEqual(logging.ERROR, data['levelno'])
self.assertTrue(data['traceback'])

View File

@@ -35,7 +35,7 @@ import sqlalchemy
import nova.db.migration as migration
import nova.db.sqlalchemy.migrate_repo
from nova.db.sqlalchemy.migration import versioning_api as migration_api
from nova import log as logging
from nova.openstack.common import log as logging
from nova import test

View File

@@ -25,7 +25,7 @@ import StringIO
from lxml import etree
from nova import log as logging
from nova.openstack.common import log as logging
from nova import test
from nova.volume import netapp

View File

@@ -16,9 +16,9 @@
import nova
from nova import context
from nova import flags
from nova import log
from nova.notifier import api as notifier_api
import nova.notifier.no_op_notifier
from nova.openstack.common import log
from nova import test
@@ -106,7 +106,7 @@ class NotifierTestCase(test.TestCase):
'nova.notifier.rabbit_notifier')
self.stubs.Set(nova.flags.FLAGS, 'publish_errors', True)
LOG = log.getLogger('nova')
log.setup()
log.setup('nova')
msgs = []
def mock_notify(context, topic, data):

View File

@@ -21,8 +21,8 @@ import traceback
from nova.compute.manager import ComputeManager
from nova import exception
from nova import log as logging
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
from nova import test
from nova.tests.image import fake as fake_image
from nova.tests import utils as test_utils

View File

@@ -28,9 +28,9 @@ from nova import context
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova.notifier import test_notifier
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
from nova.openstack.common import rpc
import nova.policy
from nova import quota
@@ -430,7 +430,7 @@ class DriverTestCase(test.TestCase):
return self.output, None
self.volume.driver.set_execute(_fake_execute)
log = logging.getLogger()
log = logging.getLogger('nova')
self.stream = cStringIO.StringIO()
log.logger.addHandler(logging.logging.StreamHandler(self.stream))

View File

@@ -23,7 +23,7 @@ from nova.db.sqlalchemy import models
from nova.db.sqlalchemy import session as sql_session
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import test
from nova.volume import volume_types

View File

@@ -20,9 +20,9 @@
from nova import context
from nova import db
from nova import flags
from nova import log as logging
from nova.notifier import test_notifier
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
from nova import test
from nova.volume import utils as volume_utils

View File

@@ -29,8 +29,8 @@ from nova import context
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
from nova.openstack.common import timeutils
from nova import test
from nova.tests.db import fakes as db_fakes

View File

@@ -22,7 +22,7 @@ from nova import context
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova.openstack.common import log as logging
from nova import test
from nova.tests.xenapi import stubs
from nova.virt.xenapi import connection as xenapi_conn