Merge "Remove log translations"

This commit is contained in:
Jenkins 2017-07-11 03:46:31 +00:00 committed by Gerrit Code Review
commit ce5926ef0a
17 changed files with 102 additions and 104 deletions

View File

@ -7,3 +7,4 @@ masakari-monitors Specific Commandments
------------------------------
- [M301] Ensure that the _() function is explicitly imported to ensure proper translations.
- [M302] Validate that log messages are not translated.

View File

@ -61,11 +61,9 @@ def _import_modules(module_names):
for modname in module_names:
mod = importlib.import_module("masakarimonitors.conf." + modname)
if not hasattr(mod, LIST_OPTS_FUNC_NAME):
msg = (_("The module 'masakarimonitors.conf.%(modname)s'"
" should have a '%(function)s' function"
" which returns the config options.") % {
'modname': modname,
'function': LIST_OPTS_FUNC_NAME})
msg = "The module 'masakarimonitors.conf.%s' should have a" \
" '%s' function which returns the config options." % \
(modname, LIST_OPTS_FUNC_NAME)
raise Exception(msg)
else:
imported_modules.append(mod)

View File

@ -20,9 +20,6 @@ from oslo_log import log as oslo_logging
from masakariclient.sdk.ha import ha_service
import masakarimonitors.conf
from masakarimonitors.i18n import _LE
from masakarimonitors.i18n import _LI
from masakarimonitors.i18n import _LW
LOG = oslo_logging.getLogger(__name__)
CONF = masakarimonitors.conf.CONF
@ -69,7 +66,7 @@ class SendNotification(object):
:param event: dictionary of event that included in notification.
"""
LOG.info(_LI("Send a notification. %s"), event)
LOG.info("Send a notification. %s", event)
# Get connection.
conn = self._get_connection(
@ -93,7 +90,7 @@ class SendNotification(object):
generated_time=event['notification']['generated_time'],
payload=event['notification']['payload'])
LOG.info(_LI("Response: %s"), response)
LOG.info("Response: %s", response)
break
except Exception as e:
@ -102,13 +99,13 @@ class SendNotification(object):
if e.http_status == 409:
msg = ("Stop retrying to send a notification because "
"same notification have been already sent.")
LOG.info(_LI("%s"), msg)
LOG.info("%s", msg)
break
if retry_count < api_retry_max:
LOG.warning(_LW("Retry sending a notification. (%s)"), e)
LOG.warning("Retry sending a notification. (%s)", e)
retry_count = retry_count + 1
eventlet.greenthread.sleep(api_retry_interval)
else:
LOG.exception(_LE("Exception caught: %s"), e)
LOG.exception("Exception caught: %s", e)
break

View File

@ -41,6 +41,12 @@ underscore_import_check = re.compile(r"(.)*import _(.)*")
underscore_import_check_multi = re.compile(r"(.)*i18n\s+import(.)* _, (.)*")
# We need this for cases where they have created their own _ function.
custom_underscore_check = re.compile(r"(.)*_\s*=\s*(.)*")
log_translation = re.compile(
r"(.)*LOG\."
r"(audit|debug|error|info|warn|warning|critical|exception)"
r"\("
r"(_|_LC|_LE|_LI|_LW)"
r"\(")
def check_explicit_underscore_import(logical_line, filename):
@ -66,5 +72,14 @@ def check_explicit_underscore_import(logical_line, filename):
yield(0, "M301: Found use of _() without explicit import of _ !")
def no_translate_logs(logical_line):
"""Check that logging doesn't translate messages
M302
"""
if log_translation.match(logical_line):
yield(0, "M302 Don't translate log messages!")
def factory(register):
register(check_explicit_underscore_import)
register(no_translate_logs)

View File

@ -18,7 +18,6 @@ from stevedore import driver
from oslo_log import log as oslo_logging
import masakarimonitors.conf
from masakarimonitors.i18n import _LE
from masakarimonitors import manager
LOG = oslo_logging.getLogger(__name__)
@ -48,8 +47,7 @@ class HostmonitorManager(manager.Manager):
)
except Exception as e:
LOG.exception(
_LE("Exception caught during initializing hostmonitor: %s"),
e)
"Exception caught during initializing hostmonitor: %s", e)
os._exit(1)
def stop(self):
@ -63,6 +61,6 @@ class HostmonitorManager(manager.Manager):
self.driver.driver.monitor_hosts()
except Exception as e:
LOG.exception(_LE("Exception caught: %s"), e)
LOG.exception("Exception caught: %s", e)
return

View File

@ -23,9 +23,6 @@ from masakarimonitors.ha import masakari
import masakarimonitors.hostmonitor.host_handler.driver as driver
from masakarimonitors.hostmonitor.host_handler import hold_host_status
from masakarimonitors.hostmonitor.host_handler import parse_cib_xml
from masakarimonitors.i18n import _LE
from masakarimonitors.i18n import _LI
from masakarimonitors.i18n import _LW
from masakarimonitors.objects import event_constants as ec
from masakarimonitors import utils
@ -77,10 +74,10 @@ class HandleHost(driver.DriverBase):
if corosync_status is False or pacemaker_status is False:
if pacemaker_remote_status is False:
LOG.error(
_LE("Neither pacemaker nor pacemaker-remote is running."))
"Neither pacemaker nor pacemaker-remote is running.")
return 2
else:
LOG.info(_LI("Works on pacemaker-remote."))
LOG.info("Works on pacemaker-remote.")
return 0
# Check whether the neccesary parameters are set.
@ -88,7 +85,7 @@ class HandleHost(driver.DriverBase):
CONF.host.corosync_multicast_ports is None:
msg = ("corosync_multicast_interfaces or "
"corosync_multicast_ports is not set.")
LOG.error(_LE("%s"), msg)
LOG.error("%s", msg)
return 2
# Check whether the corosync communication is normal.
@ -100,7 +97,7 @@ class HandleHost(driver.DriverBase):
if len(corosync_multicast_interfaces) != len(corosync_multicast_ports):
msg = ("Incorrect parameters corosync_multicast_interfaces or "
"corosync_multicast_ports.")
LOG.error(_LE("%s"), msg)
LOG.error("%s", msg)
return 2
is_nic_normal = False
@ -118,16 +115,16 @@ class HandleHost(driver.DriverBase):
# If command doesn't raise exception, nic is normal.
msg = ("Corosync communication using '%s' is normal.") \
% corosync_multicast_interfaces[num]
LOG.info(_LI("%s"), msg)
LOG.info("%s", msg)
is_nic_normal = True
break
except Exception:
msg = ("Corosync communication using '%s' is failed.") \
% corosync_multicast_interfaces[num]
LOG.warning(_LW("%s"), msg)
LOG.warning("%s", msg)
if is_nic_normal is False:
LOG.error(_LE("Corosync communication is failed."))
LOG.error("Corosync communication is failed.")
return 1
return 0
@ -151,8 +148,8 @@ class HandleHost(driver.DriverBase):
"crmadmin command output unexpected host status.")
except Exception as e:
LOG.warning(_LW("Exception caught: %s"), e)
LOG.warning(_LW("'%s' is unstable state on cluster."),
LOG.warning("Exception caught: %s", e)
LOG.warning("'%s' is unstable state on cluster.",
self.my_hostname)
return 1
@ -166,7 +163,7 @@ class HandleHost(driver.DriverBase):
raise Exception(msg)
except Exception as e:
LOG.warning(_LW("Exception caught: %s"), e)
LOG.warning("Exception caught: %s", e)
return
return out
@ -174,7 +171,7 @@ class HandleHost(driver.DriverBase):
def _is_poweroff(self, hostname):
ipmi_values = self.xml_parser.get_stonith_ipmi_params(hostname)
if ipmi_values is None:
LOG.error(_LE("Failed to get params of ipmi RA."))
LOG.error("Failed to get params of ipmi RA.")
return False
cmd_str = ("timeout %s ipmitool -U %s -P %s -I %s -H %s "
@ -197,19 +194,18 @@ class HandleHost(driver.DriverBase):
msg = ("ipmitool command output stdout: %s") % out
if 'Power is off' in out:
LOG.info(_LI("%s"), msg)
LOG.info("%s", msg)
return True
else:
raise Exception(msg)
except Exception as e:
if retry_count < CONF.host.ipmi_retry_max:
LOG.warning(_LW("Retry executing ipmitool command. (%s)"),
e)
LOG.warning("Retry executing ipmitool command. (%s)", e)
retry_count = retry_count + 1
eventlet.greenthread.sleep(CONF.host.ipmi_retry_interval)
else:
LOG.error(_LE("Exception caught: %s"), e)
LOG.error("Exception caught: %s", e)
return False
def _make_event(self, hostname, current_status):
@ -271,14 +267,14 @@ class HandleHost(driver.DriverBase):
msg = ("Recognized '%s' as a new member of cluster."
" Host status is '%s'.") \
% (node_state_tag.get('uname'), current_status)
LOG.info(_LI("%s"), msg)
LOG.info("%s", msg)
self.status_holder.set_host_status(node_state_tag)
continue
# Output host status.
msg = ("'%s' is '%s'.") % (node_state_tag.get('uname'),
current_status)
LOG.info(_LI("%s"), msg)
LOG.info("%s", msg)
# If host status changed, send a notification.
if current_status != old_status:
@ -288,7 +284,7 @@ class HandleHost(driver.DriverBase):
msg = ("Since host status is '%s',"
" hostmonitor doesn't send a notification.") \
% current_status
LOG.info(_LI("%s"), msg)
LOG.info("%s", msg)
else:
event = self._make_event(node_state_tag.get('uname'),
current_status)
@ -315,7 +311,7 @@ class HandleHost(driver.DriverBase):
# Check if pacemaker cluster have quorum.
if self.xml_parser.have_quorum() == 0:
msg = "Pacemaker cluster doesn't have quorum."
LOG.warning(_LW("%s"), msg)
LOG.warning("%s", msg)
# Get node_state tag list.
node_state_tag_list = self.xml_parser.get_node_state_tag_list()
@ -350,7 +346,7 @@ class HandleHost(driver.DriverBase):
# brain condition, sleep for a certain time.
eventlet.greenthread.sleep(CONF.host.stonith_wait)
elif ret == 2:
LOG.warning(_LW("hostmonitor skips monitoring hosts."))
LOG.warning("hostmonitor skips monitoring hosts.")
eventlet.greenthread.sleep(CONF.host.monitoring_interval)
continue
@ -361,20 +357,20 @@ class HandleHost(driver.DriverBase):
'pacemaker_remote')
if pacemaker_remote_status is False:
if self._check_host_status_by_crmadmin() != 0:
LOG.warning(_LW("hostmonitor skips monitoring hosts."))
LOG.warning("hostmonitor skips monitoring hosts.")
eventlet.greenthread.sleep(
CONF.host.monitoring_interval)
continue
# Check the host status is online or offline by cibadmin.
if self._check_host_status_by_cibadmin() != 0:
LOG.warning(_LW("hostmonitor skips monitoring hosts."))
LOG.warning("hostmonitor skips monitoring hosts.")
eventlet.greenthread.sleep(CONF.host.monitoring_interval)
continue
eventlet.greenthread.sleep(CONF.host.monitoring_interval)
except Exception as e:
LOG.exception(_LE("Exception caught: %s"), e)
LOG.exception("Exception caught: %s", e)
return

View File

@ -16,8 +16,6 @@ from xml.etree import ElementTree
from oslo_log import log as oslo_logging
from masakarimonitors.i18n import _LE
LOG = oslo_logging.getLogger(__name__)
@ -77,13 +75,13 @@ class ParseCibXml(object):
# Get status tag.
status_tag = self._get_status_tag()
if status_tag is None:
LOG.error(_LE("Cib xml doesn't have status tag."))
LOG.error("Cib xml doesn't have status tag.")
return []
# Get node_state tag list.
node_state_tag_list = self._get_node_states(status_tag)
if len(node_state_tag_list) == 0:
LOG.error(_LE("Cib xml doesn't have node_state tag."))
LOG.error("Cib xml doesn't have node_state tag.")
return node_state_tag_list
@ -157,7 +155,7 @@ class ParseCibXml(object):
configuration_tag = child
break
if configuration_tag is None:
LOG.error(_LE("Cib xml doesn't have configuration tag."))
LOG.error("Cib xml doesn't have configuration tag.")
return None
# Get resources tag from configuration tag.
@ -168,7 +166,7 @@ class ParseCibXml(object):
resources_tag = child
break
if resources_tag is None:
LOG.error(_LE("Cib xml doesn't have resources tag."))
LOG.error("Cib xml doesn't have resources tag.")
return None
# They are set at nvpair tag which exists under the

View File

@ -20,16 +20,6 @@ _translators = oslo_i18n.TranslatorFactory(domain=DOMAIN)
# The primary translation function using the well-known name "_"
_ = _translators.primary
# Translators for log levels.
#
# The abbreviated names are meant to reflect the usual use of a short
# name like '_'. The "L" is for "log" and the other letter comes from
# the level.
_LI = _translators.log_info
_LW = _translators.log_warning
_LE = _translators.log_error
_LC = _translators.log_critical
def translate(value, user_locale):
return oslo_i18n.translate(value, user_locale)

View File

@ -19,7 +19,6 @@ import eventlet
import libvirt
from oslo_log import log as oslo_logging
from masakarimonitors.i18n import _LW
from masakarimonitors.instancemonitor.libvirt_handler import eventfilter
from masakarimonitors import manager
@ -95,7 +94,7 @@ class InstancemonitorManager(manager.Manager):
-1, -1, dom.UUIDString())
def _err_handler(self, ctxt, err):
LOG.warning(_LW("Error from libvirt : %s"), err[2])
LOG.warning("Error from libvirt : %s", err[2])
def _virt_event(self, uri):
# Run a background thread with the event loop
@ -139,7 +138,7 @@ class InstancemonitorManager(manager.Manager):
# If connection between libvirtd was lost,
# clear callback connection.
LOG.warning(_LW("Libvirt Connection Closed Unexpectedly."))
LOG.warning("Libvirt Connection Closed Unexpectedly.")
for cid in callback_ids:
try:
vc.domainEventDeregisterAny(cid)

View File

@ -16,7 +16,6 @@ from oslo_log import log as oslo_logging
import masakarimonitors.conf
from masakarimonitors.ha import masakari
from masakarimonitors.i18n import _LI
LOG = oslo_logging.getLogger(__name__)
@ -47,16 +46,16 @@ class Callback(object):
"""
# Output to the syslog.
LOG.info(_LI("Libvirt Event: type=%(notice_type)s,"
" hostname=%(hostname)s,"
" uuid=%(uuid)s, time=%(current_time)s,"
" event_id=%(event_id)s,"
" detail=%(detail)s)") % {'notice_type': notice_type,
'hostname': hostname,
'uuid': domain_uuid,
'current_time': current_time,
'event_id': event_id,
'detail': details})
LOG.info("Libvirt Event: type=%(notice_type)s,"
" hostname=%(hostname)s,"
" uuid=%(uuid)s, time=%(current_time)s,"
" event_id=%(event_id)s,"
" detail=%(detail)s)" % {'notice_type': notice_type,
'hostname': hostname,
'uuid': domain_uuid,
'current_time': current_time,
'event_id': event_id,
'detail': details})
# Set the event to the dictionary.
event = {

View File

@ -18,7 +18,6 @@ import eventlet
from oslo_log import log as oslo_logging
import masakarimonitors.conf
from masakarimonitors.i18n import _LE
from masakarimonitors import manager
from masakarimonitors.processmonitor.process_handler import handle_process
@ -41,10 +40,10 @@ class ProcessmonitorManager(manager.Manager):
return process_list
except yaml.YAMLError as e:
LOG.exception(_LE("YAMLError caught: %s"), e)
LOG.exception("YAMLError caught: %s", e)
return
except Exception as e:
LOG.exception(_LE("Exception caught: %s"), e)
LOG.exception("Exception caught: %s", e)
return
def stop(self):
@ -57,7 +56,7 @@ class ProcessmonitorManager(manager.Manager):
# Load process list.
process_list = self._load_process_list()
if process_list is None:
LOG.error(_LE("Failed to load process list file."))
LOG.error("Failed to load process list file.")
return
# Set process_list object to the process handler.
@ -81,14 +80,14 @@ class ProcessmonitorManager(manager.Manager):
# Reload process list and set to the process handler.
process_list = self._load_process_list()
if process_list is None:
LOG.error(_LE("Failed to reload process list file."))
LOG.error("Failed to reload process list file.")
break
self.process_handler.set_process_list(process_list)
eventlet.greenthread.sleep(CONF.process.check_interval)
except Exception as e:
LOG.exception(_LE("Exception caught: %s"), e)
LOG.exception("Exception caught: %s", e)
return
return

View File

@ -20,9 +20,6 @@ from oslo_utils import timeutils
import masakarimonitors.conf
from masakarimonitors.ha import masakari
from masakarimonitors.i18n import _LE
from masakarimonitors.i18n import _LI
from masakarimonitors.i18n import _LW
from masakarimonitors.objects import event_constants as ec
from masakarimonitors import utils
@ -58,16 +55,16 @@ class HandleProcess(object):
if out:
msg = ("CMD '%s' output stdout: %s") % (cmd_str, out)
LOG.info(_LI("%s"), msg)
LOG.info("%s", msg)
if err:
msg = ("CMD '%s' output stderr: %s") % (cmd_str, err)
LOG.warning(_LW("%s"), msg)
LOG.warning("%s", msg)
return 1
except Exception as e:
msg = ("CMD '%s' raised exception: %s") % (cmd_str, e)
LOG.error(_LE("%s"), e)
LOG.error("%s", e)
return 1
return 0
@ -90,8 +87,7 @@ class HandleProcess(object):
continue
# Execute start command.
LOG.info(
_LI("Start of process with executing command: %s"), cmd_str)
LOG.info("Start of process with executing command: %s", cmd_str)
self._execute_cmd(cmd_str, process['run_as_root'])
# Execute post start command.
@ -118,10 +114,9 @@ class HandleProcess(object):
else:
# Append down_process_list.
down_process_list.append(process)
LOG.warning(
_LW("Process '%s' is not found."), process_name)
LOG.warning("Process '%s' is not found.", process_name)
except Exception as e:
LOG.error(_LW("Monitoring command raised exception: %s"), e)
LOG.error("Monitoring command raised exception: %s", e)
return down_process_list
@ -157,7 +152,7 @@ class HandleProcess(object):
if down_process['process_name'] in self.restart_failure_list:
msg = "Process '%s' doesn't be restarted because it failed" \
" to restart previously." % down_process['process_name']
LOG.warning(_LW("%s"), msg)
LOG.warning("%s", msg)
tmp_restart_failure_list.append(down_process['process_name'])
continue
@ -165,8 +160,7 @@ class HandleProcess(object):
pre_cmd_str = down_process['pre_restart_command']
post_cmd_str = down_process['post_restart_command']
LOG.info(
_LI("Restart of process with executing command: %s"), cmd_str)
LOG.info("Restart of process with executing command: %s", cmd_str)
for retries in range(0, CONF.process.restart_retries + 1):

View File

@ -22,7 +22,7 @@ from oslo_service import service
from oslo_utils import importutils
import masakarimonitors.conf
from masakarimonitors.i18n import _, _LE, _LI
from masakarimonitors.i18n import _
from masakarimonitors import utils
@ -56,7 +56,7 @@ class Service(service.Service):
}
def start(self):
LOG.info(_LI('Starting %s'), self.binary)
LOG.info('Starting %s', self.binary)
self.basic_config_check()
self.manager.init_host()
self.manager.main()
@ -99,7 +99,7 @@ class Service(service.Service):
self.stop()
def stop(self):
LOG.info(_LI('Stopping %s'), self.binary)
LOG.info('Stopping %s', self.binary)
self.manager.stop()
super(Service, self).stop()
@ -110,7 +110,7 @@ class Service(service.Service):
with utils.tempdir():
pass
except Exception as e:
LOG.error(_LE('Temporary directory is invalid: %s'), e)
LOG.error('Temporary directory is invalid: %s', e)
sys.exit(1)
def reset(self):

