Пример #1
0
        def initLogger(self,
                       loglevel=1,
                       log_dir=None,
                       max_logsize=None,
                       max_logfiles=None):
            import sys
            sys.excepthook = RotatingLogger.handle_exception

            logging.getLogger('apscheduler.scheduler').setLevel(logging.WARN)
            logging.getLogger('apscheduler.threadpool').setLevel(logging.WARN)
            logging.getLogger('apscheduler.scheduler').propagate = False
            logging.getLogger('apscheduler.threadpool').propagate = False
            lg = logging.getLogger('mylar')
            lg.setLevel(logging.DEBUG)

            self.filename = os.path.join(log_dir, self.filename)

            #concurrentLogHandler/0.8.7 (to deal with windows locks)
            #since this only happens on windows boxes, if it's nix/mac use the default logger.
            if mylar.OS_DETECT == 'Windows':
                #set the path to the lib here - just to make sure it can detect cloghandler & portalocker.
                import sys
                sys.path.append(os.path.join(mylar.PROG_DIR, 'lib'))

                try:
                    from ConcurrentLogHandler.cloghandler import ConcurrentRotatingFileHandler as RFHandler
                    mylar.LOGTYPE = 'clog'
                except ImportError:
                    mylar.LOGTYPE = 'log'
                    from logging.handlers import RotatingFileHandler as RFHandler
            else:
                mylar.LOGTYPE = 'log'
                from logging.handlers import RotatingFileHandler as RFHandler

            filehandler = RFHandler(self.filename,
                                    maxBytes=max_logsize,
                                    backupCount=max_logfiles)

            filehandler.setLevel(logging.DEBUG)

            fileformatter = logging.Formatter(
                '%(asctime)s - %(levelname)-7s :: %(message)s',
                '%d-%b-%Y %H:%M:%S')

            filehandler.setFormatter(fileformatter)
            lg.addHandler(filehandler)
            self.filehandler = filehandler

            if loglevel:
                consolehandler = logging.StreamHandler()
                if loglevel == 1:
                    consolehandler.setLevel(logging.INFO)
                if loglevel >= 2:
                    consolehandler.setLevel(logging.DEBUG)
                consoleformatter = logging.Formatter(
                    '%(asctime)s - %(levelname)s :: %(message)s',
                    '%d-%b-%Y %H:%M:%S')
                consolehandler.setFormatter(consoleformatter)
                lg.addHandler(consolehandler)
                self.consolehandler = consolehandler
Пример #2
0
def initLogger(console=False, log_dir=False, init=False, verbose=False):
    #concurrentLogHandler/0.8.7 (to deal with windows locks)
    #since this only happens on windows boxes, if it's nix/mac use the default logger.
    if platform.system() == 'Windows':
        #set the path to the lib here - just to make sure it can detect cloghandler & portalocker.
        import sys
        sys.path.append(os.path.join(mylar.PROG_DIR, 'lib'))

        try:
            from ConcurrentLogHandler.cloghandler import ConcurrentRotatingFileHandler as RFHandler
            mylar.LOGTYPE = 'clog'
        except ImportError:
            mylar.LOGTYPE = 'log'
            from logging.handlers import RotatingFileHandler as RFHandler
    else:
        mylar.LOGTYPE = 'log'
        from logging.handlers import RotatingFileHandler as RFHandler

    if init is True:
        max_size = 1000000 #1 MB
    else:
        if mylar.CONFIG.MAX_LOGSIZE:
            max_size = mylar.CONFIG.MAX_LOGSIZE
        else:
            max_size = 1000000 # 1 MB

    """
    Setup logging for Mylar. It uses the logger instance with the name
    'mylar'. Three log handlers are added:

    * RotatingFileHandler: for the file Mylar.log
    * LogListHandler: for Web UI
    * StreamHandler: for console
    """

    logging.getLogger('apscheduler.scheduler').setLevel(logging.WARN)
    logging.getLogger('apscheduler.threadpool').setLevel(logging.WARN)
    logging.getLogger('apscheduler.scheduler').propagate = False
    logging.getLogger('apscheduler.threadpool').propagate = False


    # Close and remove old handlers. This is required to reinit the loggers
    # at runtime
    for handler in logger.handlers[:]:
        # Just make sure it is cleaned up.
        if isinstance(handler, RFHandler):
            handler.close()
        elif isinstance(handler, logging.StreamHandler):
            handler.flush()

        logger.removeHandler(handler)

    # Configure the logger to accept all messages
    logger.propagate = False

    #1 is WARN level, 2 is ERROR
    if init is True:
        logger.setLevel(logging.DEBUG if verbose else logging.INFO)
    else:
        if mylar.CONFIG.LOG_LEVEL == '1':
            logger.setLevel(logging.DEBUG if verbose else logging.WARN)
        elif mylar.CONFIG.LOG_LEVEL == '2':
            logger.setLevel(logging.DEBUG if verbose else logging.ERROR)
        else:
            logger.setLevel(logging.DEBUG if verbose else logging.INFO)

    # Add list logger
    loglist_handler = LogListHandler()
    loglist_handler.setLevel(logging.DEBUG)
    logger.addHandler(loglist_handler)

    # Setup file logger
    if log_dir:
        filename = os.path.join(log_dir, FILENAME)
        file_formatter = Formatter('%(asctime)s - %(levelname)-7s :: %(threadName)s : %(message)s', '%d-%b-%Y %H:%M:%S')
        file_handler = RFHandler(filename, "a", maxBytes=max_size, backupCount=MAX_FILES)
        file_handler.setLevel(logging.DEBUG)
        file_handler.setFormatter(file_formatter)

        logger.addHandler(file_handler)

    # Setup console logger
    if console:
        console_formatter = logging.Formatter('%(asctime)s - %(levelname)s :: %(threadName)s : %(message)s', '%d-%b-%Y %H:%M:%S')
        console_handler = logging.StreamHandler()
        console_handler.setFormatter(console_formatter)
        console_handler.setLevel(logging.DEBUG)

        logger.addHandler(console_handler)

    # Install exception hooks
    initHooks()
