Fixed level issues on log output. Also tweaked it so that all log rows come to the log file, but not nessecarily the interactive screen (tty/journald). Also tweaked certain log messages to be printed vs not printed.

This commit is contained in:
Anton Hvornum 2020-11-04 23:53:39 +00:00
parent ab69cb7525
commit f594e6638a
2 changed files with 15 additions and 11 deletions

View File

@ -245,7 +245,7 @@ class sys_command():#Thread):
self.exit_code = 0
if self.exit_code != 0 and not self.kwargs['suppress_errors']:
self.log(f"'{self.raw_cmd}' did not exit gracefully, exit code {self.exit_code}.", level=LOG_LEVELS.Debug)
self.log(f"'{self.raw_cmd}' did not exit gracefully, exit code {self.exit_code}.", level=LOG_LEVELS.Error)
self.log(self.trace_log.decode('UTF-8'), level=LOG_LEVELS.Debug)
raise SysCallError(f"'{self.raw_cmd}' did not exit gracefully, exit code {self.exit_code}.\n{self.trace_log.decode('UTF-8')}")

View File

@ -74,16 +74,6 @@ def stylize_output(text :str, *opts, **kwargs):
return '%s%s' % (('\x1b[%sm' % ';'.join(code_list)), text or '')
def log(*args, **kwargs):
if 'level' in kwargs:
if 'LOG_LEVEL' not in storage:
storage['LOG_LEVEL'] = LOG_LEVELS.Info
if kwargs['level'] >= storage['LOG_LEVEL']:
print(f"Level {kwargs['level']} is higher than storage log level {storage['LOG_LEVEL']}.")
# Level on log message was Debug, but output level is set to Info.
# In that case, we'll drop it.
return None
string = orig_string = ' '.join([str(x) for x in args])
if supports_color():
@ -91,6 +81,7 @@ def log(*args, **kwargs):
string = stylize_output(string, **kwargs)
# Log to a file output unless specifically told to suppress this feature.
# (level has no effect on the log file, everything will be written there)
if 'file' in kwargs and not 'suppress' in kwargs and kwargs['suppress']:
if type(kwargs['file']) is str:
with open(kwargs['file'], 'a') as log_file:
@ -99,10 +90,23 @@ def log(*args, **kwargs):
kwargs['file'].write(f"{orig_string}\n")
# If we assigned a level, try to log it to systemd's journald.
# Unless the level is higher than we've decided to output interactively.
# (Remember, log files still get *ALL* the output despite level restrictions)
if 'level' in kwargs:
if 'LOG_LEVEL' not in storage:
storage['LOG_LEVEL'] = LOG_LEVELS.Info
if kwargs['level'] > storage['LOG_LEVEL']:
print(f"Level {kwargs['level']} is higher than storage log level {storage['LOG_LEVEL']}.")
# Level on log message was Debug, but output level is set to Info.
# In that case, we'll drop it.
return None
try:
journald.log(string, level=kwargs['level'])
except ModuleNotFoundError:
pass # Ignore writing to journald
# Finally, print the log unless we skipped it based on level.
# And we print the string which may or may not contain color formatting.
print(string)