Documentation for the Code¶
custom_logger.py¶
Note
Works with python3, no support for python 2.7
-
class
custom_logger.
Logger
(logger_prop_file_path, log_file_path)¶ Custom Logger class. Dont actually need to get an instance of this class, but do internally create an instance to call the
__init__()
which initializes a lot of the logger variables.Currently only provides three functions for logging
- Method entry logging
- Method exit logging
- Normal logging
Both Method entry logging and Method exit logging are only INFO level logging by default
Supports 3 levels of logging - INFO, ERROR, DEBUG. These are mutually exclusive (ie not hierarchial)
Supports 2 modes of logging (simultaneously):
- FILE - Writes logs to a file in the logs folder
- CONSOLE - Logs to the standard output console
Log format - [<log level> <timestamp>] [Module name]-[Method name] <log text>
REQUIREMENTS
- logger.properties file
- logs folder
Warning
logger.properties file needs to have [logger properties] at the root
SAMPLE USAGE
from YALogger.custom_logger import Logger
Logger.initialize_logger(logger_prop_file_path = '.\logger.properties',log_file_path = './logs')
Logger.perform_method_entry_logging('foo','bar')
Logger.perform_method_exit_logging('foo','bar')
Logger.log('info', 'foo','bar','this is the log text')
-
__init__
(logger_prop_file_path, log_file_path)¶ Initilaizing various Logger class properties:
- Logger.__level : the logging.level
- Logger.__mode : the logging mode
- Logger.__current_timestamp : the current timestamp
- Logger.__log_file_path : the log file path and name
Parameters: - logger_prop_file_path (str) – the path of logger.properties
- log_file_path (str) – the path of the log file
- Raises
- NoSectionError IOError
-
static
__new__
(cls, logger_prop_file_path, log_file_path)¶ Defining code in __new__() to make Logger a singleton class Logger.__instance keeps tracj of whether an instance of Logger exists or not. If it doesnt exist then creates it otherwise returns the existing instance of Logger
Parameters: - logger_prop_file_path (str) – the path of logger.properties
- log_file_path (str) – the path of the log file
Returns: singleton instance of Logger
Return type: instance of Logger
-
static
_open_log_file
(log_file_path)¶ Opens the log file when logging.mode = FILE Opens in append mode
Parameters: log_file_path (str) – the path of the log file Returns: reference to opened file Return type: file instance
-
static
_validate_logging_level
(logging_level)¶ Checks of the logging.level specified in logger.properties and validates whether it falls under the valid values Valid values are - ‘INFO’, ‘ERROR’ , ‘DEBUG’
Parameters: logging_level (str) – logging level in logger.properties Returns: logging levels from logger.properties Return type: list Raises: ValueError
-
static
_validate_logging_mode
(logging_mode)¶ Checks of the logging.mode specified in logger.properties and validates whether it falls under the valid values Valid values are - ‘FILE’, ‘CONSOLE’
Parameters: logging_mode (str) – logging mode in logger.properties Returns: logging modes from logger.properties Return type: list Raises: ValueError
-
static
initialize_logger
(logger_prop_file_path, log_file_path)¶ Initializes the logger. Creates a new instance of Logger to call
__init__()
Parameters: - logger_prop_file_path (str) – the path of logger.properties
- log_file_path (str) – the path of the log file
-
static
log
(log_level, module_name, method_name, log_text)¶ Call this method to log text.
Support added for log_text to be anything other than string as well (like dict, list etc)
Parameters: - log_level (str) – level of logging
- module_name (str) – module name
- method_name (str) – name of method being entered
- log_text (anything) – stuff to be logged
-
static
perform_method_entry_logging
(module_name, method_name)¶ Call this method before entering a method
Parameters: - module_name (str) – module name
- method_name (str) – name of method being entered
-
static
perform_method_exit_logging
(module_name, method_name)¶ Call this method after exiting a method
Parameters: - module_name (str) – module name
- method_name (str) – name of method being entered