Пример #3
0
    def initLogger(console=False,
                   log_dir=False,
                   init=False,
                   loglevel=1,
                   max_logsize=None,
                   max_logfiles=5):
        #concurrentLogHandler/0.8.7 (to deal with windows locks)
        #since this only happens on windows boxes, if it's nix/mac use the default logger.
        if platform.system() == 'Windows':
            #set the path to the lib here - just to make sure it can detect cloghandler & portalocker.
            import sys
            sys.path.append(os.path.join(mylar.PROG_DIR, 'lib'))

            try:
                from ConcurrentLogHandler.cloghandler import ConcurrentRotatingFileHandler as RFHandler
                mylar.LOGTYPE = 'clog'
            except ImportError:
                mylar.LOGTYPE = 'log'
                from logging.handlers import RotatingFileHandler as RFHandler
        else:
            mylar.LOGTYPE = 'log'
            from logging.handlers import RotatingFileHandler as RFHandler

        if all([init is True, max_logsize is None]):
            max_logsize = 1000000  #1 MB
        else:
            if max_logsize is None:
                max_logsize = 1000000  # 1 MB
        """
        Setup logging for Mylar. It uses the logger instance with the name
        'mylar'. Three log handlers are added:

        * RotatingFileHandler: for the file Mylar.log
        * LogListHandler: for Web UI
        * StreamHandler: for console
        """

        logging.getLogger('apscheduler.scheduler').setLevel(logging.WARN)
        logging.getLogger('apscheduler.threadpool').setLevel(logging.WARN)
        logging.getLogger('apscheduler.scheduler').propagate = False
        logging.getLogger('apscheduler.threadpool').propagate = False

        # Close and remove old handlers. This is required to reinit the loggers
        # at runtime
        for handler in logger.handlers[:]:
            # Just make sure it is cleaned up.
            if isinstance(handler, RFHandler):
                handler.close()
            elif isinstance(handler, logging.StreamHandler):
                handler.flush()

            logger.removeHandler(handler)

        # Configure the logger to accept all messages
        logger.propagate = False

        if init is True:
            logger.setLevel(logging.INFO)
        else:
            if loglevel == 1:  #normal
                logger.setLevel(logging.INFO)
            elif loglevel >= 2:  #verbose
                logger.setLevel(logging.DEBUG)

        # Add list logger
        loglist_handler = LogListHandler()
        loglist_handler.setLevel(logging.DEBUG)
        logger.addHandler(loglist_handler)

        # Setup file logger
        if log_dir:
            filename = os.path.join(log_dir, 'mylar.log')
            file_formatter = Formatter(
                '%(asctime)s - %(levelname)-7s :: %(name)s.%(funcName)s.%(lineno)s : %(threadName)s : %(message)s',
                '%d-%b-%Y %H:%M:%S')
            file_handler = RFHandler(filename,
                                     "a",
                                     maxBytes=max_logsize,
                                     backupCount=max_logfiles)
            if loglevel == 1:  #normal
                file_handler.setLevel(logging.INFO)
            elif loglevel >= 2:  #verbose
                file_handler.setLevel(logging.DEBUG)
            file_handler.setFormatter(file_formatter)

            logger.addHandler(file_handler)

        # Setup console logger
        if console:
            console_formatter = logging.Formatter(
                '%(asctime)s - %(levelname)s :: %(name)s.%(funcName)s.%(lineno)s : %(threadName)s : %(message)s',
                '%d-%b-%Y %H:%M:%S')
            console_handler = logging.StreamHandler()
            console_handler.setFormatter(console_formatter)
            if loglevel == 1:  #normal
                console_handler.setLevel(logging.INFO)
            elif loglevel >= 2:  #verbose
                console_handler.setLevel(logging.DEBUG)

            logger.addHandler(console_handler)

        # Install exception hooks
        initHooks()
