* add systemctl service * add installation procedure Signed-off-by: Thomas Klaehn <tkl@blackfinn.de>
44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
'''
|
|
Created on Feb 15, 2017
|
|
|
|
@author: tkl
|
|
'''
|
|
import logging
|
|
import os
|
|
import re
|
|
import sys
|
|
import time
|
|
import mqtt
|
|
|
|
BASE_LOG_DIR = '/var/log/mqtt/'
|
|
OWN_LOG_FILE = '/var/log/mqtt_logger.log'
|
|
LOG_FORMAT = '%(asctime)s %(levelname)s %(message)s'
|
|
|
|
def main():
|
|
'''
|
|
Entry point for mqtt logger.
|
|
'''
|
|
logging.basicConfig(format=LOG_FORMAT, filename=OWN_LOG_FILE)
|
|
conn = mqtt.Mqtt(hostname='gitlab', subscribe=['#'])
|
|
if conn.connect() is False:
|
|
logging.error('couldn\'t connect...')
|
|
return 1
|
|
else:
|
|
try:
|
|
while True:
|
|
msg = conn.receive()
|
|
logfile = BASE_LOG_DIR + msg.topic + '.log'
|
|
logdir = re.sub('/[^/]*$', '', logfile)
|
|
if not os.path.isdir(logdir):
|
|
os.makedirs(logdir)
|
|
log = open(logfile, 'a')
|
|
log.write(msg.payload + '\n')
|
|
log.close()
|
|
except KeyboardInterrupt:
|
|
logging.exception('Ececution interrupted by Keyboard Interrupt')
|
|
conn.disconnect()
|
|
return 0
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|