View File

@ -66,7 +66,7 @@ class HackingTestCase(testtools.TestCase):
"msg = _('My message')",
"masakarimonitors/tests/other_files.py"))), 0)
self.assertEqual(len(list(checks.check_explicit_underscore_import(
"from masakarimonitors.i18n import _, _LW",
"from masakarimonitors.i18n import _",
"masakarimonitors/tests/other_files2.py"))), 0)
self.assertEqual(len(list(checks.check_explicit_underscore_import(
"msg = _('My message')",
@ -77,3 +77,19 @@ class HackingTestCase(testtools.TestCase):
self.assertEqual(len(list(checks.check_explicit_underscore_import(
"msg = _('My message')",
"masakarimonitors/tests/other_files3.py"))), 0)
def test_no_translate_logs(self):
self.assertEqual(1, len(list(checks.no_translate_logs(
"LOG.error(_LE('foo'))"))))
self.assertEqual(0, len(list(checks.no_translate_logs(
"LOG.debug('foo')"))))
self.assertEqual(1, len(list(checks.no_translate_logs(
"LOG.info(_LI('foo'))"))))
self.assertEqual(1, len(list(checks.no_translate_logs(
"LOG.warning(_LW('foo'))"))))
self.assertEqual(1, len(list(checks.no_translate_logs(
"LOG.critical(_LC('foo'))"))))

View File

@ -27,7 +27,7 @@ from oslo_utils import importutils
import six
import masakarimonitors.conf
from masakarimonitors.i18n import _, _LE
from masakarimonitors.i18n import _
from masakarimonitors import privsep
@ -97,7 +97,7 @@ def tempdir(**kwargs):
try:
shutil.rmtree(tmpdir)
except OSError as e:
LOG.error(_LE('Could not remove tmpdir: %s'), e)
LOG.error('Could not remove tmpdir: %s', e)
@privsep.monitors_priv.entrypoint

View File

@ -14,8 +14,6 @@
from pbr import version as pbr_version
from masakarimonitors.i18n import _LE
MONITORS_VENDOR = "OpenStack Foundation"
MONITORS_PRODUCT = "OpenStack Masakari Monitors"
MONITORS_PACKAGE = None # OS distro package version suffix
@ -59,7 +57,7 @@ def _load_config():
MONITORS_PACKAGE = cfg.get("Masakarimonitors", "package")
except Exception as ex:
LOG = logging.getLogger(__name__)
LOG.error(_LE("Failed to load %(cfgfile)s: %(ex)s"),
LOG.error("Failed to load %(cfgfile)s: %(ex)s",
{'cfgfile': cfgfile, 'ex': ex})

View File

@ -48,4 +48,4 @@ exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build
[hacking]
import_exceptions = masakarimonitors.i18n
local-check-factory = masakarimonitors.hacking.checks.factory