Пример #4
0
def initLogger(console=False, log_dir=False, verbose=False):
    #concurrentLogHandler/0.8.7 (to deal with windows locks)
    #since this only happens on windows boxes, if it's nix/mac use the default logger.
    if platform.system() == 'Windows':
        #set the path to the lib here - just to make sure it can detect cloghandler & portalocker.
        import sys
        sys.path.append(os.path.join(mylar.PROG_DIR, 'lib'))

        try:
            from ConcurrentLogHandler.cloghandler import ConcurrentRotatingFileHandler as RFHandler
            mylar.LOGTYPE = 'clog'
        except ImportError:
            mylar.LOGTYPE = 'log'
            from logging.handlers import RotatingFileHandler as RFHandler
    else:
        mylar.LOGTYPE = 'log'
        from logging.handlers import RotatingFileHandler as RFHandler

    if mylar.MAX_LOGSIZE:
        MAX_SIZE = mylar.MAX_LOGSIZE
    else:
        MAX_SIZE = 1000000  # 1 MB
    """
    Setup logging for Mylar. It uses the logger instance with the name
    'mylar'. Three log handlers are added:

    * RotatingFileHandler: for the file Mylar.log
    * LogListHandler: for Web UI
    * StreamHandler: for console
    """

    # Close and remove old handlers. This is required to reinit the loggers
    # at runtime
    for handler in logger.handlers[:]:
        # Just make sure it is cleaned up.
        if isinstance(handler, RFHandler):
            handler.close()
        elif isinstance(handler, logging.StreamHandler):
            handler.flush()

        logger.removeHandler(handler)

    # Configure the logger to accept all messages
    logger.propagate = False
    logger.setLevel(logging.DEBUG if verbose else logging.INFO)

    # Add list logger
    loglist_handler = LogListHandler()
    loglist_handler.setLevel(logging.DEBUG)
    logger.addHandler(loglist_handler)

    # Setup file logger
    if log_dir:
        filename = os.path.join(mylar.LOG_DIR, FILENAME)
        file_formatter = Formatter(
            '%(asctime)s - %(levelname)-7s :: %(threadName)s : %(message)s',
            '%d-%b-%Y %H:%M:%S')
        file_handler = RFHandler(filename,
                                 "a",
                                 maxBytes=MAX_SIZE,
                                 backupCount=MAX_FILES)
        file_handler.setLevel(logging.DEBUG)
        file_handler.setFormatter(file_formatter)

        logger.addHandler(file_handler)

    # Setup console logger
    if console:
        console_formatter = logging.Formatter(
            '%(asctime)s - %(levelname)s :: %(threadName)s : %(message)s',
            '%d-%b-%Y %H:%M:%S')
        console_handler = logging.StreamHandler()
        console_handler.setFormatter(console_formatter)
        console_handler.setLevel(logging.DEBUG)

        logger.addHandler(console_handler)

    # Install exception hooks
    initHooks()
Пример #5
0
def initLogger(verbose=1):
    #concurrentLogHandler/0.8.7 (to deal with windows locks)
    #since this only happens on windows boxes, if it's nix/mac use the default logger.
    if platform.system() == 'Windows':
        #set the path to the lib here - just to make sure it can detect cloghandler & portalocker.
        import sys
        sys.path.append(os.path.join(mylar.PROG_DIR, 'lib'))

        try:
            from ConcurrentLogHandler.cloghandler import ConcurrentRotatingFileHandler as RFHandler
            mylar.LOGTYPE = 'clog'
        except ImportError:
            mylar.LOGTYPE = 'log'
            from logging.handlers import RotatingFileHandler as RFHandler
    else:
        mylar.LOGTYPE = 'log'
        from logging.handlers import RotatingFileHandler as RFHandler


    if mylar.MAX_LOGSIZE:
        MAX_SIZE = mylar.MAX_LOGSIZE
    else:
        MAX_SIZE = 1000000 # 1 MB

    """
    Setup logging for Mylar. It uses the logger instance with the name
    'mylar'. Three log handlers are added:

    * RotatingFileHandler: for the file Mylar.log
    * LogListHandler: for Web UI
    * StreamHandler: for console (if verbose > 0)
    """

    # Configure the logger to accept all messages
    logger.propagate = False
    logger.setLevel(DEBUG)# if verbose == 2 else logging.INFO)

    # Setup file logger
    filename = os.path.join(mylar.LOG_DIR, FILENAME)

    file_formatter = Formatter('%(asctime)s - %(levelname)-7s :: %(threadName)s : %(message)s', '%d-%b-%Y %H:%M:%S')
    file_handler = RFHandler(filename, "a", maxBytes=MAX_SIZE, backupCount=MAX_FILES)
    file_handler.setLevel(DEBUG)
    file_handler.setFormatter(file_formatter)

    logger.addHandler(file_handler)

    # Add list logger
    loglist_handler = LogListHandler()
    #-- this needs to get enabled and logging changed everywhere so the accessing the log GUI won't hang the system.
    #-- right now leave it set to INFO only, everything else will still get logged to the mylar.log file.
    #if verbose == 2:
    #    loglist_handler.setLevel(logging.DEBUG)
    #else:
    #    loglist_handler.setLevel(logging.INFO)
    #--
    loglist_handler.setLevel(INFO)
    logger.addHandler(loglist_handler)

    # Setup console logger
    if verbose:
        console_formatter = Formatter('%(asctime)s - %(levelname)s :: %(threadName)s : %(message)s', '%d-%b-%Y %H:%M:%S')
        console_handler = StreamHandler()
        console_handler.setFormatter(console_formatter)
        #print 'verbose is ' + str(verbose)        
        #if verbose == 2:
        #    console_handler.setLevel(logging.DEBUG)
        #else:
        #    console_handler.setLevel(logging.INFO)
        console_handler.setLevel(INFO)

        logger.addHandler(console_handler)

    # Install exception hooks
    initHooks()
Пример #6
0
def initLogger(verbose=1):
    #concurrentLogHandler/0.8.7 (to deal with windows locks)
    #since this only happens on windows boxes, if it's nix/mac use the default logger.
    if platform.system() == 'Windows':
        #set the path to the lib here - just to make sure it can detect cloghandler & portalocker.
        import sys
        sys.path.append(os.path.join(mylar.PROG_DIR, 'lib'))

        try:
            from ConcurrentLogHandler.cloghandler import ConcurrentRotatingFileHandler as RFHandler
            mylar.LOGTYPE = 'clog'
        except ImportError:
            mylar.LOGTYPE = 'log'
            from logging.handlers import RotatingFileHandler as RFHandler
    else:
        mylar.LOGTYPE = 'log'
        from logging.handlers import RotatingFileHandler as RFHandler

    if mylar.MAX_LOGSIZE:
        MAX_SIZE = mylar.MAX_LOGSIZE
    else:
        MAX_SIZE = 1000000  # 1 MB
    """
    Setup logging for Mylar. It uses the logger instance with the name
    'mylar'. Three log handlers are added:

    * RotatingFileHandler: for the file Mylar.log
    * LogListHandler: for Web UI
    * StreamHandler: for console (if verbose > 0)
    """

    # Configure the logger to accept all messages
    logger.propagate = False
    logger.setLevel(DEBUG)  # if verbose == 2 else logging.INFO)

    # Setup file logger
    filename = os.path.join(mylar.LOG_DIR, FILENAME)

    file_formatter = Formatter(
        '%(asctime)s - %(levelname)-7s :: %(threadName)s : %(message)s',
        '%d-%b-%Y %H:%M:%S')
    file_handler = RFHandler(filename,
                             "a",
                             maxBytes=MAX_SIZE,
                             backupCount=MAX_FILES)
    file_handler.setLevel(DEBUG)
    file_handler.setFormatter(file_formatter)

    logger.addHandler(file_handler)

    # Add list logger
    loglist_handler = LogListHandler()
    #-- this needs to get enabled and logging changed everywhere so the accessing the log GUI won't hang the system.
    #-- right now leave it set to INFO only, everything else will still get logged to the mylar.log file.
    #if verbose == 2:
    #    loglist_handler.setLevel(logging.DEBUG)
    #else:
    #    loglist_handler.setLevel(logging.INFO)
    #--
    loglist_handler.setLevel(INFO)
    logger.addHandler(loglist_handler)

    # Setup console logger
    if verbose:
        console_formatter = Formatter(
            '%(asctime)s - %(levelname)s :: %(threadName)s : %(message)s',
            '%d-%b-%Y %H:%M:%S')
        console_handler = StreamHandler()
        console_handler.setFormatter(console_formatter)
        #print 'verbose is ' + str(verbose)
        #if verbose == 2:
        #    console_handler.setLevel(logging.DEBUG)
        #else:
        #    console_handler.setLevel(logging.INFO)
        console_handler.setLevel(INFO)

        logger.addHandler(console_handler)

    # Install exception hooks
    initHooks()