Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Fix use of logging
Browse files Browse the repository at this point in the history
Needed to use 'log', which sends messages to the rho logger, instead
of 'logging', which sends messages to the default system logger. (The
rho logger is configured to send warnings and above to stdout; the
system logger is not.)
  • Loading branch information
Noah Lavine committed Sep 19, 2017
1 parent beb499e commit d4fe7f0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion rho/scancommand.py
Expand Up @@ -371,7 +371,7 @@ def run_ansible_with_vault(cmd_string, vault_pass, env=None, log_path=None,
new_pos = logfile.tell()
logfile.seek(last_pos)
logfile_lines = logfile.readlines()
logging.info(logfile_lines)
log.info(logfile_lines)
print(logfile_lines[-1].replace('\r\n', ''))
logfile.seek(new_pos)
last_pos = new_pos
Expand Down
32 changes: 15 additions & 17 deletions rho/utilities.py
Expand Up @@ -167,6 +167,12 @@ def setup_logging(verbosity):
logging.basicConfig(filename=RHO_LOG)
# but we only adjust the log level for the 'rho' logger.
log.setLevel(log_level)
# the StreamHandler sends warnings and above to stdout, but
# only for messages going to the 'rho' logger, i.e. Rho
# output.
stderr_handler = logging.StreamHandler()
stderr_handler.setLevel(logging.WARNING)
log.addHandler(stderr_handler)


def threaded_tailing(path, ansible_verbosity=0):
Expand Down Expand Up @@ -290,10 +296,8 @@ def read_ranges(ranges_or_path):
if ranges_or_path and os.path.isfile(ranges_or_path[0]):
range_list = _read_in_file(ranges_or_path[0])
elif invalid_path == [] and len(ranges_or_path) == 1:
logging.error(_("Couldn't interpret %s as host file because "
"no such file"), ranges_or_path[0])
print(_("Couldn't interpret %s as host file because "
"no such file exist." % (ranges_or_path[0])))
log.error(_("Couldn't interpret %s as host file because "
"no such file exists."), ranges_or_path[0])
sys.exit(1)
else:
range_list = ranges_or_path
Expand All @@ -317,8 +321,7 @@ def read_ranges(ranges_or_path):
pass

if not match_found:
logging.error(_("Bad host name/range : '%s'" % (reg_item)))
print(_("Bad host name/range : '%s'" % (reg_item)))
log.error(_("Bad host name/range : '%s'" % (reg_item)))
sys.exit(1)

return normalized
Expand Down Expand Up @@ -354,42 +357,37 @@ def cidr_to_ansible(ip_range):
try:
base_address, prefix_bits = ip_range.split('/')
except ValueError as split_error:
logging.error(split_error)
err_msg = err_prefix + 'Invalid format was supplied with %s.'
print(_(err_msg % (ip_range)))
err_msg = err_prefix + 'IP range %s has invalid format.'
log.error(err_msg, ip_range)
sys.exit(1)

prefix_bits = int(prefix_bits)

if prefix_bits < 0 or prefix_bits > 32:
err_msg = err_prefix + 'Bit mask length %s of IP range %s is not in ' \
'the valid range [0,32].'
logging.error(err_msg, prefix_bits, ip_range)
print(_(err_msg % (prefix_bits, ip_range)))
log.error(err_msg, prefix_bits, ip_range)
sys.exit(1)

octet_strings = base_address.split('.')
if len(octet_strings) != 4:
err_msg = err_prefix + 'IP address %s (part of IP range %s) ' \
'does not have exactly 4 octets'
logging.error(err_msg, base_address, ip_range)
print(_(err_msg % (base_address, ip_range)))
log.error(err_msg, base_address, ip_range)
sys.exit(1)

octets = [None] * 4
for i in range(4):
if not octet_strings[i]:
err_msg = err_prefix + 'Empty octet in IP range %s'
logging.error(err_msg, ip_range)
print(_(err_msg % (ip_range)))
log.error(err_msg, ip_range)
sys.exit(1)

val = int(octet_strings[i])
if val < 0 or val > 255:
err_msg = err_prefix + 'IP octet %s (part of IP range %s) ' \
'is not in the valid range [0,255]'
logging.error(err_msg, val, ip_range)
print(_(err_msg % (val, ip_range)))
log.error(err_msg, val, ip_range)
sys.exit(1)
octets[i] = val

Expand Down

0 comments on commit d4fe7f0

Please sign in to comment.