Changeset 966 for trunk/Monitoring


Ignore:
Timestamp:
11/11/13 20:19:13 (10 years ago)
Author:
jripsl
Message:
  • improve log management.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Monitoring/Watch/watch

    r940 r966  
    1818import sys 
    1919import signal 
    20 import traceback 
    2120import smtplib 
    2221from email.mime.text import MIMEText 
     
    3736CSTE_LOG_FILENAME_MSG='message.log' # log AMQP msgs 
    3837CSTE_LOG_FILE_MAIN="%s/%s"%(CSTE_LOG_DIR,CSTE_LOG_FILENAME_MAIN) 
    39  
    40 # logger init. 
    41 logging.basicConfig(filename=CSTE_LOG_FILE_MAIN,level=logging.INFO,) 
    42  
     38CSTE_LOG_FILE_MSG="%s/%s"%(CSTE_LOG_DIR,CSTE_LOG_FILENAME_MSG) 
     39 
     40def create_logger(name,fullpath_filename): 
     41 
     42        # new logger instance 
     43        logger = logging.getLogger(name) 
     44        logger.setLevel(logging.DEBUG) 
     45 
     46        # create associated file 
     47        fh = logging.FileHandler(fullpath_filename) 
     48        fh.setLevel(logging.DEBUG) 
     49 
     50        # create formatter 
     51        formatter = logging.Formatter('%(asctime)-15s - %(message)s') 
     52        fh.setFormatter(formatter) 
     53 
     54        # binding 
     55        logger.addHandler(fh) 
     56 
     57        return logger 
     58 
     59 
     60 
     61 
     62# loggers init. 
     63logger=create_logger(CSTE_LOG_FILENAME_MAIN,CSTE_LOG_FILE_MAIN) 
     64msg_logger=create_logger(CSTE_LOG_FILENAME_MSG,CSTE_LOG_FILE_MSG) 
    4365 
    4466class Mail(): 
     
    81103 
    82104                except: 
    83                         traceback.print_exc() 
    84105                        raise 
    85106 
     
    100121 
    101122                except: 
    102                         traceback.print_exc() 
    103123                        raise 
    104124 
     
    144164        def log_msg(cls,message): 
    145165                line="%s %s %s %s"%(message.code,message.jobid,message.timestamp,message.command) 
    146                 cls.log(CSTE_LOG_FILENAME_MSG,line) 
     166                msg_logger.info(line) 
    147167 
    148168                """ 
     
    164184                                getattr(Actions, proc_name)(message) 
    165185                        except Exception,e: 
    166                                 traceback.print_exc() 
    167  
    168                                 raise Exception("WATCH-ERR002","procedure error (%s,%s)"%(proc_name,str(e))) 
     186                                logger.exception("ERR909 - exception occurs") 
     187 
     188                                raise Exception("WATCH-ERR002","procedure error (%s)"%(proc_name,)) 
    169189 
    170190class MessageActionsMapping(): 
    171191 
    172         # debug 
    173         # 
    174192        # TAG0001: note that crea_sim must be BEFORE store_msg in the list (because when we insert the msg, we need the simu_id) 
    175193        # 
     
    180198                                "2000":["log_msg", "store_msg"], 
    181199                                "3000":["log_msg", "store_msg"], 
     200                                "7000":["log_msg"], 
    182201                                "8888":["cleanup"], 
    183202                                "9000":["log_msg", "store_msg"], 
    184203                                "9999":["log_msg", "store_msg", "set_sim_status_to_error"] } 
    185204 
    186         # prod 
    187         # 
    188         # TAG0001: note that crea_sim must be BEFORE store_msg in the list (because when we insert the msg, we need the simu_id) 
    189         # 
     205        # prod (mail added for some action) 
    190206        """ 
    191         mapping = { "0000":["crea_sim", "log_msg", "store_msg"], 
    192                                 "0100":["log_msg", "store_msg", "set_sim_status_to_complete"], 
    193                                 "1000":["log_msg", "store_msg"], 
    194                                 "1100":["log_msg", "store_msg"], 
    195                                 "2000":["log_msg", "store_msg"], 
    196                                 "3000":["log_msg", "store_msg"], 
    197                                 "8888":["cleanup"], 
    198207                                "9000":["log_msg", "store_msg", "mail"], 
    199208                                "9999":["log_msg", "store_msg", "set_sim_status_to_error", "mail"] } 
     
    229238                self.channel = connection.channel() 
    230239 
    231                 logging.info("[*] Waiting for messages") 
     240                logger.info("[*] Waiting for messages") 
    232241 
    233242                def callback(ch, method, properties, raw_msg): 
     
    247256 
    248257                                # debug 
    249                                 #logging.debug(" [x] Received %s"%field) 
     258                                #logger.debug(" [x] Received %s"%field) 
    250259 
    251260                                splitted_field=field.split(":") 
     
    258267 
    259268                        # debug 
    260                         #logging.debug(" [x] Received %s (encoded)" % l__tmp_dic["body"]) 
     269                        #logger.debug(" [x] Received %s (encoded)" % l__tmp_dic["body"]) 
    261270 
    262271                         
     
    266275 
    267276                        # debug 
    268                         #logging.debug(" [x] Received %s" % raw_msg) 
    269                         #logging.debug(" [x] Received %s (uudecoded)" % base64_decoded_msg ) 
    270                         #logging.debug(" [x] Received %s (uudecoded)" % base64_decoded_msg ) 
     277                        #logger.debug(" [x] Received %s" % raw_msg) 
     278                        #logger.debug(" [x] Received %s (uudecoded)" % base64_decoded_msg ) 
     279                        #logger.debug(" [x] Received %s (uudecoded)" % base64_decoded_msg ) 
    271280 
    272281 
     
    278287 
    279288                                # non working 
    280                                 #logging.debug("DEB003 - %s"%message.type) 
     289                                #logger.debug("DEB003 - %s"%message.type) 
    281290 
    282291                                # working 
    283                                 #logging.debug("DEB009 - %s"%message.code) 
     292                                #logger.debug("DEB009 - %s"%message.code) 
    284293 
    285294 
     
    287296                        except Exception,e: 
    288297 
    289                                 logging.exception("ERR009 - exception occurs (exception=%s)"%(str(e),)) 
     298                                logger.exception("ERR009 - exception occurs") 
    290299 
    291300                                Actions.log_debug("DEB021 - %s"%base64_decoded_msg)  
     
    313322 
    314323                        except Exception,e: 
    315                                 logging.exception("ERR019 - exception occurs (exception=%s)"%(str(e),)) 
     324                                logger.exception("ERR019 - exception occurs") 
    316325 
    317326                                Actions.log_debug("DEB020 - %s"%base64_decoded_msg)  
     
    323332                        #time.sleep(0.5) 
    324333 
    325                 self.channel.queue_declare(queue='myqueue') 
     334                self.channel.queue_declare(queue='myqueue',durable=True) 
    326335 
    327336                self.channel.basic_consume(callback, queue='myqueue', no_ack=True) 
     
    335344 
    336345def signal_handler(signal, frame): 
    337                 logging.info("TERM signal received: exiting.") 
     346                logger.info("TERM signal received: exiting.") 
    338347                Watcher.channel.stop_consuming() 
    339348                Watcher.stop() 
     
    357366        except Exception, e: 
    358367 
    359                 traceback.print_exc() 
     368                logger.exception("ERR904 - exception occurred") 
    360369 
    361370                sys.exit(1) 
Note: See TracChangeset for help on using the changeset viewer.