Example #1
0
def get_logger_instance(name):
    """
    Logger for all the tests
    Args:
        name (str): test name
    Returns:
        logger: logger obj
    """
    logging.tests = logging.getLogger(name)
    logging.tests.setLevel(logging.DEBUG)

    WO_FUNCTION_FORMAT = '[%(asctime)s,%(msecs)03d] [%(levelname)s] [%(threadName)s] [%(file_line)s] [%(filename)s:%(lineno)s-%(funcName)s()] %(message)s'
    formatter = logging.Formatter(WO_FUNCTION_FORMAT, "%Y-%m-%d %H:%M:%S")

    ROOT_DIR = "/Users/samuel.ip/Documents/Workspace/Learning_Workspace/Python_OOPs/"
    file_name_suffix = datetime.now().strftime('%Y-%m-%d-%f.log')

    debug_handler = FileHandler(
        os.path.join(ROOT_DIR, 'tests', 'test_logs',
                     name + '_' + file_name_suffix), 'a')
    debug_handler.set_name('debug_file')
    debug_handler.setLevel(logging.DEBUG)
    debug_handler.setFormatter(formatter)
    logging.tests.addHandler(debug_handler)

    console_handler = StreamHandler(sys.stdout)

    console_handler.set_name('console_stream')
    console_handler.setLevel(logging.DEBUG)
    console_handler.setFormatter(formatter)
    logging.tests.addHandler(console_handler)
    def publish(self, timestamp, source, data):

        #convert : to _ because windows do not like folders with :
        s = source[0].replace(':', '_')

        #create logger according to app name.

        optionList = data['header'].getOptionList()
        app = optionList[PATH_FIELD]
        app = app[PATH_SUBFIELD]
        strname = "".join(chr(b) for b in app)
        #check if there is an instance of that file handler
        root = logging.getLogger(s + strname)
        root.setLevel(logging.INFO)

        print " ".join(h.get_name() for h in root.handlers)

        for h in root.handlers:
            if isinstance(h, FileHandler):
                name = h.get_name()
                if (name in (s + strname)):  #already exists
                    pass
                else:  #create the new filehandler
                    if not os.path.exists(os.path.curdir + "/" + s):
                        os.makedirs(os.path.curdir + "/" + s)
                    fh = FileHandler(
                        os.path.curdir + "/" + s + "/" + strname + ".csv", "a")
                    #fh=FileHandler(strname +".csv", "a")
                    fh.set_name(s + strname)  #set a name to be able to find it
                    fh.setLevel(logging.INFO)
                    root.addHandler(
                        fh)  #add the handler to the logger only once
            else:  #other handlers
                pass

        #the first time will be added here
        if (len(root.handlers) == 0):

            if not os.path.exists(os.path.curdir + "/" + s):
                os.makedirs(os.path.curdir + "/" + s)
            fh = FileHandler(os.path.curdir + "/" + s + "/" + strname + ".csv",
                             "a")
            fh.setLevel(logging.INFO)
            fh.set_name(s + strname)  #set a name to be able to find it
            root.addHandler(fh)  #add the handler to the logger only once

        #prepare the ; separated tuples to be printed in csv
        x = ";".join(",".join(str(val) for val in dict.itervalues())
                     for dict in data["parsed"])

        root.info("{0},{1}".format(self._formatTimestamp(timestamp), x))
        log.debug("{0},{1}".format(self._formatTimestamp(timestamp), x))
Example #3
0
 def publish(self,timestamp,source,data):
     
     #convert : to _ because windows do not like folders with :
     s=source[0].replace(':','_')
     
     #create logger according to app name.
     
     optionList=data['header'].getOptionList()
     app=optionList[PATH_FIELD]
     app=app[PATH_SUBFIELD]
     strname="".join(chr(b) for b in app)
     #check if there is an instance of that file handler
     root=logging.getLogger(s+strname)
     root.setLevel(logging.INFO)
     
     
     print " ".join(h.get_name() for h in root.handlers)
     
     for h in root.handlers:
         if isinstance(h, FileHandler):
             name=h.get_name()
             if (name in (s+strname) ):#already exists
                 pass
             else:#create the new filehandler
                 if not os.path.exists(os.path.curdir +"/"+ s):
                     os.makedirs(os.path.curdir +"/" + s)
                 fh=FileHandler(os.path.curdir +"/"+ s +"/" +strname +".csv", "a")
                 #fh=FileHandler(strname +".csv", "a")
                 fh.set_name(s+strname)#set a name to be able to find it 
                 fh.setLevel(logging.INFO)
                 root.addHandler(fh)#add the handler to the logger only once
         else:#other handlers 
              pass
                 
     #the first time will be added here
     if (len(root.handlers)==0):
         
         if not os.path.exists(os.path.curdir +"/"+ s):
             os.makedirs(os.path.curdir +"/" + s)    
         fh=FileHandler(os.path.curdir +"/"+ s +"/" + strname +".csv", "a")
         fh.setLevel(logging.INFO)
         fh.set_name(s+strname)#set a name to be able to find it 
         root.addHandler(fh)#add the handler to the logger only once
     
     #prepare the ; separated tuples to be printed in csv
     x=";".join(",".join(str(val) for val in dict.itervalues() ) for dict in data["parsed"])
     
     root.info("{0},{1}".format(self._formatTimestamp(timestamp),x))
     log.debug("{0},{1}".format(self._formatTimestamp(timestamp),x))
