From f31d5e34cdfe9594678316dc100d8c381aac1f43 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Sun, 24 Jan 2021 22:42:07 +0100 Subject: Trying to centralize the configuration for logging. Phasing out some parameters which also affects the current ability to create multiple log files for multiple runs. This will be re-introduced when logging is made into a class object in a later version --- archinstall/lib/output.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'archinstall/lib/output.py') diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index 3c1b12e2..267f2635 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -2,6 +2,7 @@ import abc import os import sys import logging +from pathlib import Path from .storage import storage class LOG_LEVELS: @@ -76,30 +77,28 @@ def stylize_output(text :str, *opts, **kwargs): def log(*args, **kwargs): string = orig_string = ' '.join([str(x) for x in args]) + # Attempt to colorize the output if supported + # Insert default colors and override with **kwargs if supports_color(): kwargs = {'bg' : 'black', 'fg': 'white', **kwargs} string = stylize_output(string, **kwargs) - if (logfile := storage.get('logfile', None)) and 'file' not in kwargs: - kwargs['file'] = logfile + # If a logfile is defined in storage, + # we use that one to output everything + if (logfile := storage.get('LOG_FILE', None)): + absolute_logfile = os.path.join(storage.get('LOG_PATH', './'), logfile) + if not os.path.isfile(absolute_logfile): + os.makedirs(os.path.dirname(absolute_logfile)) + Path(absolute_logfile).touch() # Overkill? - # 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 ('suppress' not in kwargs or kwargs['suppress'] == False): - if type(kwargs['file']) is str: - with open(kwargs['file'], 'a') as log_file: - log_file.write(f"{orig_string}\n") - elif kwargs['file']: - kwargs['file'].write(f"{orig_string}\n") + with open(absolute_logfile, 'a') as log_file: + logfile.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']: + if kwargs['level'] > storage.get('LOG_LEVEL', LOG_LEVELS.Info): # Level on log message was Debug, but output level is set to Info. # In that case, we'll drop it. return None @@ -111,4 +110,5 @@ def log(*args, **kwargs): # 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) \ No newline at end of file + sys.stdout.write(string) + sys.stdout.flush() \ No newline at end of file -- cgit v1.2.3-70-g09d2