Changeset 331


Ignore:
Timestamp:
Jul 20, 2011, 9:46:04 PM (13 years ago)
Author:
Rick van der Zwet
Message:
  • File Based logging.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • py-tcpmultiplexer/TCPMultiplexer.py

    r325 r331  
    1212import argparse
    1313import logging
     14import logging.handlers
    1415import os
    1516import socket
     
    2526DEFAULT_DOCUMENTROOT = './htdocs'
    2627DEFAULT_TIMEOUT = None
     28DEFAULT_LOGFILE = 'py-tcpmultiplexer.log'
    2729
    2830
     
    4446recv_timeout = None
    4547
    46 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
     48logger = logging.getLogger(__name__)
     49logger.setLevel(logging.INFO)
     50formatter = logging.Formatter('%(asctime)s - %(threadName)s - %(levelname)s - %(message)s')
     51
     52ch = logging.StreamHandler()
     53ch.setFormatter(formatter)
     54
     55logger.addHandler(ch)
    4756
    4857class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
     
    106115  """ Fetch the DATA from the WAV or MJPEG Stream """
    107116  global running, dataheader, urlheader, urldata, stream_running, recv_timeout
    108   logger = logging.getLogger('recv_id=' + urlid)
    109117  # Fill buffers if needed
    110118  logger.info("Starting thread '%s' (%s)", url, urlid)
     
    178186        for key in urldata[urlid].keys():
    179187          urldata[urlid][key].append(data)
    180     except IOError, e:
     188    except (urllib2.URLError, IOError) as e:
    181189      #Enforce a connection reset
    182190      logger.warning("URL reset '%s' (%s)", url, e)
     
    191199  parser.add_argument('--host', dest='host', default=DEFAULT_HOST, help='Listen to IP [default: %s]' % DEFAULT_HOST)
    192200  parser.add_argument('--port', dest='port', default=DEFAULT_PORT, type=int, help='Listen to PORT [default: %s]' % DEFAULT_PORT)
     201  parser.add_argument('--logfile', dest='logfile', default=DEFAULT_LOGFILE, help='File to write logfiles to [default: %s]' % DEFAULT_LOGFILE)
    193202  parser.add_argument('--timeout', dest='timeout', default=DEFAULT_TIMEOUT, type=int, help='Default socket timeout [default: %s]' % DEFAULT_TIMEOUT)
    194203  parser.add_argument('--stream-cfg', dest='stream_cfg', default=DEFAULT_CONFIG, help='YAML Stream configuration [default: %s]' % DEFAULT_CONFIG)
     
    196205  args = parser.parse_args()
    197206
     207  # Add file logger
     208  ch = logging.handlers.WatchedFileHandler(args.logfile)
     209  ch.setFormatter(formatter)
     210  logger.addHandler(ch)
     211 
    198212  document_root = os.path.abspath(os.path.join(os.path.dirname(__file__),args.document_root))
    199   logging.info("Serving '/htdocs' from document_root '%s'", document_root)
     213  logger.info("Serving '/htdocs' from document_root '%s'", document_root)
    200214
    201215  # Set the timeout
    202   logging.info("Changing socket timeout from '%s' to '%s'", socket.getdefaulttimeout(), args.timeout)
     216  logger.info("Changing socket timeout from '%s' to '%s'", socket.getdefaulttimeout(), args.timeout)
    203217  recv_timeout = args.timeout
     218
    204219
    205220  # Inport streams
     
    209224    streams.update(yaml.load(open(args.stream_cfg)))
    210225  except (ImportError, IOError) as e:
    211     logging.warning("Stream config file '%s' not readable or parsable (%s)", args.stream_cfg, e)
     226    logger.warning("Stream config file '%s' not readable or parsable (%s)", args.stream_cfg, e)
    212227
    213228
     
    220235        break
    221236      except IOError, e:
    222         logging.warning('For conection %s:%s to become available (%s)', args.host, args.port , e)
     237        logger.warning('For conection %s:%s to become available (%s)', args.host, args.port , e)
    223238        time.sleep(1)
    224239  except KeyboardInterrupt:
     
    226241
    227242  for urlid, url in streams.iteritems():
    228     recv_threads.append(threading.Thread(target=get_data, args=(url,urlid)))
     243    recv_threads.append(threading.Thread(name=urlid, target=get_data, args=(url,urlid)))
    229244    recv_threads[-1].setDaemon(True)
    230245    recv_threads[-1].start()
     
    233248  # interrupt the program with Ctrl-C
    234249  try:
    235     logging.info('Serving at %s:%s', args.host, args.port)
     250    logger.info('Serving at %s:%s', args.host, args.port)
    236251    server.serve_forever()
    237252  except KeyboardInterrupt, IOError:
    238     logging.info('Shutting down, please wait...')
     253    logger.info('Shutting down, please wait...')
    239254    running = False
    240255    server.shutdown()
    241256    [thread.join() for thread in recv_threads]
    242     logging.info('All done, good bye!')
    243 
     257    logger.info('All done, good bye!')
     258
Note: See TracChangeset for help on using the changeset viewer.