diff options
| author | Andrew Laski <andrew.laski@rackspace.com> | 2013-05-03 10:33:18 -0400 |
|---|---|---|
| committer | Andrew Laski <andrew.laski@rackspace.com> | 2013-05-03 15:11:16 -0400 |
| commit | 3e136dea2b259def305c9032cab5e1a08243c1c6 (patch) | |
| tree | 42ee307d01d689722051520f422b9fb729e8685e /nova/openstack | |
| parent | 00daebb7d12e6b78cc77f9d6eaaa19e940b21b38 (diff) | |
| download | nova-3e136dea2b259def305c9032cab5e1a08243c1c6.tar.gz nova-3e136dea2b259def305c9032cab5e1a08243c1c6.tar.xz nova-3e136dea2b259def305c9032cab5e1a08243c1c6.zip | |
Import latest log module from oslo
Deprecates log_format in favor of the formatting options used by the
context aware formatter, such as logging_default_format_string.
Changes the default logging formatter back to a colorful and context
aware formatter. The default was inadvertently changed in the great
oslo sync at the start of Havana, this returns it to what was in at the
end of Grizzly.
Adds support for lazy instantiation of the logger.
Bug 1167081
Bug 1165211
Change-Id: Ibe41afc6e9d7b432453785819821b3c8f0078613
Diffstat (limited to 'nova/openstack')
| -rw-r--r-- | nova/openstack/common/log.py | 66 |
1 files changed, 46 insertions, 20 deletions
diff --git a/nova/openstack/common/log.py b/nova/openstack/common/log.py index 49dbadebe..e11f666fc 100644 --- a/nova/openstack/common/log.py +++ b/nova/openstack/common/log.py @@ -37,7 +37,6 @@ import logging import logging.config import logging.handlers import os -import stat import sys import traceback @@ -49,7 +48,6 @@ from nova.openstack.common import local from nova.openstack.common import notifier -_DEFAULT_LOG_FORMAT = "%(asctime)s %(levelname)8s [%(name)s] %(message)s" _DEFAULT_LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S" common_cli_opts = [ @@ -74,11 +72,13 @@ logging_cli_opts = [ 'documentation for details on logging configuration ' 'files.'), cfg.StrOpt('log-format', - default=_DEFAULT_LOG_FORMAT, + default=None, metavar='FORMAT', help='A logging.Formatter log message format string which may ' 'use any of the available logging.LogRecord attributes. ' - 'Default: %(default)s'), + 'This option is deprecated. Please use ' + 'logging_context_format_string and ' + 'logging_default_format_string instead.'), cfg.StrOpt('log-date-format', default=_DEFAULT_LOG_DATE_FORMAT, metavar='DATE_FORMAT', @@ -104,10 +104,7 @@ logging_cli_opts = [ generic_log_opts = [ cfg.BoolOpt('use_stderr', default=True, - help='Log output to standard error'), - cfg.StrOpt('logfile_mode', - default='0644', - help='Default file mode used when creating log files'), + help='Log output to standard error') ] log_opts = [ @@ -211,7 +208,27 @@ def _get_log_file_path(binary=None): return '%s.log' % (os.path.join(logdir, binary),) -class ContextAdapter(logging.LoggerAdapter): +class BaseLoggerAdapter(logging.LoggerAdapter): + + def audit(self, msg, *args, **kwargs): + self.log(logging.AUDIT, msg, *args, **kwargs) + + +class LazyAdapter(BaseLoggerAdapter): + def __init__(self, name='unknown', version='unknown'): + self._logger = None + self.extra = {} + self.name = name + self.version = version + + @property + def logger(self): + if not self._logger: + self._logger = getLogger(self.name, self.version) + return self._logger + + +class ContextAdapter(BaseLoggerAdapter): warn = logging.LoggerAdapter.warning def __init__(self, logger, project_name, version_string): @@ -219,8 +236,9 @@ class ContextAdapter(logging.LoggerAdapter): self.project = project_name self.version = version_string - def audit(self, msg, *args, **kwargs): - self.log(logging.AUDIT, msg, *args, **kwargs) + @property + def handlers(self): + return self.logger.handlers def deprecated(self, msg, *args, **kwargs): stdmsg = _("Deprecated: %s") % msg @@ -340,7 +358,7 @@ class LogConfigError(Exception): def _load_log_config(log_config): try: logging.config.fileConfig(log_config) - except ConfigParser.Error, exc: + except ConfigParser.Error as exc: raise LogConfigError(log_config, str(exc)) @@ -399,11 +417,6 @@ def _setup_logging_from_conf(): filelog = logging.handlers.WatchedFileHandler(logpath) log_root.addHandler(filelog) - mode = int(CONF.logfile_mode, 8) - st = os.stat(logpath) - if st.st_mode != (stat.S_IFREG | mode): - os.chmod(logpath, mode) - if CONF.use_stderr: streamlog = ColorHandler() log_root.addHandler(streamlog) @@ -417,13 +430,17 @@ def _setup_logging_from_conf(): if CONF.publish_errors: log_root.addHandler(PublishErrorsHandler(logging.ERROR)) + datefmt = CONF.log_date_format for handler in log_root.handlers: - datefmt = CONF.log_date_format + # NOTE(alaski): CONF.log_format overrides everything currently. This + # should be deprecated in favor of context aware formatting. if CONF.log_format: handler.setFormatter(logging.Formatter(fmt=CONF.log_format, datefmt=datefmt)) + log_root.info('Deprecated: log_format is now deprecated and will ' + 'be removed in the next release') else: - handler.setFormatter(LegacyFormatter(datefmt=datefmt)) + handler.setFormatter(ContextFormatter(datefmt=datefmt)) if CONF.debug: log_root.setLevel(logging.DEBUG) @@ -449,6 +466,15 @@ def getLogger(name='unknown', version='unknown'): return _loggers[name] +def getLazyLogger(name='unknown', version='unknown'): + """ + create a pass-through logger that does not create the real logger + until it is really needed and delegates all calls to the real logger + once it is created + """ + return LazyAdapter(name, version) + + class WritableLogger(object): """A thin wrapper that responds to `write` and logs.""" @@ -460,7 +486,7 @@ class WritableLogger(object): self.logger.log(self.level, msg) -class LegacyFormatter(logging.Formatter): +class ContextFormatter(logging.Formatter): """A context.RequestContext aware formatter configured through flags. The flags used to set format strings are: logging_context_format_string |
