2017-11-21 5 views
1

これにいくつかのロギングを追加しようとしていますので、間違ったconfigなどを選択するとこれを記録します。ロガーデバッグpython既存のコードをどのように実装するか。提案:

は次のようなものを追加することを考えていた:

LOG_FILENAME = 'backup.log' 
rotating_handler = RotatingFileHandler(LOG_FILENAME, 
        maxBytes=10000000, 
        backupCount=3) 
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s') 
rotating_handler.setFormatter(formatter) 

ない私のデフread_configにでこれを追加する方法がわから....誰もがさらに助言することはできますか?

#!/usr/bin/python3 
import argparse 
import shutil 
import logging 


parser = argparse.ArgumentParser(description='Copy multiple Files from a specified data file') 
parser.add_argument('-c', '--configfile', default="config.dat", help='file to read the config from') 
parser.add_argument('-l', '--location', default="/home/admin/Documents/backup/",help='Choose location to store files') 


def read_config(data): 
    try: 
     dest = '/home/admin/Documents/backup/' 
     # Read in date from config.dat 
     logger.debug(data = open(data)) 
     # Interate through list of files '\n' 
     filelist = data.read().split('\n') 
     # Copy through interated list and strip white spaces and empty lines 
     for file in filelist: 
      if file: 
       shutil.copy(file.strip(), dest) 
    except FileNotFoundError: 
     logger.error("Config file not found") 



args = vars(parser.parse_args()) 
read = read_config(args['configfile']) 

ありがとうございます。

+1

質問は次のとおりです。「ここでそれは私のログ関連のコードを配置するのがベストでしょうか」? –

答えて

0

は、私はこれを考え出したと私は場合にこれを投稿だろうと思っ考える:

#!/usr/bin/python3 
import argparse 
import shutil 
import logging 
import re 
import pickle 
import sys 
import subprocess 
from logging.handlers import RotatingFileHandler 


parser = argparse.ArgumentParser(description='Copy multiple Files from a specified data file') 
parser.add_argument('-c', '--configfile', default="config.dat", help='file to read the config from') 
parser.add_argument('-l', '--location', default="/home/admin/Documents/backup/",help='Choose location to store files') 

LOG_FILENAME = 'test.log' 
rotating_handler = RotatingFileHandler(LOG_FILENAME, 
        maxBytes=10000000, 
        backupCount=3) 
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s') 
rotating_handler.setFormatter(formatter) 

logger = logging.getLogger('test') 
logger.setLevel(logging.DEBUG) 
logger.addHandler(rotating_handler) 

def read_config(data): 
    try: 
     dest = '/home/admin/Documents/backup/' 
     # Read in date from config.dat 
     data = open(data) 
     # Interate through list of files '\n' 
     filelist = data.read().split('\n') 
     # Copy through interated list and strip white spaces and empty lines 
     for file in filelist: 
      if file: 
       shutil.copy(file.strip(), dest) 
    except FileNotFoundError: 
     logger.error("Config file not found") 
     print ("Config File not found") 

args = vars(parser.parse_args()) 
read = read_config(args['configfile']) 
関連する問題