def warning_with_traceback(): LOG.debug("Demonstrating the use of _LW with traceback for warning") try: file_name = ".does.not.exist" open(file_name, "r") except Exception: LOG.warn(_LW("Unable to open file"), exc_info=True)
def warning_multi(): LOG.debug("Demonstrating the use of positional parameters with _LW") try: dir_name = "dir.does.not.exist" file_name = ".does.not.exist" open(os.path.join(dir_name, file_name), "r") except Exception: # Incorrect: (_LW("%s %s"), (var, var)) # # When more than one variable is used the named interpolation # sytnax (i.e. %(var)s) is used to help translators adjust # messages for gammar rules which change parameter ordering LOG.warn(_LW("Unable to open file %(file)s in %(dir)s"), {"file": file_name, "dir": dir_name})
def warning(): LOG.debug("Demonstrating the use of _LW for LOG.warning") try: file_name = ".does.not.exist" open(file_name, "r") except Exception: # When using _LW in log messages: # # Correct: (_LW("msg %s"), var) # Incorrect: _LW("msg %s") % var # Incorrect: _LW("msg %s" % var) # # When adding variables to log messages string interpolation # should be delayed (i.e. ,) rather then being done at # the point of logging (i.e. %) # # http://docs.openstack.org/developer/oslo.i18n/guidelines.html#adding-variables-to-log-messages # # See Also [H702] # http://docs.openstack.org/developer/hacking/#internationalization-i18n-strings LOG.warn(_LW("Unable to open file %s"), file_name)