Commit 58607a74 authored by Anastasia Galkin's avatar Anastasia Galkin
Browse files

logger added, in progress

parent 18719f55
......@@ -2,42 +2,62 @@
import time
import MySQLdb
import logging
from settings import *
# logger
logger = logging.getLogger('check-wp')
hdlr = logging.FileHandler(LOG_DIR + LOG_FILE)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
logger.info('start check')
# old logfile
log_name = LOG_DIR + time.strftime("%Y%m%d_%H%M%S") + '_wordpress_stats.log'
logfile = open(log_name, "w")
# Open database connection
for i, wp in WORDPRESS_DATABASES.items():
for instance, wp in WORDPRESS_DATABASES.items():
logfile.write("Wordpress: '{0}' ".format(wp['NAME']))
db_name = "{0} ".format(wp['NAME'])
logfile.write(db_name)
nonews = True
try:
db = MySQLdb.connect(wp['HOST'], wp['USER'], wp['PASSWORD'], wp['DATABASE'])
cursor = db.cursor()
# users, posts, pages
for queries, stats in WORDPRESS_STATS.items():
db = MySQLdb.connect(wp['HOST'], wp['USER'], wp['PASSWORD'], wp['DATABASE'])
cursor = db.cursor()
cursor.execute(stats['count'])
count = cursor.fetchone()
# users, posts, pages
for j, stats in WORDPRESS_STATS.items():
if int(count[0]) > 0:
nonews = False
logfile.write(' '+ wp['URL'] + '\n')
logfile.write("{0} new {1}\n".format(''.join(map(str, count)), queries))
logfile.write(stats['header'] + "\n")
cursor.execute(stats['query'])
cursor.execute(stats['count'])
count = cursor.fetchone()
for row in cursor:
logfile.write(', '.join(map(str, row)) + '\n')
if int(count[0]) > 0:
nonews = False
logfile.write(' '+ wp['URL'] + '\n')
logfile.write("{0} new {1}\n".format(''.join(map(str, count)), j))
logfile.write(stats['header'] + "\n")
cursor.execute(stats['query'])
db.close()
for row in cursor:
logfile.write(' | '.join(map(str, row)) + '\n')
except MySQLdb.Error as err:
print(err)
logger.warning(db_name + err.__str__())
# logger.warning("{0} : no connection".format(wp['NAME']))
#logger.warning(''.format(err))
if nonews:
logfile.write('.... \n')
logfile.write(' checked \n')
logfile.write('\n')
db.close()
logfile.close()
logfile.close
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment