Changeset 940 for trunk/Monitoring/Watch


Ignore:
Timestamp:
08/26/13 09:45:18 (11 years ago)
Author:
jripsl
Message:

Improve logging.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Monitoring/Watch/watch

    r937 r940  
    2222from email.mime.text import MIMEText 
    2323import datetime 
     24import logging 
    2425 
    2526# line below is to include "smon" package in the search path 
     
    3132CSTE_BROKER_HOST='cstest-broker.ipsl.jussieu.fr' # cstest 
    3233#CSTE_BROKER_HOST='localhost' # vesg4 
     34CSTE_LOG_DIR='/var/log/cssupervisor' 
     35CSTE_LOG_FILENAME_MAIN='supervisor.log' 
     36CSTE_LOG_FILENAME_DEBUG='debug.log' 
     37CSTE_LOG_FILENAME_MSG='message.log' # log AMQP msgs 
     38CSTE_LOG_FILE_MAIN="%s/%s"%(CSTE_LOG_DIR,CSTE_LOG_FILENAME_MAIN) 
     39 
     40# logger init. 
     41logging.basicConfig(filename=CSTE_LOG_FILE_MAIN,level=logging.INFO,) 
     42 
    3343 
    3444class Mail(): 
     
    116126                        simulation=smon.types.Simulation(name=message.simuid,status="running") 
    117127 
    118  
    119128                        repo_io.create_simulation(simulation) 
    120129 
     
    124133 
    125134        @classmethod 
    126         def print_stdout(cls,message): 
    127                 # used for debug 
     135        def log_debug(cls,line): 
     136                cls.log(CSTE_LOG_FILENAME_DEBUG,line) 
     137 
     138        @classmethod 
     139        def log(cls,filename,line): 
     140                with open("%s/%s"%(CSTE_LOG_DIR,filename), "a") as log_file: 
     141                        log_file.write("%s %s\n"%(datetime.datetime.now().strftime('%Y%m%d_%H%M%S'), line)) 
     142 
     143        @classmethod 
     144        def log_msg(cls,message): 
     145                line="%s %s %s %s"%(message.code,message.jobid,message.timestamp,message.command) 
     146                cls.log(CSTE_LOG_FILENAME_MSG,line) 
    128147 
    129148                """ 
    130149                if message.file is not None: 
    131                         print "%s %s %s %s %s\n"%(message.code,message.jobid,message.command,message.timestamp,message.file) 
     150                        "%s %s %s %s %s\n"%(message.code,message.jobid,message.command,message.timestamp,message.file) 
    132151                else: 
    133                         print "%s %s %s %s\n"%(message.code,message.jobid,message.command,message.timestamp) 
    134                 """ 
    135  
    136                 print "%s %s %s %s\n"%(message.code,message.jobid,message.command,message.timestamp) 
    137                 #pass 
    138  
    139         @classmethod 
    140         def log(cls,message): 
    141                 with open("/opt/supervisor/log/supervisor.log", "a") as log_file: 
    142                         log_file.write("%s %s %s %s %s\n"%(datetime.datetime.now().strftime('%Y%m%d_%H%M%S'), message.code,message.jobid,message.timestamp,message.command)) 
     152                "%s %s %s %s\n"%(message.code,message.jobid,message.command,message.timestamp) 
     153                """ 
    143154 
    144155        @classmethod 
     
    163174        # TAG0001: note that crea_sim must be BEFORE store_msg in the list (because when we insert the msg, we need the simu_id) 
    164175        # 
    165         mapping = { "0000":["crea_sim", "log", "store_msg", "print_stdout"], 
    166                                 "0100":["log", "store_msg", "print_stdout", "set_sim_status_to_complete"], 
    167                                 "1000":["log", "store_msg", "print_stdout"], 
    168                                 "1100":["log", "store_msg", "print_stdout"], 
    169                                 "2000":["log", "store_msg", "print_stdout"], 
    170                                 "3000":["log", "store_msg", "print_stdout"], 
     176        mapping = { "0000":["crea_sim", "log_msg", "store_msg"], 
     177                                "0100":["log_msg", "store_msg", "set_sim_status_to_complete"], 
     178                                "1000":["log_msg", "store_msg"], 
     179                                "1100":["log_msg", "store_msg"], 
     180                                "2000":["log_msg", "store_msg"], 
     181                                "3000":["log_msg", "store_msg"], 
    171182                                "8888":["cleanup"], 
    172                                 "9000":["log", "store_msg", "print_stdout"], 
    173                                 "9999":["log", "store_msg", "print_stdout", "set_sim_status_to_error"] } 
     183                                "9000":["log_msg", "store_msg"], 
     184                                "9999":["log_msg", "store_msg", "set_sim_status_to_error"] } 
    174185 
    175186        # prod 
     
    178189        # 
    179190        """ 
    180         mapping = { "0000":["crea_sim", "log", "store_msg"], 
    181                                 "0100":["log", "store_msg", "set_sim_status_to_complete"], 
    182                                 "1000":["log", "store_msg"], 
    183                                 "1100":["log", "store_msg"], 
    184                                 "2000":["log", "store_msg"], 
    185                                 "3000":["log", "store_msg"], 
     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"], 
    186197                                "8888":["cleanup"], 
    187                                 "9000":["log", "store_msg", "mail"], 
    188                                 "9999":["log", "store_msg", "set_sim_status_to_error", "mail"] } 
     198                                "9000":["log_msg", "store_msg", "mail"], 
     199                                "9999":["log_msg", "store_msg", "set_sim_status_to_error", "mail"] } 
    189200        """ 
    190201 
     
    218229                self.channel = connection.channel() 
    219230 
    220  
    221                 print ' [*] Waiting for messages. To exit press CTRL+C' 
     231                logging.info("[*] Waiting for messages") 
    222232 
    223233                def callback(ch, method, properties, raw_msg): 
     
    237247 
    238248                                # debug 
    239                                 #print " [x] Received %s" % field 
     249                                #logging.debug(" [x] Received %s"%field) 
    240250 
    241251                                splitted_field=field.split(":") 
     
    248258 
    249259                        # debug 
    250                         #print " [x] Received %s (encoded)" % l__tmp_dic["body"] 
     260                        #logging.debug(" [x] Received %s (encoded)" % l__tmp_dic["body"]) 
    251261 
    252262                         
     
    256266 
    257267                        # debug 
    258                         #print " [x] Received %s" % raw_msg 
    259                         #print " [x] Received %s (uudecoded)" % base64_decoded_msg  
    260                         #print " [x] Received %s (uudecoded)" % base64_decoded_msg  
     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 ) 
    261271 
    262272 
     
    268278 
    269279                                # non working 
    270                                 #print message.type 
     280                                #logging.debug("DEB003 - %s"%message.type) 
    271281 
    272282                                # working 
    273                                 #print message.code 
     283                                #logging.debug("DEB009 - %s"%message.code) 
    274284 
    275285 
    276286 
    277287                        except Exception,e: 
    278                                 print "ERR009 - exception occurs (exception=%s,msg=%s)"%(str(e),base64_decoded_msg) 
    279  
    280                                 traceback.print_exc() 
     288 
     289                                logging.exception("ERR009 - exception occurs (exception=%s)"%(str(e),)) 
     290 
     291                                Actions.log_debug("DEB021 - %s"%base64_decoded_msg)  
     292 
    281293                                raise 
    282294 
     
    301313 
    302314                        except Exception,e: 
    303                                 print "ERR019 - exception occurs (exception=%s)"%(str(e)) 
    304                                 #print "ERR019 - exception occurs (exception=%s,msg=%s)"%(str(e),base64_decoded_msg) 
    305  
    306                                 traceback.print_exc() 
     315                                logging.exception("ERR019 - exception occurs (exception=%s)"%(str(e),)) 
     316 
     317                                Actions.log_debug("DEB020 - %s"%base64_decoded_msg)  
    307318 
    308319                                raise 
     
    324335 
    325336def signal_handler(signal, frame): 
    326                 print 'You pressed Ctrl+C!' 
     337                logging.info("TERM signal received: exiting.") 
    327338                Watcher.channel.stop_consuming() 
    328339                Watcher.stop() 
     
    331342if __name__ == '__main__': 
    332343 
     344        signal.signal(signal.SIGTERM, signal_handler) 
    333345        signal.signal(signal.SIGINT, signal_handler) 
    334346 
     
    348360 
    349361                sys.exit(1) 
     362# vim: set ts=4 sw=4 : 
Note: See TracChangeset for help on using the changeset viewer.