From 851559acedf1d61ec7f5508aebbd98587ebaed5d Mon Sep 17 00:00:00 2001 From: "Fabian P. Schmidt" Date: Mon, 30 Nov 2015 15:03:38 +0100 Subject: [PATCH] Logging: Enable custom logfile location. --- ogn/gateway/client.py | 9 +++++---- ogn/gateway/manage.py | 23 ++++++++++++++++------- ogn/logger.py | 17 ----------------- 3 files changed, 21 insertions(+), 28 deletions(-) delete mode 100644 ogn/logger.py diff --git a/ogn/gateway/client.py b/ogn/gateway/client.py index 5a6e4ec..d73e300 100644 --- a/ogn/gateway/client.py +++ b/ogn/gateway/client.py @@ -1,4 +1,5 @@ import socket +import logging from time import time from ogn.gateway import settings @@ -6,7 +7,6 @@ from ogn.commands.dbutils import session from ogn.aprs_parser import parse_aprs from ogn.aprs_utils import create_aprs_login from ogn.exceptions import AprsParseError, OgnParseError -from ogn.logger import logger from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker @@ -16,9 +16,9 @@ from ogn.model import Base class ognGateway: def __init__(self): pass - def connect_db(self): self.session = session + self.logger = logging.getLogger(__name__) def connect(self, aprs_user): # create socket, connect to server, login and make a file object associated with the socket @@ -57,11 +57,12 @@ class ognGateway: def proceed_line(self, line): try: beacon = parse_aprs(line) + self.logger.debug('Received beacon: {}'.format(beacon)) except AprsParseError: - logger.error('AprsParseError while parsing line: %s' % line, exc_info=True) + self.logger.error('AprsParseError while parsing line: {}'.format(line), exc_info=True) return except OgnParseError: - logger.error('OgnParseError while parsing line: ' % line, exc_info=True) + self.logger.error('OgnParseError while parsing line: {}'.format(line), exc_info=True) return if beacon is not None: diff --git a/ogn/gateway/manage.py b/ogn/gateway/manage.py index e243811..75a15ad 100644 --- a/ogn/gateway/manage.py +++ b/ogn/gateway/manage.py @@ -1,35 +1,44 @@ import socket +import logging from ogn.gateway.client import ognGateway -from ogn.logger import logger from manager import Manager manager = Manager() DB_URI = 'sqlite:///beacons.db' +logging_formatstr = '%(asctime)s - %(levelname).4s - %(name)s - %(message)s' +log_levels = ['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'] @manager.command -def run(aprs_user="anon-dev"): +def run(aprs_user='anon-dev', logfile='main.log', loglevel='INFO'): """Run the aprs client.""" + # User input validation if len(aprs_user) < 3 or len(aprs_user) > 9: - print("aprs_user must be a string of 3-9 characters") + print('aprs_user must be a string of 3-9 characters.') return + if loglevel not in log_levels: + print('loglevel must be an element of {}.'.format(log_levels)) + return + + # Enable logging + log_handlers = [logging.StreamHandler()] + if logfile: + log_handlers.append(logging.FileHandler(logfile)) + logging.basicConfig(format=logging_formatstr, level=loglevel, handlers=log_handlers) + user_interrupted = False gateway = ognGateway() print("Connect to DB") - logger.info('Connect to DB') gateway.connect_db() while user_interrupted is False: - logger.info("Connect OGN gateway as {}".format(aprs_user)) gateway.connect(aprs_user) - try: - logger.info('Run gateway') gateway.run() except KeyboardInterrupt: logger.error('User interrupted', exc_info=True) diff --git a/ogn/logger.py b/ogn/logger.py deleted file mode 100644 index 3809228..0000000 --- a/ogn/logger.py +++ /dev/null @@ -1,17 +0,0 @@ -import logging - -logger = logging.getLogger('main') -logger.setLevel(logging.DEBUG) - -filehandler = logging.FileHandler('main.log') -filehandler.setLevel(logging.DEBUG) - -consolehandler = logging.StreamHandler() -consolehandler.setLevel(logging.INFO) - -formatter = logging.Formatter("%(asctime)s - %(levelname).4s - %(name)s - %(message)s") -filehandler.setFormatter(formatter) -consolehandler.setFormatter(formatter) - -logger.addHandler(filehandler) -logger.addHandler(consolehandler)