2013年11月11日 星期一

TypeError: __init__() should return None

import logging

LOG_PATH = 'logs'

LOG_FILENAME = 'log.txt'
 
class Logger(object):
    def __new__(cls, logger_name, filename=None, format=None):
        if not os.path.exists(LOG_PATH):
            os.makedirs(LOG_PATH)

        if filename:
            log_file_name = '/'.join([LOG_PATH, filename])
        else:
            log_file_name = '/'.join([LOG_PATH, LOG_FILENAME])
        print log_file_name

        if not format:
            format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

        logging.basicConfig(filename=log_file_name, format=format, level=5)

        logger = logging.getLogger(logger_name)

        return logger

 

 
 
Reference :
python - Using a class' __new__ method as a Factory: __init__ gets called twice - Stack Overflow

沒有留言:

張貼留言