Better logging. Less logging.

This commit is contained in:
Scott Idem
2024-10-09 11:17:25 -04:00
parent 3bf54fcb47
commit 1c91c92d67
3 changed files with 34 additions and 25 deletions

View File

@@ -85,24 +85,27 @@ def sql_connect(
log.debug('############## ############') log.debug('############## ############')
if current_db: if current_db:
DB = settings.DB current_db.engine.dispose()
settings.SQLALCHEMY_DB_URI = 'mysql://'+DB['username']+':'+DB['password']+'@'+DB['server']+'/'+DB['name'] log.info('Disposed of the current engine (connection pool).')
log.debug(settings.DB)
# new_engine = create_engine(url=settings.SQLALCHEMY_DB_URI, poolclass=NullPool, echo=False, isolation_level='READ UNCOMMITTED', connect_args={'connect_timeout': settings.DB['connect_timeout']}) # DB = settings.DB
new_engine = create_engine( # settings.SQLALCHEMY_DB_URI = 'mysql://'+DB['username']+':'+DB['password']+'@'+DB['server']+'/'+DB['name']
url = settings.SQLALCHEMY_DB_URI, # log.debug(settings.DB)
echo = False,
pool_use_lifo = True,
pool_pre_ping = True,
isolation_level = 'READ COMMITTED',
connect_args = {'connect_timeout': settings.DB['connect_timeout']}
)
# new_engine = create_engine(url=settings.SQLALCHEMY_DB_URI, pool_size=5, max_overflow=15, timeout=settings.DB['connect_timeout'], pool_recycle=settings.DB['pool_recycle'], pool_pre_ping=True, echo=False, echo_pool=True, isolation_level='READ UNCOMMITTED', connect_args={'connect_timeout': settings.DB['connect_timeout']})
# new_engine = create_engine(url=settings.SQLALCHEMY_DB_URI, pool_size=5, max_overflow=15, pool_recycle=settings.DB['pool_recycle'], pool_pre_ping=True, echo=False, echo_pool=True, isolation_level='READ UNCOMMITTED', connect_args={'connect_timeout': settings.DB['connect_timeout']})
current_db.engine = new_engine # # new_engine = create_engine(url=settings.SQLALCHEMY_DB_URI, poolclass=NullPool, echo=False, isolation_level='READ UNCOMMITTED', connect_args={'connect_timeout': settings.DB['connect_timeout']})
log.info(f'Created and connected to database: {settings.SQLALCHEMY_DB_URI}') # new_engine = create_engine(
# url = settings.SQLALCHEMY_DB_URI,
# echo = False,
# pool_use_lifo = True,
# pool_pre_ping = True,
# isolation_level = 'READ COMMITTED',
# connect_args = {'connect_timeout': settings.DB['connect_timeout']}
# )
# # new_engine = create_engine(url=settings.SQLALCHEMY_DB_URI, pool_size=5, max_overflow=15, timeout=settings.DB['connect_timeout'], pool_recycle=settings.DB['pool_recycle'], pool_pre_ping=True, echo=False, echo_pool=True, isolation_level='READ UNCOMMITTED', connect_args={'connect_timeout': settings.DB['connect_timeout']})
# # new_engine = create_engine(url=settings.SQLALCHEMY_DB_URI, pool_size=5, max_overflow=15, pool_recycle=settings.DB['pool_recycle'], pool_pre_ping=True, echo=False, echo_pool=True, isolation_level='READ UNCOMMITTED', connect_args={'connect_timeout': settings.DB['connect_timeout']})
# current_db.engine = new_engine
# log.info(f'Created and connected to database: {settings.SQLALCHEMY_DB_URI}')
return True return True
else: else:
log.warning(f'Could not created and or connect to database') log.warning(f'Could not created and or connect to database')
@@ -1016,8 +1019,8 @@ def run_sql_select(
log.error('An operational error exception happened. This is likely a "MySQL server has gone away" error. Going to try again...') log.error('An operational error exception happened. This is likely a "MySQL server has gone away" error. Going to try again...')
log.exception('**** *** ** * ### BEGIN ### Operational Exception Happened: Trying again... * ** *** ****') log.exception('**** *** ** * ### BEGIN ### Operational Exception Happened: Trying again... * ** *** ****')
log.error('^^^^ ^^^ ^^ ^ ### END ### Operational Exception ^ ^^ ^^^ ^^^^') log.error('^^^^ ^^^ ^^ ^ ### END ### Operational Exception ^ ^^ ^^^ ^^^^')
log.error('SQL Alchemy DB URI:') # log.error('SQL Alchemy DB URI:')
log.error(settings.SQLALCHEMY_DB_URI) # log.error(settings.SQLALCHEMY_DB_URI)
sql_connect(current_db=db) sql_connect(current_db=db)
@@ -1059,8 +1062,8 @@ def run_sql_select(
log.error('A programming error exception happened. This may be related to multithreading. It may also be related to a DB connection issue. Going to try again...') log.error('A programming error exception happened. This may be related to multithreading. It may also be related to a DB connection issue. Going to try again...')
log.exception('**** *** ** * ### BEGIN ### Programming Exception Happened: Trying again... * ** *** ****') log.exception('**** *** ** * ### BEGIN ### Programming Exception Happened: Trying again... * ** *** ****')
log.error('^^^^ ^^^ ^^ ^ ### END ### Programming Exception ^ ^^ ^^^ ^^^^') log.error('^^^^ ^^^ ^^ ^ ### END ### Programming Exception ^ ^^ ^^^ ^^^^')
log.error('SQL Alchemy DB URI:') # log.error('SQL Alchemy DB URI:')
log.error(settings.SQLALCHEMY_DB_URI) # log.error(settings.SQLALCHEMY_DB_URI)
sql_connect(current_db=db) sql_connect(current_db=db)
@@ -1383,9 +1386,15 @@ def redis_lookup_id_random(
log.error('The SQL result was not what was expected. The ID field was not found.') log.error('The SQL result was not what was expected. The ID field was not found.')
return False return False
else: else:
log.error(f'SQL: More than one record may have been found in the table "{table_name}". There may be a duplicate id_random value in this table.') log.error(f'SQL: More than one record may have been found in the table "{table_name}". There may be a duplicate id_random value in this table. This should not happen!')
log.error(select_results) log.error(select_results)
return False
# Try again...
log.warning(f'SQL: ID Random "{record_id_random}" was not found in table "{table_name}". Trying again...')
new_result = redis_lookup_id_random(record_id_random=record_id_random, table_name=table_name)
return new_result
# return False
else: else:
log.warning(f'SQL: ID Random "{record_id_random}" was not found in table "{table_name}". Returning None.') log.warning(f'SQL: ID Random "{record_id_random}" was not found in table "{table_name}". Returning None.')
return None return None

View File

@@ -85,7 +85,7 @@ def logger_reset(func):
# log.info(locals()) # log.info(locals())
@functools.wraps(func) @functools.wraps(func)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
if func.__name__ not in ['redis_lookup_id_random']: if func.__name__ not in ['redis_lookup_id_random', 'sql_enable_part', 'sql_hidden_part']:
log.info(f'*** Function: "{func.__name__}()"') log.info(f'*** Function: "{func.__name__}()"')
log.debug(f'*** Function Positional Args: {args}\nFunction Key Args: {kwargs}') log.debug(f'*** Function Positional Args: {args}\nFunction Key Args: {kwargs}')
init_log_level = log.level init_log_level = log.level

View File

@@ -514,7 +514,7 @@ def handle_get_obj_li(
limit = commons.limit, limit = commons.limit,
offset = commons.offset, offset = commons.offset,
as_list = True, as_list = True,
log_lvl = logging.INFO # log_lvl = logging.INFO
) )
else: else:
# NOTE: The enabled and hidden parameters are new to this endpoint and the sql_select function! -2023-07-06 # NOTE: The enabled and hidden parameters are new to this endpoint and the sql_select function! -2023-07-06
@@ -534,7 +534,7 @@ def handle_get_obj_li(
limit = commons.limit, limit = commons.limit,
offset = commons.offset, offset = commons.offset,
as_list = True, as_list = True,
log_lvl = logging.INFO # log_lvl = logging.INFO
) )
# log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL # log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL