Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FBLoggingHandler(logging.StreamHandler):
- """
- This creates a custom logging handler object that allows for re-directing
- output to various locations.
- """
- def __init__(self,
- widget=None,
- write_to_logfile=True,
- destFile=None,
- log_to_stdout=False,
- level=logging.INFO):
- """
- The constructor.
- :param log_to_stdout: ``bool`` determining if logging output should be
- redirected to ``stdout``. Defaults to ``False``.
- :param widget: ``QWidget`` to redirect logging output to. Must accept
- text insertion method of some sort. Will be ignored if unspecified.
- :param write_to_logfile: ``bool`` determining if the output should be
- written to a logfile. Defaults to ``True``.
- :param destFile: ``string`` that is path to the logfile to be written to on disk.
- If no file is specified, one will be created based on the application name in
- the user's ``HOME`` folder. Does nothing if ``write_to_logfile`` is set to
- ``False``.
- :param level: ``logging.LEVEL`` that will be the logging level specified to be used
- for writing to disk. Defaults to ``INFO`` level.
- :return: ``None``
- """
- super(FBLoggingHandler, self).__init__()
- self.widget = widget
- self.write_to_logfile = write_to_logfile
- if self.write_to_logfile:
- if destFile and os.path.isfile(destFile):
- self.destFile = destFile
- else:
- self.destFile = os.path.join(
- os.path.expanduser('~'),
- __organization__,
- __application__,
- 'log.txt'
- )
- self.level = level
- # Set logging message output format
- formatter = logging.Formatter('%(asctime)s - %(name)s - '
- '%(levelname)s - %(message)s\n\n')
- self.setFormatter(formatter)
- def flush(self):
- """
- Overloads the base flush() method.
- Does nothing for this handler.
- :return:
- """
- pass
- def emit(self, record, write_mode='a+'):
- """
- Overloads the base emit() method.
- Emits a record to log.
- :param write_mode: ``str`` that determines how the logfile is written to.
- This follows the standard modes that any Python file object handle
- has for writing data to.
- :param record: ``str`` message that will be written to log
- :return: ``str`` that is the logging output.
- """
- try:
- msg = self.format(record)
- if self.write_to_logfile:
- if not os.path.isfile(self.destFile):
- sys.stdout.write('\n\nExisting logfile not found, creating '
- 'default one...\n\n')
- os.makedirs(os.path.dirname(self.destFile))
- write_mode = 'w'
- # Create logfile if it doesn't exist and append the logging info
- with open(self.destFile, mode=write_mode) as log_file:
- log_file.write(msg)
- if hasattr(self.widget, 'insertPlainText') and \
- hasattr(self.widget, 'moveCursor'):
- self.widget.moveCursor(QTextCursor.End)
- self.widget.insertPlainText(self.format(record))
- return msg
- except KeyboardInterrupt:
- raise KeyboardInterrupt
- except SystemExit:
- raise KeyboardInterrupt
- except Exception:
- self.handleError(record)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement