Created
July 3, 2017 12:43
-
-
Save luerhard/fac378efe30d9b8852853be9302b8a4c to your computer and use it in GitHub Desktop.
This file contains 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 | |
def setup_logging(file_level=logging.DEBUG, filename='logfile.log', mode="w", console_level=logging.WARNING): | |
''' | |
Creates logger with file and console support and different logging levels. | |
''' | |
if not logging.getLogger().handlers: | |
file_handler = logging.FileHandler(filename=filename, mode=mode, delay=False, encoding='utf-8') | |
file_formatter = logging.Formatter('%(message)s') | |
file_handler.setFormatter(file_formatter) | |
file_handler.setLevel(file_level) | |
console_logger = logging.StreamHandler() | |
console_formatter = logging.Formatter('%(asctime)s:%(message)s') | |
console_logger.setFormatter(console_formatter) | |
console_logger.setLevel(console_level) | |
logging.getLogger().addHandler(file_handler) | |
logging.getLogger().addHandler(console_logger) | |
logging.getLogger().setLevel(logging.DEBUG) | |
else: | |
logging.getLogger().handlers[0].setLevel(file_level) | |
logging.getLogger().handlers[1].setLevel(console_level) | |
for logger in logging.getLogger().handlers: | |
logging.info("\n{} active on Level {}\n".format(logger, logger.level)) | |
#setup_logging(filename='name_of_logfile.log', console_level=logging.INFO) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment