Last active
August 29, 2015 14:14
-
-
Save QuantTraderEd/751b303874943a50423e to your computer and use it in GitHub Desktop.
thread logging test
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Feb 1 21:28:22 2015 | |
@author: assa | |
""" | |
import sys | |
import logging | |
from PyQt4 import QtCore, QtGui | |
from logger_ex4_thread import loggerThread | |
logger = logging.getLogger('AppEx4') | |
logger.setLevel(logging.DEBUG) | |
# create file handler which logs even debug messages | |
fh = logging.FileHandler('threadtest.log') | |
fh.setLevel(logging.DEBUG) | |
# create console handler with a higher log level | |
ch = logging.StreamHandler() | |
ch.setLevel(logging.INFO) | |
# create formatter and add it to the handlers | |
formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(name)s %(message)s') | |
fh.setFormatter(formatter) | |
ch.setFormatter(formatter) | |
# add the handler to logger | |
logger.addHandler(fh) | |
logger.addHandler(ch) | |
class MyWidget(QtGui.QWidget): | |
def __init__(self): | |
super(MyWidget,self).__init__() | |
self.initUI() | |
self.initThread() | |
logger.info('Start MyWidget') | |
def initUI(self): | |
self.button = QtGui.QPushButton('Start',self) | |
self.button.setGeometry(80,80,70,40) | |
self.button.move(100,60) | |
self.button.clicked.connect(self.onClicked) | |
self.setGeometry(300, 300, 280, 170) | |
self.setWindowTitle('logging thread test') | |
def initThread(self): | |
self._thread = loggerThread() | |
self._thread.finished.connect(self.endThread) | |
def onClicked(self): | |
if self.button.text() == 'Start' and (not self._thread.isRunning()): | |
self.button.setText('Stop') | |
self._thread.start() | |
pass | |
def endThread(self): | |
self.button.setText('Start') | |
logger.info('Thread finished') | |
pass | |
def main(): | |
app = QtGui.QApplication(sys.argv) | |
wid = MyWidget() | |
wid.show() | |
sys.exit(app.exec_()) | |
if __name__ == '__main__': | |
main() | |
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Feb 2 21:47:22 2015 | |
@author: assa | |
""" | |
import time | |
import logging | |
from PyQt4 import QtCore | |
class loggerThread(QtCore.QThread): | |
def __init__(self): | |
super(loggerThread,self).__init__() | |
self.logger = logging.getLogger('AppEx4.Thread') | |
self.logger.info('Thread initialize') | |
def run(self): | |
self.logger.info('Thread running') | |
time.sleep(5) | |
self.logger.info('Thread stop') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment