add: log messages woth python module 'logging' instead of print

This commit is contained in:
Bernhard Stampfer
2016-05-07 20:51:06 +02:00
parent d124755c88
commit 54ce22c0c1
6 changed files with 36 additions and 28 deletions

View File

@@ -3,7 +3,7 @@ from flask import session, render_template
from user import User from user import User
from database import * from database import *
from functools import wraps from functools import wraps
import logging
def requires_baron(fn): def requires_baron(fn):
@wraps(fn) @wraps(fn)

View File

@@ -8,6 +8,7 @@ from deposit import Deposit
import random as rand import random as rand
import datetime import datetime
from settings import settings from settings import settings
import logging
DATABASE = 'test/database.db' DATABASE = 'test/database.db'
@@ -28,13 +29,11 @@ def query_db(query, args=(), one=False):
try: try:
db = get_db() db = get_db()
except RuntimeError: except RuntimeError:
print "GUI DB acces" logging.info("GUI DB access")
db = sqlite3.connect(DATABASE) db = sqlite3.connect(DATABASE)
closeflag = True closeflag = True
print query logging.info("Database query: " + str(query) + " args: " + str(args))
print args
#print "Sqlite: " + query % args
cur = db.execute(query, args) cur = db.execute(query, args)
rows = cur.fetchall() rows = cur.fetchall()
cur.close() cur.close()
@@ -58,7 +57,7 @@ def get_user(u):
u.isbaron=row[7] u.isbaron=row[7]
u.isshown=row[8] u.isshown=row[8]
u.autoblack=row[9] u.autoblack=row[9]
print u logging.info(u)
return u return u
def get_user_by_name(name): def get_user_by_name(name):
@@ -76,7 +75,7 @@ def get_user_by_name(name):
u.isbaron=row[7] u.isbaron=row[7]
u.isshown=row[8] u.isshown=row[8]
u.autoblack=row[9] u.autoblack=row[9]
print u logging.debug(u)
return u return u
def get_user_by_rfid(rfidid): def get_user_by_rfid(rfidid):
@@ -94,7 +93,7 @@ def get_user_by_rfid(rfidid):
u.isbaron=row[7] u.isbaron=row[7]
u.isshown=row[8] u.isshown=row[8]
u.autoblack=row[9] u.autoblack=row[9]
print u logging.debug(u)
return u return u
def get_users(): def get_users():
@@ -196,8 +195,7 @@ def get_consumed(user=None, startdate=None, enddate=None):
def add_consume(username, productid): def add_consume(username, productid):
consumerid = query_db("SELECT ID FROM USERS WHERE NAME = ?", [username], one=True) consumerid = query_db("SELECT ID FROM USERS WHERE NAME = ?", [username], one=True)
print "consumerid = " logging.info("consumerid = " + str(consumerid))
print consumerid
consumerid = int(consumerid[0]) consumerid = int(consumerid[0])
product = get_product_by_id(productid) product = get_product_by_id(productid)
#INSERT INTO USERS (NAME, PASSWORD, LONGNAME, EMAIL, RFID_ID) VALUES (? ,? ,?, ?, ?)", (u.name, u.password, u.longname, u.email, u.rfid_id)) #INSERT INTO USERS (NAME, PASSWORD, LONGNAME, EMAIL, RFID_ID) VALUES (? ,? ,?, ?, ?)", (u.name, u.password, u.longname, u.email, u.rfid_id))
@@ -209,7 +207,7 @@ def add_consume(username, productid):
u = get_user_by_name(username) u = get_user_by_name(username)
u.isblack = True u.isblack = True
update_user(u) update_user(u)
print "consumed" logging.info("consumed")
return return
@@ -256,7 +254,7 @@ def add_deposit(username, amount):
u = get_user_by_name(username) u = get_user_by_name(username)
u.isblack = False u.isblack = False
update_user(u) update_user(u)
print "deposit" logging.info("deposit")
return return
@@ -286,7 +284,7 @@ def generate_test_consumptions():
p = 1 + int(rand.random() * (1.0*num_p)) p = 1 + int(rand.random() * (1.0*num_p))
daysa = int(rand.random() * (30.0)) daysa = int(rand.random() * (30.0))
add_test_consume(u,p,daysa) add_test_consume(u,p,daysa)
print 'trying to add ' + str(p) + ' to ' + str(u) logging.info('trying to add ' + str(p) + ' to ' + str(u))
return return
def add_test_consume(consumerid, productid, daysago): def add_test_consume(consumerid, productid, daysago):

View File

@@ -72,13 +72,13 @@ class MainWindow(wx.Frame):
self.active = 4 #Sorry Bro self.active = 4 #Sorry Bro
else: else:
self.active = 1 #Drinks self.active = 1 #Drinks
print "switching panels", self.active logging.info("switching panels: " + str(self.active))
#has to be called from main thread!! #has to be called from main thread!!
wx.CallAfter(self.switchPanels) wx.CallAfter(self.switchPanels)
def onProduct(self, e): def onProduct(self, e):
self.active = 3 self.active = 3
print self.user.longname + ' consumes' logging.info(self.user.longname + ' consumes')
self.drinkl = e.GetEventObject().GetLabelText() self.drinkl = e.GetEventObject().GetLabelText()
drink = get_product_by_name(self.drinkl.split('\n')[0]).id drink = get_product_by_name(self.drinkl.split('\n')[0]).id
with app.app_context(): with app.app_context():
@@ -112,7 +112,7 @@ class MainWindow(wx.Frame):
try: try:
self.panelThanks.bitmap_2.SetBitmap(wx.Bitmap("./app/static/product_%s.png" % self.drinkl.split('\n')[0], wx.BITMAP_TYPE_ANY)) self.panelThanks.bitmap_2.SetBitmap(wx.Bitmap("./app/static/product_%s.png" % self.drinkl.split('\n')[0], wx.BITMAP_TYPE_ANY))
except: except:
print "no picture for drink:", self.drinkl.split('\n') logging.error("no picture for drink: " + self.drinkl.split('\n')[0])
self.panelThanks.Show() self.panelThanks.Show()
self.delayExit() self.delayExit()
elif active == 4: elif active == 4:

View File

@@ -1,16 +1,18 @@
try: try:
import MFRC522 import MFRC522
except: except:
print "ERROR: Need MFRC522 Library to read RFID tags, disable RFID if no reader is present!" logging.critical("Need MFRC522 Library to read RFID tags, disable RFID if no reader is present!")
exit() exit()
import signal import signal
import thread import thread
import time import time
import logging
class RFID: class RFID:
def __init__(self, callbackf): def __init__(self, callbackf):
logging.info("RFID Reader initialized!")
self.reader = MFRC522.MFRC522() self.reader = MFRC522.MFRC522()
signal.signal(signal.SIGINT, self.stop) signal.signal(signal.SIGINT, self.stop)
self.callback = callbackf self.callback = callbackf
@@ -21,21 +23,21 @@ class RFID:
while True: while True:
while self.loop: while self.loop:
(status, tagtype) = self.reader.MFRC522_Request(self.reader.PICC_REQIDL) (status, tagtype) = self.reader.MFRC522_Request(self.reader.PICC_REQIDL)
print "RFID Status:", status logging.debug("RFID Status: " + str(status))
if status == self.reader.MI_OK: if status == self.reader.MI_OK:
(status, uid) = self.reader.MFRC522_Anticoll() (status, uid) = self.reader.MFRC522_Anticoll()
if status == self.reader.MI_OK: if status == self.reader.MI_OK:
uids = "0x" + "".join(format(x, '02x') for x in uid) uids = "0x" + "".join(format(x, '02x') for x in uid)
print "RFID Detect:", uids logging.info("RFID Detect: " + uids)
self.stop() self.stop()
self.callback(uids) self.callback(uids)
while not self.loop: while not self.loop:
time.sleep(0.1) time.sleep(0.1)
def start(self): def start(self):
print "RFID reader started" logging.info("RFID reader started")
self.loop = True self.loop = True
def stop(self): def stop(self):
print "RFID reader stopped" logging.info("RFID reader stopped")
self.loop = False self.loop = False

View File

@@ -4,6 +4,7 @@ import database
# from email.mime.text import MIMEText # from email.mime.text import MIMEText
# from email.MIMEText import MIMEText # from email.MIMEText import MIMEText
import user import user
import logging
def send_email(recipient, subject, body): def send_email(recipient, subject, body):
gmail_user = 'bier1baroness' gmail_user = 'bier1baroness'
@@ -17,7 +18,7 @@ def send_email(recipient, subject, body):
message = message.encode('utf-8') message = message.encode('utf-8')
# message = msg.as_string() # message = msg.as_string()
print message logging.info(message)
try: try:
server = smtplib.SMTP("smtp.gmail.com:587") server = smtplib.SMTP("smtp.gmail.com:587")
#server.set_debuglevel(1) #server.set_debuglevel(1)
@@ -25,10 +26,10 @@ def send_email(recipient, subject, body):
server.login(gmail_user, gmail_pwd) server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, recipient, message) server.sendmail(FROM, recipient, message)
server.quit() server.quit()
print 'Mail was sent to %s' % recipient logging.info('Mail was sent to %s' % recipient)
except: except:
print "Failed to send mail to %s" %recipient logging.error("Failed to send mail to %s" %recipient)
def send_emails(body, subject, users): def send_emails(body, subject, users):
FROM = 'bier1baroness@gmail.com' FROM = 'bier1baroness@gmail.com'
@@ -39,9 +40,6 @@ def send_emails(body, subject, users):
body_parsed = parse_email(body, user, debt) body_parsed = parse_email(body, user, debt)
send_email(user.email, subject_parsed, body_parsed) send_email(user.email, subject_parsed, body_parsed)
def parse_email(text, u, dept): def parse_email(text, u, dept):
text = text.replace('%%longname%%', u.longname) text = text.replace('%%longname%%', u.longname)

12
run.py
View File

@@ -4,8 +4,18 @@ from app import app
from os import urandom from os import urandom
from app import gui from app import gui
import thread import thread
import logging
if __name__ == '__main__': if __name__ == '__main__':
#logging!
logfile = "baroness.log"
#logging.basicConfig(filename=logfile, level=logging.WARNING)
logging.basicConfig(level=logging.DEBUG)
logging.info("Baroness started!")
print "Baroness started: logging to ", logfile
#start gui #start gui
wx = wx.App() wx = wx.App()
gui.MainWindow(None) gui.MainWindow(None)
@@ -13,6 +23,6 @@ if __name__ == '__main__':
# start flask # start flask
app.secret_key = urandom(24) app.secret_key = urandom(24)
app.run(debug=True) app.run(host="0.0.0.0")
#app.run() #app.run()