#!/usr/bin/env python # -*- coding:utf-8 -*- import json import logging import logging.handlers import timing import threading import upload import config def format_logger(log_path): logger = logging.getLogger() rf = logging.handlers.TimedRotatingFileHandler(filename=log_path, when='S', interval=1, backupCount=3) rf.setFormatter(logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')) handler = logging.StreamHandler() handler.setLevel(logging.DEBUG) handler.setFormatter(logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')) logger.addHandler(rf) logger.addHandler(handler) logging.root = logger if __name__ == '__main__': # read local config config_path = 'config.json' conf = config.Config(config_path) local_config = conf.read_local_config() remote_config = conf.read_redis_config() # init logger log_path = local_config.get('log_path', 'elec_monitor.log') format_logger(log_path) logging.info('[MAIN] local config: %s', local_config) logging.info('[MAIN] elec monitor init logger successful') # timing server start timing_tread = threading.Thread(target=timing.run, name='elec_timing_server') timing_tread.setDaemon(True) timing_tread.start() logging.info('[MAIN] elec monitor init timing server successful') # load redis config to init upload server remote_host_config = conf.load_remote_host_config() if remote_host_config is None: logging.info('[MAIN] elec monitor load remote host successful but None') exit(0) logging.info('[MAIN] elec monitor load remote host successful: %s', remote_host_config) # upload server start logging.info('[MAIN] elec monitor will listen to upload server') upload.defaultHandler = upload.Handle( local_config['upload_max_length'], local_config['upload_temp_file'], local_config['upload_official_dir'], local_config['upload_rubbish'], remote_host_config['host'], remote_host_config['port'], remote_host_config['username'], remote_host_config['password'], remote_host_config['file_path'], local_config['upload_interval'], ) upload.listen()