Skip to content

Commit

Permalink
improve handling of how log level is set
Browse files Browse the repository at this point in the history
  • Loading branch information
n8marti committed Jan 2, 2024
1 parent 034cdbb commit 64ec81c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
12 changes: 6 additions & 6 deletions LogosLinuxInstaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from msg import cli_msg
from msg import initialize_logging
from msg import logos_error
from msg import update_log_level
from utils import checkDependencies
from utils import curses_menu
from utils import die_if_root
Expand Down Expand Up @@ -121,7 +122,7 @@ def main():
cli_args = parse_command_line()

# Initialize logging.
initialize_logging(logging.WARNING)
initialize_logging(config.LOG_LEVEL)

die_if_running()
die_if_root()
Expand All @@ -140,13 +141,12 @@ def main():
# Parse CLI args and update affected config vars.
parse_args(cli_args)

# Update logging level from config.
logger = logging.getLogger()
logger.setLevel(config.LOG_LEVEL)

if config.DELETE_INSTALL_LOG and os.path.isfile(config.LOGOS_LOG):
os.remove(config.LOGOS_LOG)


# Set terminal log level based on current config.
update_log_level(config.LOG_LEVEL)

# If Logos app is installed, run the desired Logos action.
if config.LOGOS_EXE is not None and os.access(config.LOGOS_EXE, os.X_OK):
if config.ACTION == 'control':
Expand Down
22 changes: 22 additions & 0 deletions msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@
import config


def get_log_level_name(level):
name = None
levels = {
"CRITICAL": logging.CRITICAL,
"ERROR": logging.ERROR,
"WARNING": logging.WARNING,
"INFO": logging.INFO,
"DEBUG": logging.DEBUG,
}
for k, v in levels.items():
if level == v:
name = k
break
return name

def initialize_logging(stderr_log_level):
'''
Log levels:
Expand Down Expand Up @@ -40,6 +55,13 @@ def initialize_logging(stderr_log_level):
)
cli_msg(f"Installer log file: {config.LOGOS_LOG}")

def update_log_level(new_level):
# Update logging level from config.
for h in logging.getLogger().handlers:
if type(h) == logging.StreamHandler:
h.setLevel(new_level)
logging.info(f"Terminal log level set to {get_log_level_name(new_level)}")

def cli_msg(message, end='\n'):
''' Used for messages that should be printed to stdout regardless of log level. '''
print(message, end=end)
Expand Down

0 comments on commit 64ec81c

Please sign in to comment.