Last active
February 26, 2018 15:51
-
-
Save ThriceGood/fdc5dd9c4190a3bdc1576af0d625f1f9 to your computer and use it in GitHub Desktop.
Python Logging wrapper for easy logging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import logging | |
| import os | |
| class Logger: | |
| def __init__(self, name): | |
| self.create_log_dir() | |
| self.logger = logging.getLogger('{}'.format(name)) | |
| format = "%(asctime)s [%(levelname)s]: %(message)s" | |
| logging.basicConfig(format=format, level=logging.DEBUG) | |
| self.log_file_path = os.path.join(self.logs_dir, '{}.log'.format(name)) | |
| file_handler = logging.FileHandler(self.log_file_path) | |
| file_handler.setLevel(logging.DEBUG) | |
| formatter = logging.Formatter(format) | |
| file_handler.setFormatter(formatter) | |
| self.logger.addHandler(file_handler) | |
| def create_log_dir(self): | |
| root = os.path.dirname(os.path.realpath(__file__)) | |
| self.logs_dir = os.path.join(root, 'logs') | |
| if not os.path.isdir(self.logs_dir): | |
| os.mkdir(self.logs_dir) | |
| def info(self, message): | |
| self.logger.info(message) | |
| def warning(self, message): | |
| self.logger.warning(message) | |
| def debug(self, message): | |
| self.logger.debug(message) | |
| def error(self, message): | |
| self.logger.error(message) | |
| def exception(self, message): | |
| self.logger.exception(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment