Commits

FredGan committed 206e200

Логирование по нормальному

Comments (0)

Files changed (1)

 #BACKUP FILES company "AlieParusa"                                       #
 ##########################################################################
 import xml.parsers.expat
+import logging
 import tarfile
 import os
 import modules
 def Archive(paths):
     archivepach="/usr/NAS/backups/"
     date=datetime.now().strftime('%Y'"."'%m'"."'%d')
+    logging.basicConfig(filename=archivepach+'logs'+'_'+date+'.log',format='%(asctime)s %(message)s',level=logging.DEBUG)
     for i in paths:
+        logging.info('Start task: '+str(i))
         tar = tarfile.open(archivepach+str(i)+'_'+date+".tar", "w")
-        log = open(archivepach+str(i)+'_'+date+".log", "w")
-        log.write(datetime.now().strftime('%Y'"."'%m'"."'%d'" "'%H'":"'%m')+" Start task:\n")
         for root, dir, files in os.walk(str(paths[i])):
             for file in files:
                 fullpath = os.path.join(root,file) 
                 try:
                       tar.add(fullpath)
                 except:
-                      log.write(+datetime.now().strftime('%Y'"."'%m'"."'%d'" "'%H'":"'%m')+" "+"ERROR:"+fullpath+'\n')
-        log.write(datetime.now().strftime('%Y'"."'%m'"."'%d'" "'%H'":"'%m')+" Finish task:\n")
-        log.close()
+                      logging.warning("Error: "+fullpath)
+        logging.info('Finish task: '+str(i))
         tar.close()
 #Send email          
         modules.Sendemail(fro_m='service@redsails.ru', to='backup@redsails.ru', body='Job '+str(i)+' is done.' , subject='BackUP')