diff --git a/documents/references.md b/documents/references.md new file mode 100644 index 0000000000000000000000000000000000000000..0b6fc289032f444c79fa46646922ba4374ea608c --- /dev/null +++ b/documents/references.md @@ -0,0 +1,15 @@ +# Custom Logging - Documents > References + + +## Description +Various references used in project. + + +## References + +### Python Logging Docs +<https://docs.python.org/3.8/library/logging.html> + +### Django Logging +(Pretty thorough docs of logging "settings dictionaries" in Python) +<https://docs.djangoproject.com/en/dev/topics/logging/> diff --git a/main.py b/main.py index f99485df720e959b3e741d46310bd1f0e166f4f2..b5dcb31ee0e984b52ea03be72886b31f7e4e4663 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,27 @@ Descripton here. """ +# User Imports. +from src.logging import init_logging + + +# Import logger. +logger = init_logging(__name__) + + +def main(): + """ + Test logging output to console. + """ + logger.debug('Test DEBUG logging statement.') + logger.info('Test INFO logging statement.') + logger.warning('Test WARNING logging statement.') + logger.error('Test ERROR logging statement.') + if __name__ == '__main__': - pass + print('Starting program.') + + main() + + print('Terminating program.') diff --git a/src/logging.py b/src/logging.py index 7e9d1728afa473f01446cf5ca060d6e16d012970..4441fe0d32034a0cc98793c85609b1eedae8ef35 100644 --- a/src/logging.py +++ b/src/logging.py @@ -4,3 +4,116 @@ Custom Python logger, defined via a dictionary of logging settings. https://git.brandon-rodriguez.com/python/custom_logger Version 1.0 """ + + +# System Imports. +import logging.config, os + + +# Logging Variables. +project_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +logging_directory = os.path.join(project_dir, 'src/logs') +logging_class = 'logging.handlers.RotatingFileHandler' +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): + """ + Returns an instance of the logger. + """ + 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', + }, + # 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', + }, + }, + '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)