Skip to content

Commit

Permalink
refine code and update the logging config in example
Browse files Browse the repository at this point in the history
  • Loading branch information
wjo1212 committed Jan 2, 2019
1 parent 1d40576 commit 86ed8b2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
23 changes: 8 additions & 15 deletions tests/consumer_group_examples/keyword_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
from multiprocessing import current_process
import re

logging.basicConfig(level=logging.INFO,
format='[%(asctime)s] - [%(threadName)s] - {%(module)s:%(funcName)s:%(lineno)d} %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[RotatingFileHandler("keyword_monitor_{0}.log".format(current_process().pid), maxBytes=100*1024*1024, backupCount=5),
logging.StreamHandler()])
# configure logging file
root = logging.getLogger()
handler = RotatingFileHandler("{0}_{1}.log".format(os.path.basename(__file__), current_process().pid), maxBytes=100*1024*1024, backupCount=5)
handler.setFormatter(logging.Formatter(fmt='[%(asctime)s] - [%(threadName)s] - {%(module)s:%(funcName)s:%(lineno)d} %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
root.setLevel(logging.INFO)
root.addHandler(handler)
root.addHandler(logging.StreamHandler())

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -124,16 +126,7 @@ def main():

logger.info("*** start to consume data...")
worker = ConsumerWorker(KeywordMonitor, option, args=(keywords,) )
worker.start()

try:
while worker.is_alive():
worker.join(timeout=60)
logger.info("worker exit unexpected, try to shutdown it")
worker.shutdown()
except KeyboardInterrupt:
logger.info("*** try to exit **** ")
worker.shutdown()
worker.start(join=True)


if __name__ == '__main__':
Expand Down
23 changes: 8 additions & 15 deletions tests/consumer_group_examples/sync_data_to_splunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import socket
import requests

logging.basicConfig(level=logging.INFO,
format='[%(asctime)s] - [%(threadName)s] - {%(module)s:%(funcName)s:%(lineno)d} %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[RotatingFileHandler("sync_data_to_splunk_{0}.log".format(current_process().pid), maxBytes=100*1024*1024, backupCount=5),
logging.StreamHandler()])
# configure logging file
root = logging.getLogger()
handler = RotatingFileHandler("{0}_{1}.log".format(os.path.basename(__file__), current_process().pid), maxBytes=100*1024*1024, backupCount=5)
handler.setFormatter(logging.Formatter(fmt='[%(asctime)s] - [%(threadName)s] - {%(module)s:%(funcName)s:%(lineno)d} %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
root.setLevel(logging.INFO)
root.addHandler(handler)
root.addHandler(logging.StreamHandler())

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -160,16 +162,7 @@ def main():

logger.info("*** start to consume data...")
worker = ConsumerWorker(SyncData, option, args=(settings,) )
worker.start()

try:
while worker.is_alive():
worker.join(timeout=60)
logger.info("worker exit unexpected, try to shutdown it")
worker.shutdown()
except KeyboardInterrupt:
logger.info("*** try to exit **** ")
worker.shutdown()
worker.start(join=True)


if __name__ == '__main__':
Expand Down

0 comments on commit 86ed8b2

Please sign in to comment.