Skip to content

Commit

Permalink
formatting tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ckuhtz committed Jul 23, 2024
1 parent 2bb2382 commit 00443ec
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
3 changes: 2 additions & 1 deletion api/internal/check_env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def check_env_vars() -> Dict:
If not, substitute a reasonable default.
Returns:
Dict: returns a dictionary of environment variables and their assigned values. NOT typesafe. Consumer is resonsible for typecasting as needed.
Dict: returns a dictionary of environment variables and their assigned
values. NOT typesafe. Consumer is resonsible for typecasting as needed.
"""

# access the 'global' logger
Expand Down
37 changes: 26 additions & 11 deletions api/internal/logger_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CustomFormatter(logging.Formatter):
"""
logging.Formatter to modify the logging format based on logging level
"""

def __init__(self, fmt=None, datefmt=None, style='%', level_formats=None):
super().__init__(fmt, datefmt, style)
self.level_formats = level_formats or {}
Expand All @@ -23,6 +24,7 @@ def format(self, record):
class ANSIColors:
"""ANSI escape sequences for colors to use in console text strings
"""

green = '\033[32m'
blue = '\033[34m'
white = '\033[37m'
Expand All @@ -36,26 +38,38 @@ class ANSIColors:

# Define custom formats for each severity level
level_formats = {
# logging.DEBUG: ANSIColors.cyan + '%(levelname)s:' + ANSIColors.white + '\t %(asctime)s %(module)s' + ANSIColors.cyan + ' %(message)s' + ANSIColors.reset,
logging.DEBUG: ANSIColors.cyan + '%(levelname)s:' + ANSIColors.white + '\t %(module)s' + ANSIColors.cyan + ' %(message)s' + ANSIColors.reset,
logging.INFO: ANSIColors.green + '%(levelname)s: \t %(message)s' + ANSIColors.reset,
logging.WARNING: ANSIColors.yellow + '%(levelname)s:' + ANSIColors.reset + '\t %(message)s',
logging.ERROR: ANSIColors.red + '%(levelname)s:' + ANSIColors.reset + '\t %(message)s',
logging.CRITICAL: ANSIColors.bold_red + '%(levelname)s:' + ANSIColors.reset + ' %(message)s'
logging.DEBUG: ANSIColors.cyan + '%(levelname)s:' + ANSIColors.white +
'\t %(module)s' + ANSIColors.cyan + ' %(message)s' +
ANSIColors.reset,

logging.INFO: ANSIColors.green + '%(levelname)s: \t %(message)s' +
ANSIColors.reset,

logging.WARNING: ANSIColors.yellow + '%(levelname)s:' + ANSIColors.reset +
'\t %(message)s',

logging.ERROR: ANSIColors.red + '%(levelname)s:' + ANSIColors.reset +
'\t %(message)s',

logging.CRITICAL: ANSIColors.bold_red + '%(levelname)s:' +
ANSIColors.reset + ' %(message)s'
}



# def logger_init(startup_logging_level: str = 'INFO') -> logging.Logger:
def logger_init(startup_logging_level: str = 'INFO'):
"""
Initialize logging.Logger with custom logging format based on logging severity level.
Initialize logging.Logger with custom logging format based on logging
severity level.
Args:
startup_logging_level (str, optional): Optional early logging level (i.e. 'DEBUG'). Defaults to 'INFO'.
startup_logging_level (str, optional): Optional early logging level
(i.e. 'DEBUG'). Defaults to 'INFO'.
Returns:
logging.Logger: returns a Logger with custom formatter/handler and level set (see Args).
logging.Logger: returns a Logger with custom formatter/handler and
level set (see Args).
"""

logger = logging.getLogger('api')
Expand All @@ -69,8 +83,9 @@ def logger_init(startup_logging_level: str = 'INFO'):

logger.addHandler(handler)

# set_log_level(logger, 'debug') # normally not needed, enable for debugging of env variables only
# set_log_level(logger, 'debug') # normally not needed, enable for
# debugging of env variables only

logger.setLevel(logging.getLevelName(startup_logging_level))

# return logger
# return logger
10 changes: 6 additions & 4 deletions api/internal/set_log_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

def set_log_level(level_str: str) -> None:
"""
Sets logging level for a provided Logger. If string doesn't match one of the defined values in the function (excludes NOTSET), set to DEBUG.
Sets logging level for a provided Logger. If string doesn't match one of
the defined values in the function (excludes NOTSET), set to DEBUG.
Args:
logger (Logger): reference to an existing Logger
level_str (str): case-insensitive text string for logging level, e.g. 'debug' for LOG_DEBUG.
level_str (str): case-insensitive text string for logging level,
e.g. 'debug' for LOG_DEBUG.
"""

# access the 'global' logger
Expand All @@ -27,8 +29,8 @@ def set_log_level(level_str: str) -> None:
logger.debug(f'level_str = {level_str}')

if not level_str in log_levels:
logger.critical('unrecognized LOG_LEVEL, FastAPI will probably throw up, exiting.')
logger.debug('sleeping for 5 seconds to reduce thrashing.')
logger.critical('unrecognized LOG_LEVEL, exiting.')
logger.debug('(sleeping for 5 seconds to reduce thrashing.)')
sleep(5)
os.sys.exit()

Expand Down

0 comments on commit 00443ec

Please sign in to comment.