2016-03-30 17 views
0

我有一個工作得很好的python腳本鏈。我想要做的是使用python日誌記錄模塊來記錄我在這個腳本中的所有打印語句(忘記了這個腳本的目的,我所要做的就是添加日誌記錄功能來保存所有打印語句的打算以顯示在控制檯登錄到一個日誌文件):使用Python日誌記錄到現有的腳本

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() 

我看着here並嘗試了一些東西,但沒能挺適合我的代碼的解決方案。任何幫助深表感謝。

+1

爲什麼不直接輸出重定向到一個文件? 'myscript.py> myscript.log' – russdot

+0

@russdot此腳本用於在未監控的計算機上運行進度(這些打印語句)以便屏幕顯示並將日誌消息保存到文件中,所以'> myscript .log'是我真正希望我可以使用但不能的。 –

+0

@russdot如果你看看xi_的評論,他的回答如下,他鏈接到python的日誌記錄模塊。所有這些功能都會自動添加到日誌信息中,購買一行格式聲明。這樣做手動寫入日誌文件將涉及多行代碼,這有點重新發明輪子,所以我的理由是使用python的日誌記錄模塊的另一個原因。 –

回答

3

您可以使用logging模塊而不是一堆打印件。

import logging 
logging.basicConfig(level=logging.DEBUG) 
logging.debug(Back.GREEN +'Entering sweeper.py...'+ Style.RESET_ALL) 

要節省輸出到文件集:

logging.basicConfig(filename='myscript.log', level=logging.DEBUG) 
+0

這是我的想法,但它將這些打印語句保存到文件? –

+0

@NikhilGupta當然,檢查更新的答案 –

+0

我會嘗試並回來。另外,我可以在該語句中使用多個級別,比如'level = logging.DEBUG,logging.ERROR,logging.INFO'等。 –