Example #4
0
File: notes.py Project: seldin/BAC0
def note_and_log(cls):
    """
    This will be used as a decorator on class to activate
    logging and store messages in the variable cls._notes
    This will allow quick access to events in the web app.
    A note can be added to cls._notes without logging if passing
    the argument log=false to function note()
    Something can be logged without addind a note using function log()
    """
    if hasattr(cls, 'DEBUG_LEVEL'):
        if cls.DEBUG_LEVEL == 'debug':
            file_level = logging.DEBUG
            console_level = logging.DEBUG
        elif cls.DEBUG_LEVEL == 'info':
            file_level = logging.INFO
            console_level = logging.INFO
    else:
        file_level = logging.WARNING
        console_level = logging.INFO
    # Notes object
    cls._notes = namedtuple('_notes', ['timestamp', 'notes'])
    cls._notes.timestamp = []
    cls._notes.notes = []

    # Defining log object
    cls.logname = '{} | {}'.format(cls.__module__, cls.__name__)
    root_logger = logging.getLogger()
    cls._log = logging.getLogger('BAC0')
    if not len(root_logger.handlers):
        root_logger.addHandler(cls._log)

    # Console Handler
    ch = logging.StreamHandler()
    ch.set_name('stderr')
    ch2 = logging.StreamHandler(sys.stdout)
    ch2.set_name('stdout')
    ch.setLevel(console_level)
    ch2.setLevel(logging.CRITICAL)

    formatter = logging.Formatter('{asctime} - {levelname:<8}| {message}',
                                  style='{')

    # File Handler
    _PERMISSION_TO_WRITE = True
    logUserPath = expanduser('~')
    logSaveFilePath = join(logUserPath, '.BAC0')

    logFile = join(logSaveFilePath, 'BAC0.log')
    if not os.path.exists(logSaveFilePath):
        try:
            os.makedirs(logSaveFilePath)
        except:
            _PERMISSION_TO_WRITE = False
    if _PERMISSION_TO_WRITE:
        fh = FileHandler(logFile)
        fh.set_name('file_handler')
        fh.setLevel(file_level)
        fh.setFormatter(formatter)

    ch.setFormatter(formatter)
    ch2.setFormatter(formatter)
    # Add handlers the first time only...
    if not len(cls._log.handlers):
        if _PERMISSION_TO_WRITE:
            cls._log.addHandler(fh)
        cls._log.addHandler(ch)
        cls._log.addHandler(ch2)

#    cls._log.setLevel(logging.CRITICAL)

    def log_title(self, title, args=None, width=35):
        cls._log.debug("")
        cls._log.debug("#" * width)
        cls._log.debug("# {}".format(title))
        cls._log.debug("#" * width)
        if args:
            cls._log.debug("{!r}".format(args))
            cls._log.debug("#" * 35)

    def log_subtitle(self, subtitle, args=None, width=35):
        cls._log.debug("")
        cls._log.debug("=" * width)
        cls._log.debug("{}".format(subtitle))
        cls._log.debug("=" * width)
        if args:
            cls._log.debug("{!r}".format(args))
            cls._log.debug("=" * width)

    def log(self, note, *, level=logging.DEBUG):
        """
        Add a log entry...no note
        """
        if not note:
            raise ValueError('Provide something to log')
        note = '{} | {}'.format(cls.logname, note)
        cls._log.log(level, note)

    def note(self, note, *, level=logging.INFO, log=True):
        """
        Add note to the object. By default, the note will also
        be logged
        :param note: (str) The note itself
        :param level: (logging.level)
        :param log: (boolean) Enable or disable logging of note
        """
        if not note:
            raise ValueError('Provide something to log')
        note = '{} | {}'.format(cls.logname, note)
        cls._notes.timestamp.append(datetime.now())
        cls._notes.notes.append(note)
        if log:
            cls.log(level, note)

    @property
    def notes(self):
        """
        Retrieve notes list as a Pandas Series
        """
        if not _PANDAS:
            return dict(zip(self._notes.timestamp, self._notes.notes))
        return pd.Series(self._notes.notes, index=self._notes.timestamp)

    def clear_notes(self):
        """
        Clear notes object
        """
        cls._notes.timestamp = []
        cls._notes.notes = []

    # Add the functions to the decorated class
    cls.clear_notes = clear_notes
    cls.note = note
    cls.notes = notes
    cls.log = log
    cls.log_title = log_title
    cls.log_subtitle = log_subtitle
    return cls
Example #5
0
def note_and_log(cls):
    """
    This will be used as a decorator on class to activate
    logging and store messages in the variable cls._notes
    This will allow quick access to events in the web app.
    A note can be added to cls._notes without logging if passing
    the argument log=false to function note()
    Something can be logged without addind a note using function log()
    """
    if hasattr(cls, "DEBUG_LEVEL"):
        if cls.DEBUG_LEVEL == "debug":
            file_level = logging.DEBUG
            console_level = logging.DEBUG
        elif cls.DEBUG_LEVEL == "info":
            file_level = logging.INFO
            console_level = logging.INFO
    else:
        file_level = logging.WARNING
        console_level = logging.INFO
    # Notes object
    cls._notes = namedtuple("_notes", ["timestamp", "notes"])
    cls._notes.timestamp = []
    cls._notes.notes = []

    # Defining log object
    cls.logname = "{} | {}".format(cls.__module__, cls.__name__)
    root_logger = logging.getLogger()
    cls._log = logging.getLogger("BAC0")
    if not len(root_logger.handlers):
        root_logger.addHandler(cls._log)

    # Console Handler
    ch = logging.StreamHandler()
    ch.set_name("stderr")
    ch2 = logging.StreamHandler(sys.stdout)
    ch2.set_name("stdout")
    ch.setLevel(console_level)
    ch2.setLevel(logging.CRITICAL)

    formatter = logging.Formatter("{asctime} - {levelname:<8}| {message}", style="{")

    # File Handler
    _PERMISSION_TO_WRITE = True
    logUserPath = expanduser("~")
    logSaveFilePath = join(logUserPath, ".BAC0")

    logFile = join(logSaveFilePath, "BAC0.log")
    if not os.path.exists(logSaveFilePath):
        try:
            os.makedirs(logSaveFilePath)
        except:
            _PERMISSION_TO_WRITE = False
    if _PERMISSION_TO_WRITE:
        fh = FileHandler(logFile)
        fh.set_name("file_handler")
        fh.setLevel(file_level)
        fh.setFormatter(formatter)

    ch.setFormatter(formatter)
    ch2.setFormatter(formatter)
    # Add handlers the first time only...
    if not len(cls._log.handlers):
        if _PERMISSION_TO_WRITE:
            cls._log.addHandler(fh)
        cls._log.addHandler(ch)
        cls._log.addHandler(ch2)

    #    cls._log.setLevel(logging.CRITICAL)

    def log_title(self, title, args=None, width=35):
        cls._log.debug("")
        cls._log.debug("#" * width)
        cls._log.debug("# {}".format(title))
        cls._log.debug("#" * width)
        if args:
            cls._log.debug("{!r}".format(args))
            cls._log.debug("#" * 35)

    def log_subtitle(self, subtitle, args=None, width=35):
        cls._log.debug("")
        cls._log.debug("=" * width)
        cls._log.debug("{}".format(subtitle))
        cls._log.debug("=" * width)
        if args:
            cls._log.debug("{!r}".format(args))
            cls._log.debug("=" * width)

    def log(self, note, *, level=logging.DEBUG):
        """
        Add a log entry...no note
        """
        if not note:
            raise ValueError("Provide something to log")
        note = "{} | {}".format(cls.logname, note)
        cls._log.log(level, note)

    def note(self, note, *, level=logging.INFO, log=True):
        """
        Add note to the object. By default, the note will also
        be logged
        :param note: (str) The note itself
        :param level: (logging.level)
        :param log: (boolean) Enable or disable logging of note
        """
        if not note:
            raise ValueError("Provide something to log")
        note = "{} | {}".format(cls.logname, note)
        cls._notes.timestamp.append(datetime.now())
        cls._notes.notes.append(note)
        if log:
            cls.log(level, note)

    @property
    def notes(self):
        """
        Retrieve notes list as a Pandas Series
        """
        if not _PANDAS:
            return dict(zip(self._notes.timestamp, self._notes.notes))
        return pd.Series(self._notes.notes, index=self._notes.timestamp)

    def clear_notes(self):
        """
        Clear notes object
        """
        cls._notes.timestamp = []
        cls._notes.notes = []

    # Add the functions to the decorated class
    cls.clear_notes = clear_notes
    cls.note = note
    cls.notes = notes
    cls.log = log
    cls.log_title = log_title
    cls.log_subtitle = log_subtitle
    return cls