I have a chain of python scripts which are working perfectly fine. What I am trying to do is to using python logging module to log all the print statements I have in this script (forget about the purpose of this script. All I am trying to do is to add logging capability to save all those print statements intended to show up on console to be logged in to a log file):
from __future__ import print_function
#import MySQLdb
import pymysql
import shutil
import os
import sys
import io
import string
import traceback
import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
from colorama import *
init()
# ==== FOR THE TRANSFORMATION SCRIPT ====
from datetime import tzinfo, timedelta, datetime
import pytz
import codecs
from pytz import timezone
import csv
# =======================================
if not os.path.exists('Archive'):
os.mkdir("Archive")
if not os.path.exists('Failed'):
os.mkdir("Failed")
class MyHandler(PatternMatchingEventHandler):
patterns = ["*.csv","*.processing", "*.transforming","*.loading"]
def process(self, event):
"""
event.event_type
'modified' | 'created' | 'moved' | 'deleted'
event.is_directory
True | False
event.src_path
path/to/observed/file
"""
eventFileName = event.src_path
eventType = event.event_type
if eventType == 'moved':
eventFileName = event.dest_path
fileNameWithPath, fileExtension = os.path.splitext(eventFileName)
if fileExtension == '.csv':
print (Back.GREEN +'Entering copier.py...'+ Style.RESET_ALL)
execfile('./copier_MySQL.py')
#if fileExtension == '.processing':
# print (Back.GREEN +'Entering parser_MySQL.py...'+ Style.RESET_ALL)
# execfile('./parser_MySQL.py')
if fileExtension == '.processing': #change this to '.transforming' if the above parser_MySQL.py is uncommented
t = time.time()
print (Back.GREEN +'Entering transform_MySQL.py...'+ Style.RESET_ALL)
execfile('./transform_MySQL.py')
elapsed = time.time() - t
print (Back.MAGENTA +'Time took to transform the file = '+str(elapsed)+ Style.RESET_ALL)
print (Back.BLUE + "Exiting transform_MySQL.py..." + Style.RESET_ALL)
print (Fore.YELLOW + "-----------#*#*#*-----------#*#*#*-----------" + Style.RESET_ALL)
if fileExtension == '.loading':
if os.path.isfile(eventFileName):
t = time.time()
print (Back.GREEN +'Entering sweeper.py...'+ Style.RESET_ALL)
execfile('./sweeper_MySQL.py')
elapsed = time.time() - t
print (Back.MAGENTA +'Time took to load the file into database = '+str(elapsed-1)+ Style.RESET_ALL)
print (Back.BLUE +"Disconnected from the MySQL server. Exiting sweeper.py..."+ Style.RESET_ALL)
print (Fore.YELLOW+'-----------#*#*#*-----------#*#*#*-----------'+ Style.RESET_ALL)
else:
print (Fore.RED+eventFileName+' is not created by transform_MySQL.py. Check the above messages. NOT executing sweeper_MySQL.py'+ Style.RESET_ALL)
def on_moved(self, event):
self.process(event)
def on_created(self, event):
self.process(event)
if __name__ == '__main__':
observer = Observer()
observer.schedule(MyHandler(), path='.')
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
I was looking at here and tried a few things but not able to quite fit that solution in my code. Any help is much appreciated.