Source code for tuna.log.utils
"""
This module's scope covers interactions with the logging facilities.
"""
import logging
import sys
import tuna
[docs]def function_name ( ):
"""
This method's goal is to access the name of the caller function.
Returns:
* unnamed variable : string
Containing the function name of the caller.
"""
return sys._getframe ( ).f_back.f_code.co_name
[docs]def line_number ( ):
"""
This method's goal is to provide the line number from which the caller called this function, for logging purposes.
Returns:
* unnamed variable : int
Containing the line number (in the relevant source file) from which the caller called.
"""
return sys._getframe ( ).f_back.f_lineno
[docs]def script_name ( ):
"""
This method's goal is to access the name of the script currently being processed.
Returns:
* unnamed variable : string
Containing the name of the command used to start the current session.
"""
return sys._getframe ( ).f_back.f_code.co_filename.split ( "/" ) [ -1 ]
[docs]def set_path ( file_name ):
"""
This method's goal is to set the path and file name where the log output will be saved.
Parameters:
* file_name : string
Containing a valid path and file name for a text file, where new log entries will be appended.
"""
log = logging.getLogger ( __name__ )
log.setLevel ( logging.INFO )
if not isinstance ( file_name, str ):
log.error ( "Non-string passed as file_name." )
return
new_handler = logging.FileHandler ( file_name )
new_formatter = logging.Formatter ( fmt = "%(asctime)s %(name)s %(levelname)5s %(message)s",
datefmt = '%Y-%m-%d %H:%M:%S' )
new_handler.setFormatter ( new_formatter )
tuna._log.addHandler ( new_handler )
tuna._log_handlers.append ( new_handler )
log.info ( "Log file set to %s." % file_name )
[docs]def verbose ( handler_type, verbosity ):
"""
This method's goal is to set the specified logging handler type to the specified verbosity.
Parameters:
* handler_type : string
can be either "console" or "file".
* verbosity : string
Must be the name of a level from the logging module, such as "DEBUG" or "INFO".
"""
try:
level = getattr ( logging, verbosity )
except:
print ( "Unrecognized logging level." )
return
if handler_type == "console":
for handler in tuna._log_handlers:
if isinstance ( handler, logging.StreamHandler ):
handler.setLevel ( level )
print ( "Handler {} set to {}.".format ( handler, level ) )
if handler_type == "file":
for handler in tuna._log_handlers:
if isinstance ( handler, logging.FileHandler ):
handler.setLevel ( level )
print ( "Handler {} set to {}.".format ( handler, level ) )