Fix logging for mqtt_sender script
This commit is contained in:
parent
0cc667dbec
commit
ea94c08939
1 changed files with 30 additions and 13 deletions
|
|
@ -12,13 +12,13 @@ import logging
|
|||
|
||||
|
||||
def sigint_handler(signal, frame):
|
||||
logging.warning('Interrupted')
|
||||
#logging.warning('Interrupted')
|
||||
sys.exit(0)
|
||||
os._exit(0)
|
||||
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
logging.info("Connected with result code "+str(rc))
|
||||
#logging.info("Connected with result code "+str(rc))
|
||||
client.subscribe("$SYS/#")
|
||||
|
||||
|
||||
|
|
@ -27,25 +27,39 @@ def on_publish(client, userdata, mid):
|
|||
pass
|
||||
|
||||
|
||||
|
||||
def getRedis(i, client):
|
||||
def getRedis(i, client, logger):
|
||||
logger.info("Listening in redis events")
|
||||
r = redis.StrictRedis(host='localhost', port=6379, db=0)
|
||||
while 1:
|
||||
time.sleep(0)
|
||||
data = r.brpop("events", 10)
|
||||
if data:
|
||||
info = data[1].split('|')
|
||||
logging.info("Channel: " + info[0] + " message: " + info[1])
|
||||
logger.info("Channel: " + info[0] + " message: " + info[1])
|
||||
client.publish(info[0], info[1])
|
||||
|
||||
|
||||
|
||||
def sigint_handler(signal, frame):
|
||||
logging.warning('Interrupted')
|
||||
sys.exit(0)
|
||||
|
||||
def get_logger():
|
||||
logger = logging.getLogger("mqtt_logger")
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
fh = logging.FileHandler("/tmp/mqtt_sender.log")
|
||||
fmt = '%(asctime)s - %(threadName)s - %(levelname)s - %(message)s'
|
||||
formatter = logging.Formatter(fmt)
|
||||
fh.setFormatter(formatter)
|
||||
|
||||
logger.addHandler(fh)
|
||||
return logger
|
||||
|
||||
def main():
|
||||
logger = get_logger()
|
||||
logger.info("Starting mqtt_sender")
|
||||
logger.info("Connecting to MQTT server")
|
||||
|
||||
client = mqtt.Client()
|
||||
client.on_connect = on_connect
|
||||
client.on_publish = on_publish
|
||||
|
|
@ -55,7 +69,9 @@ def main():
|
|||
tls_version=ssl.PROTOCOL_TLSv1)
|
||||
|
||||
client.connect("resqaws.jankstudio.com", 8883, 60)
|
||||
t = Thread(target=getRedis, args=(1, client))
|
||||
|
||||
logger.info("Starting redis thread")
|
||||
t = Thread(target=getRedis, args=(1, client, logger))
|
||||
|
||||
t.start()
|
||||
|
||||
|
|
@ -63,9 +79,10 @@ def main():
|
|||
client.loop_forever()
|
||||
|
||||
|
||||
logging.basicConfig(filename='/tmp/mqtt_sender.log', level=logging.INFO)
|
||||
logging.info('Started mqtt_sender')
|
||||
pid = "/tmp/mqtt_sender.pid"
|
||||
daemon = Daemonize(app="mqtt_sender", pid=pid, action=main)
|
||||
daemon.start()
|
||||
#main()
|
||||
#logging.basicConfig(filename='/tmp/mqtt_sender.log', level=logging.INFO)
|
||||
#logging.info('Started mqtt_sender')
|
||||
|
||||
#pid = "/tmp/mqtt_sender.pid"
|
||||
#daemon = Daemonize(app="mqtt_sender", pid=pid, action=main)
|
||||
#daemon.start()
|
||||
main()
|
||||
|
|
|
|||
Loading…
Reference in a new issue