This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved logging mechanism (and a few minor goodies) (#54)
* Improved logging mechanism * Allow for suppression of any errors regarding failed device updates
- Loading branch information
Showing
14 changed files
with
199 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,59 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import logging | ||
import sys | ||
|
||
if sys.version_info < (3, 5): | ||
print("To use this script you need python 3.5 or newer! got %s" % sys.version_info) | ||
sys.exit(1) | ||
|
||
from logger import _LOGGER | ||
import logger | ||
logger.setup() | ||
|
||
import logging | ||
import argparse | ||
import queue | ||
|
||
from workers_queue import _WORKERS_QUEUE | ||
from config import settings | ||
from mqtt import MqttClient | ||
from workers_manager import WorkersManager | ||
|
||
|
||
parser = argparse.ArgumentParser() | ||
group = parser.add_mutually_exclusive_group() | ||
group.add_argument('-d', '--debug', action='store_true', default=False) | ||
group.add_argument('-q', '--quiet', action='store_true', default=False) | ||
group.add_argument('-d', '--debug', action='store_true', default=False, help='Set logging to output debug information') | ||
group.add_argument('-q', '--quiet', action='store_true', default=False, help='Set logging to just output warnings and errors') | ||
parser.add_argument('-s', '--suppress-update-failures', dest='suppress', action='store_true', default=False, help='Suppress any errors regarding failed device updates') | ||
parsed = parser.parse_args() | ||
|
||
if parsed.debug: | ||
_LOGGER = logger.get() | ||
if parsed.quiet: | ||
_LOGGER.setLevel(logging.WARNING) | ||
elif parsed.debug: | ||
_LOGGER.setLevel(logging.DEBUG) | ||
logger.enable_debug_formatter() | ||
else: | ||
_LOGGER.setLevel(logging.INFO) | ||
logger.suppress_update_failures(parsed.suppress) | ||
|
||
_LOGGER.debug('Starting') | ||
_LOGGER.info('Starting') | ||
|
||
mqtt = MqttClient(settings['mqtt']) | ||
manager = WorkersManager() | ||
manager.register_workers(settings['manager']).start(mqtt) | ||
|
||
running = True | ||
|
||
try: | ||
while running: | ||
try: | ||
mqtt.publish(_WORKERS_QUEUE.get(block=True).execute()) | ||
except (KeyboardInterrupt, SystemExit): | ||
raise | ||
except Exception as e: | ||
if not parsed.quiet: | ||
_LOGGER.exception(e) | ||
except KeyboardInterrupt: | ||
running = False | ||
_LOGGER.info('Exiting allowing jobs to finish. If you need force exit use kill') | ||
|
||
while running: | ||
try: | ||
mqtt.publish(_WORKERS_QUEUE.get(timeout=10).execute()) | ||
except queue.Empty: # Allow for SIGINT processing | ||
pass | ||
except TimeoutError: | ||
logger.log_exception(_LOGGER, "Timeout while executing worker command", suppress=True) | ||
except (KeyboardInterrupt, SystemExit): | ||
running = False | ||
_LOGGER.info('Finish current jobs and shut down. If you need force exit use kill') | ||
except Exception as e: | ||
logger.log_exception(_LOGGER, "Fatal error while executing worker command: %s", type(e).__name__) | ||
raise e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,50 @@ | ||
import logging | ||
import logging.config | ||
import yaml | ||
|
||
import sys | ||
APP_ROOT = 'bt-mqtt-gw' | ||
SUPPRESSION_ENABLED = False | ||
|
||
_LOGGER = logging.getLogger('bt-mqtt-gw') | ||
_LOGGER.addHandler(logging.StreamHandler(sys.stdout)) | ||
|
||
def setup(): | ||
with open('logger.yaml', 'rt') as f: | ||
config = yaml.safe_load(f.read()) | ||
logging.config.dictConfig(config) | ||
|
||
|
||
def get(name=None): | ||
if name: | ||
logger_name = '{}.{}'.format(APP_ROOT, name) | ||
else: | ||
logger_name = APP_ROOT | ||
return logging.getLogger(logger_name) | ||
|
||
|
||
def enable_debug_formatter(): | ||
logging.getLogger().handlers[0].setFormatter(logging.getLogger('dummy_debug').handlers[0].formatter) | ||
|
||
|
||
def reset(): | ||
app_level = get().getEffectiveLevel() | ||
|
||
root = logging.getLogger() | ||
map(root.removeHandler, root.handlers[:]) | ||
map(root.removeFilter, root.filters[:]) | ||
|
||
setup() | ||
get().setLevel(app_level) | ||
if app_level <= logging.DEBUG: | ||
enable_debug_formatter() | ||
|
||
|
||
def suppress_update_failures(suppress): | ||
global SUPPRESSION_ENABLED | ||
SUPPRESSION_ENABLED = suppress | ||
|
||
|
||
def log_exception(logger, message, *args, **kwargs): | ||
if not ('suppress' in kwargs and kwargs.pop('suppress') and SUPPRESSION_ENABLED): | ||
if logger.isEnabledFor(logging.DEBUG): | ||
logger.exception(message, *args, **kwargs) | ||
elif logger.isEnabledFor(logging.WARNING): | ||
logger.warning(message, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
version: 1 | ||
disable_existing_loggers: True | ||
|
||
formatters: | ||
default: | ||
format: '%(asctime)s %(message)s' | ||
datefmt: '%X' | ||
minimal: | ||
format: '%(message)s' | ||
debug: | ||
format: '%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)d:%(funcName)s - %(message)s' | ||
|
||
handlers: | ||
console: | ||
class: logging.StreamHandler | ||
formatter: default | ||
stream: ext://sys.stdout | ||
dummy_debug: | ||
class: logging.NullHandler | ||
formatter: debug | ||
|
||
loggers: | ||
bt-mqtt-gw: | ||
level: INFO | ||
dummy_debug: | ||
handlers: [dummy_debug] | ||
|
||
root: | ||
handlers: [console] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.