kopia lustrzana https://github.com/glidernet/ogn-python
Logging: Enable custom logfile location.
rodzic
76ec41a2ae
commit
851559aced
|
@ -1,4 +1,5 @@
|
||||||
import socket
|
import socket
|
||||||
|
import logging
|
||||||
from time import time
|
from time import time
|
||||||
|
|
||||||
from ogn.gateway import settings
|
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_parser import parse_aprs
|
||||||
from ogn.aprs_utils import create_aprs_login
|
from ogn.aprs_utils import create_aprs_login
|
||||||
from ogn.exceptions import AprsParseError, OgnParseError
|
from ogn.exceptions import AprsParseError, OgnParseError
|
||||||
from ogn.logger import logger
|
|
||||||
|
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
@ -16,9 +16,9 @@ from ogn.model import Base
|
||||||
class ognGateway:
|
class ognGateway:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def connect_db(self):
|
def connect_db(self):
|
||||||
self.session = session
|
self.session = session
|
||||||
|
self.logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def connect(self, aprs_user):
|
def connect(self, aprs_user):
|
||||||
# create socket, connect to server, login and make a file object associated with the socket
|
# 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):
|
def proceed_line(self, line):
|
||||||
try:
|
try:
|
||||||
beacon = parse_aprs(line)
|
beacon = parse_aprs(line)
|
||||||
|
self.logger.debug('Received beacon: {}'.format(beacon))
|
||||||
except AprsParseError:
|
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
|
return
|
||||||
except OgnParseError:
|
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
|
return
|
||||||
|
|
||||||
if beacon is not None:
|
if beacon is not None:
|
||||||
|
|
|
@ -1,35 +1,44 @@
|
||||||
import socket
|
import socket
|
||||||
|
import logging
|
||||||
|
|
||||||
from ogn.gateway.client import ognGateway
|
from ogn.gateway.client import ognGateway
|
||||||
from ogn.logger import logger
|
|
||||||
|
|
||||||
from manager import Manager
|
from manager import Manager
|
||||||
manager = Manager()
|
manager = Manager()
|
||||||
|
|
||||||
DB_URI = 'sqlite:///beacons.db'
|
DB_URI = 'sqlite:///beacons.db'
|
||||||
|
logging_formatstr = '%(asctime)s - %(levelname).4s - %(name)s - %(message)s'
|
||||||
|
log_levels = ['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG']
|
||||||
|
|
||||||
|
|
||||||
@manager.command
|
@manager.command
|
||||||
def run(aprs_user="anon-dev"):
|
def run(aprs_user='anon-dev', logfile='main.log', loglevel='INFO'):
|
||||||
"""Run the aprs client."""
|
"""Run the aprs client."""
|
||||||
|
|
||||||
|
# User input validation
|
||||||
if len(aprs_user) < 3 or len(aprs_user) > 9:
|
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
|
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
|
user_interrupted = False
|
||||||
gateway = ognGateway()
|
gateway = ognGateway()
|
||||||
|
|
||||||
print("Connect to DB")
|
print("Connect to DB")
|
||||||
logger.info('Connect to DB')
|
|
||||||
gateway.connect_db()
|
gateway.connect_db()
|
||||||
|
|
||||||
while user_interrupted is False:
|
while user_interrupted is False:
|
||||||
logger.info("Connect OGN gateway as {}".format(aprs_user))
|
|
||||||
gateway.connect(aprs_user)
|
gateway.connect(aprs_user)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
logger.info('Run gateway')
|
|
||||||
gateway.run()
|
gateway.run()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logger.error('User interrupted', exc_info=True)
|
logger.error('User interrupted', exc_info=True)
|
||||||
|
|
|
@ -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)
|
|
Ładowanie…
Reference in New Issue