diff --git a/logging.py b/logging.py index 4441fe0d32034a0cc98793c85609b1eedae8ef35..2b606d97aa3d41a966803fa485d7011b6efd1b49 100644 --- a/logging.py +++ b/logging.py @@ -18,102 +18,106 @@ logging_max_bytes = 1024 * 1024 * 10 # Max log file size of 10 MB. logging_backup_count = 10 # Keep 10 log files before overwriting. -def init_logging(caller): +def get_logging_settings(): """ - Returns an instance of the logger. + Returns an instance of the logging settings dictionary. """ - return logging.getLogger(caller) - - -# Create logging folder if does not exist. -if not os.path.exists(logging_directory): - print('Creating logging folders.') - os.makedirs(logging_directory) - - -# Dictionary style logging options. -LOGGING = { - 'version': 1, - 'formatters': { - # Minimal logging. Only includes message. - # Generally meant for terminal "end user" interface display. - 'minimal': { - 'format': '%(message)s', - }, - # Simple logging. Includes message type and actual message. - # Generally meant for console logging. - 'simple': { - 'format': '[%(levelname)s] [%(filename)s %(lineno)d]: %(message)s', - }, - # Basic logging. Includes date, message type, file originated, and actual message. - # Generally meant for file logging. - 'standard': { - 'format': '%(asctime)s [%(levelname)s] [%(name)s %(lineno)d]: %(message)s', - }, - # Verbose logging. Includes standard plus the process number and thread id. - # For when you wanna be really verbose. - 'verbose': { - 'format': '%(asctime)s [%(levelname)s] [%(name)s %(lineno)d] || %(process)d %(thread)d || %(message)s' - }, - }, - 'handlers': { - # Sends log message to the void. May be useful for debugging. - 'null': { - 'class': 'logging.NullHandler', + return { + 'version': 1, + 'formatters': { + # Minimal logging. Only includes message. + # Generally meant for terminal "end user" interface display. + 'minimal': { + 'format': '%(message)s', + }, + # Simple logging. Includes message type and actual message. + # Generally meant for console logging. + 'simple': { + 'format': '[%(levelname)s] [%(filename)s %(lineno)d]: %(message)s', + }, + # Basic logging. Includes date, message type, file originated, and actual message. + # Generally meant for file logging. + 'standard': { + 'format': '%(asctime)s [%(levelname)s] [%(name)s %(lineno)d]: %(message)s', + }, + # Verbose logging. Includes standard plus the process number and thread id. + # For when you wanna be really verbose. + 'verbose': { + 'format': '%(asctime)s [%(levelname)s] [%(name)s %(lineno)d] || %(process)d %(thread)d || %(message)s' + }, }, - # To console. - 'console': { - 'level': 'INFO', - 'class': 'logging.StreamHandler', - 'formatter': 'simple', + 'handlers': { + # Sends log message to the void. May be useful for debugging. + 'null': { + 'class': 'logging.NullHandler', + }, + # To console. + 'console': { + 'level': 'INFO', + 'class': 'logging.StreamHandler', + 'formatter': 'simple', + }, + # Debug Level - To file. + 'file_debug': { + 'level': 'DEBUG', + 'class': logging_class, + 'filename': os.path.join(logging_directory, 'debug.log'), + 'maxBytes': logging_max_bytes, + 'backupCount': logging_backup_count, + 'formatter': 'standard', + }, + # Info Level - To file. + 'file_info': { + 'level': 'INFO', + 'class': logging_class, + 'filename': os.path.join(logging_directory, 'info.log'), + 'maxBytes': logging_max_bytes, + 'backupCount': logging_backup_count, + 'formatter': 'standard', + }, + # Warn Level - To file. + 'file_warn': { + 'level': 'WARNING', + 'class': logging_class, + 'filename': os.path.join(logging_directory, 'warn.log'), + 'maxBytes': logging_max_bytes, + 'backupCount': logging_backup_count, + 'formatter': 'standard', + }, + # Error Level - To file. + 'file_error': { + 'level': 'ERROR', + 'class': logging_class, + 'filename': os.path.join(logging_directory, 'error.log'), + 'maxBytes': logging_max_bytes, + 'backupCount': logging_backup_count, + 'formatter': 'standard', + }, }, - # Debug Level - To file. - 'file_debug': { - 'level': 'DEBUG', - 'class': logging_class, - 'filename': os.path.join(logging_directory, 'debug.log'), - 'maxBytes': logging_max_bytes, - 'backupCount': logging_backup_count, - 'formatter': 'standard', + 'loggers': { + # All basic logging. + '': { + 'handlers': ['console', 'file_debug', 'file_info', 'file_warn', 'file_error'], + 'level': 'NOTSET', + 'propagate': False, + } }, - # Info Level - To file. - 'file_info': { - 'level': 'INFO', - 'class': logging_class, - 'filename': os.path.join(logging_directory, 'info.log'), - 'maxBytes': logging_max_bytes, - 'backupCount': logging_backup_count, - 'formatter': 'standard', - }, - # Warn Level - To file. - 'file_warn': { - 'level': 'WARNING', - 'class': logging_class, - 'filename': os.path.join(logging_directory, 'warn.log'), - 'maxBytes': logging_max_bytes, - 'backupCount': logging_backup_count, - 'formatter': 'standard', - }, - # Error Level - To file. - 'file_error': { - 'level': 'ERROR', - 'class': logging_class, - 'filename': os.path.join(logging_directory, 'error.log'), - 'maxBytes': logging_max_bytes, - 'backupCount': logging_backup_count, - 'formatter': 'standard', - }, - }, - 'loggers': { - # All basic logging. - '': { - 'handlers': ['console', 'file_debug', 'file_info', 'file_warn', 'file_error'], - 'level': 'NOTSET', - 'propagate': False, - } - }, -} + } -# Load dictionary of settings into logger. -logging.config.dictConfig(LOGGING) +def init_logging(caller): + """ + Initializes and returns an instance of the logger. + :param caller: __name__ attribute of calling file. + :return: Instance of logger, associated with calling file's __name__. + """ + # Create logging folder if does not exist. + if not os.path.exists(logging_directory): + print('Creating logging folders.') + os.makedirs(logging_directory) + + # Load dictionary of settings into logger. + logger_settings = get_logging_settings() + logging.config.dictConfig(logger_settings) + + return logging.getLogger(caller) diff --git a/tests/src/logging.py b/tests/src/logging.py index 4441fe0d32034a0cc98793c85609b1eedae8ef35..2b606d97aa3d41a966803fa485d7011b6efd1b49 100644 --- a/tests/src/logging.py +++ b/tests/src/logging.py @@ -18,102 +18,106 @@ logging_max_bytes = 1024 * 1024 * 10 # Max log file size of 10 MB. logging_backup_count = 10 # Keep 10 log files before overwriting. -def init_logging(caller): +def get_logging_settings(): """ - Returns an instance of the logger. + Returns an instance of the logging settings dictionary. """ - return logging.getLogger(caller) - - -# Create logging folder if does not exist. -if not os.path.exists(logging_directory): - print('Creating logging folders.') - os.makedirs(logging_directory) - - -# Dictionary style logging options. -LOGGING = { - 'version': 1, - 'formatters': { - # Minimal logging. Only includes message. - # Generally meant for terminal "end user" interface display. - 'minimal': { - 'format': '%(message)s', - }, - # Simple logging. Includes message type and actual message. - # Generally meant for console logging. - 'simple': { - 'format': '[%(levelname)s] [%(filename)s %(lineno)d]: %(message)s', - }, - # Basic logging. Includes date, message type, file originated, and actual message. - # Generally meant for file logging. - 'standard': { - 'format': '%(asctime)s [%(levelname)s] [%(name)s %(lineno)d]: %(message)s', - }, - # Verbose logging. Includes standard plus the process number and thread id. - # For when you wanna be really verbose. - 'verbose': { - 'format': '%(asctime)s [%(levelname)s] [%(name)s %(lineno)d] || %(process)d %(thread)d || %(message)s' - }, - }, - 'handlers': { - # Sends log message to the void. May be useful for debugging. - 'null': { - 'class': 'logging.NullHandler', + return { + 'version': 1, + 'formatters': { + # Minimal logging. Only includes message. + # Generally meant for terminal "end user" interface display. + 'minimal': { + 'format': '%(message)s', + }, + # Simple logging. Includes message type and actual message. + # Generally meant for console logging. + 'simple': { + 'format': '[%(levelname)s] [%(filename)s %(lineno)d]: %(message)s', + }, + # Basic logging. Includes date, message type, file originated, and actual message. + # Generally meant for file logging. + 'standard': { + 'format': '%(asctime)s [%(levelname)s] [%(name)s %(lineno)d]: %(message)s', + }, + # Verbose logging. Includes standard plus the process number and thread id. + # For when you wanna be really verbose. + 'verbose': { + 'format': '%(asctime)s [%(levelname)s] [%(name)s %(lineno)d] || %(process)d %(thread)d || %(message)s' + }, }, - # To console. - 'console': { - 'level': 'INFO', - 'class': 'logging.StreamHandler', - 'formatter': 'simple', + 'handlers': { + # Sends log message to the void. May be useful for debugging. + 'null': { + 'class': 'logging.NullHandler', + }, + # To console. + 'console': { + 'level': 'INFO', + 'class': 'logging.StreamHandler', + 'formatter': 'simple', + }, + # Debug Level - To file. + 'file_debug': { + 'level': 'DEBUG', + 'class': logging_class, + 'filename': os.path.join(logging_directory, 'debug.log'), + 'maxBytes': logging_max_bytes, + 'backupCount': logging_backup_count, + 'formatter': 'standard', + }, + # Info Level - To file. + 'file_info': { + 'level': 'INFO', + 'class': logging_class, + 'filename': os.path.join(logging_directory, 'info.log'), + 'maxBytes': logging_max_bytes, + 'backupCount': logging_backup_count, + 'formatter': 'standard', + }, + # Warn Level - To file. + 'file_warn': { + 'level': 'WARNING', + 'class': logging_class, + 'filename': os.path.join(logging_directory, 'warn.log'), + 'maxBytes': logging_max_bytes, + 'backupCount': logging_backup_count, + 'formatter': 'standard', + }, + # Error Level - To file. + 'file_error': { + 'level': 'ERROR', + 'class': logging_class, + 'filename': os.path.join(logging_directory, 'error.log'), + 'maxBytes': logging_max_bytes, + 'backupCount': logging_backup_count, + 'formatter': 'standard', + }, }, - # Debug Level - To file. - 'file_debug': { - 'level': 'DEBUG', - 'class': logging_class, - 'filename': os.path.join(logging_directory, 'debug.log'), - 'maxBytes': logging_max_bytes, - 'backupCount': logging_backup_count, - 'formatter': 'standard', + 'loggers': { + # All basic logging. + '': { + 'handlers': ['console', 'file_debug', 'file_info', 'file_warn', 'file_error'], + 'level': 'NOTSET', + 'propagate': False, + } }, - # Info Level - To file. - 'file_info': { - 'level': 'INFO', - 'class': logging_class, - 'filename': os.path.join(logging_directory, 'info.log'), - 'maxBytes': logging_max_bytes, - 'backupCount': logging_backup_count, - 'formatter': 'standard', - }, - # Warn Level - To file. - 'file_warn': { - 'level': 'WARNING', - 'class': logging_class, - 'filename': os.path.join(logging_directory, 'warn.log'), - 'maxBytes': logging_max_bytes, - 'backupCount': logging_backup_count, - 'formatter': 'standard', - }, - # Error Level - To file. - 'file_error': { - 'level': 'ERROR', - 'class': logging_class, - 'filename': os.path.join(logging_directory, 'error.log'), - 'maxBytes': logging_max_bytes, - 'backupCount': logging_backup_count, - 'formatter': 'standard', - }, - }, - 'loggers': { - # All basic logging. - '': { - 'handlers': ['console', 'file_debug', 'file_info', 'file_warn', 'file_error'], - 'level': 'NOTSET', - 'propagate': False, - } - }, -} + } -# Load dictionary of settings into logger. -logging.config.dictConfig(LOGGING) +def init_logging(caller): + """ + Initializes and returns an instance of the logger. + :param caller: __name__ attribute of calling file. + :return: Instance of logger, associated with calling file's __name__. + """ + # Create logging folder if does not exist. + if not os.path.exists(logging_directory): + print('Creating logging folders.') + os.makedirs(logging_directory) + + # Load dictionary of settings into logger. + logger_settings = get_logging_settings() + logging.config.dictConfig(logger_settings) + + return logging.getLogger(caller)