From 35d41582ee0cfc9dce505526e8a5648d93a1be12 Mon Sep 17 00:00:00 2001 From: Hemna Date: Sun, 16 Jul 2023 16:28:15 -0400 Subject: [PATCH] Moved logging to log for wsgi.py Added wsgi.py to be used with gunicorn to start aprsd's web admin interface. gunicorn -b :8080 "aprsd.wsgi:app" --- README.rst | 14 ++++++++++++++ aprsd/admin_web.py | 13 +++++-------- aprsd/cli_helper.py | 2 +- aprsd/client.py | 2 +- aprsd/cmds/fetch_stats.py | 2 +- aprsd/cmds/healthcheck.py | 2 +- aprsd/cmds/listen.py | 2 +- aprsd/cmds/webchat.py | 2 +- aprsd/conf/__init__.py | 2 +- aprsd/conf/client.py | 2 +- aprsd/conf/log.py | 10 ++++++++-- aprsd/{logging => log}/__init__.py | 0 aprsd/{logging => log}/log.py | 6 +++--- aprsd/{logging => log}/rich.py | 2 +- aprsd/main.py | 2 +- aprsd/plugins/email.py | 2 +- aprsd/threads/log_monitor.py | 2 +- aprsd/wsgi.py | 12 ++++++++++++ tests/cmds/test_send_message.py | 4 ++-- tests/cmds/test_webchat.py | 2 +- 20 files changed, 57 insertions(+), 28 deletions(-) rename aprsd/{logging => log}/__init__.py (100%) rename aprsd/{logging => log}/log.py (95%) rename aprsd/{logging => log}/rich.py (99%) create mode 100644 aprsd/wsgi.py diff --git a/README.rst b/README.rst index caaf8b1..088404b 100644 --- a/README.rst +++ b/README.rst @@ -289,6 +289,20 @@ LOCATION AND... ping, fortune, time..... +Web Admin Interface +=================== +To start the web admin interface, You have to install gunicorn in your virtualenv that already has aprsd installed. + +:: + + source /bin/activate + pip install gunicorn + gunicorn --bind 0.0.0.0:8080 "aprsd.wsgi:app" + +The web admin interface will be running on port 8080 on the local machine. http://localhost:8080 + + + Development =========== diff --git a/aprsd/admin_web.py b/aprsd/admin_web.py index 6cc0e76..0bac8eb 100644 --- a/aprsd/admin_web.py +++ b/aprsd/admin_web.py @@ -14,7 +14,7 @@ from werkzeug.security import check_password_hash, generate_password_hash import aprsd from aprsd import cli_helper, client, conf, packets, plugin, threads -from aprsd.logging import rich as aprsd_logging +from aprsd.log import rich as aprsd_logging from aprsd.rpc import client as aprsd_rpc_client @@ -335,7 +335,7 @@ def init_flask(loglevel, quiet): return socketio, flask_app -def create_app(config_file=None, log_level=None, gunicorn=False): +def create_app(config_file=None, log_level=None): global socketio global app @@ -351,12 +351,9 @@ def create_app(config_file=None, log_level=None, gunicorn=False): if not log_level: log_level = CONF.logging.log_level - if gunicorn: - socketio, app = init_flask(log_level, False) - setup_logging(app, log_level, False) - return app - else: - return socketio + socketio, app = init_flask(log_level, False) + setup_logging(app, log_level, False) + return app if __name__ == "aprsd.flask": diff --git a/aprsd/cli_helper.py b/aprsd/cli_helper.py index 4dbf727..42c5891 100644 --- a/aprsd/cli_helper.py +++ b/aprsd/cli_helper.py @@ -8,7 +8,7 @@ from oslo_config import cfg import aprsd from aprsd import conf # noqa: F401 -from aprsd.logging import log +from aprsd.log import log from aprsd.utils import trace diff --git a/aprsd/client.py b/aprsd/client.py index 93c192a..9c9522d 100644 --- a/aprsd/client.py +++ b/aprsd/client.py @@ -144,7 +144,7 @@ class APRSISClient(Client): try: LOG.info("Creating aprslib client") aprs_client = aprsis.Aprsdis(user, passwd=password, host=host, port=port) - # Force the logging to be the same + # Force the log to be the same aprs_client.logger = LOG aprs_client.connect() connected = True diff --git a/aprsd/cmds/fetch_stats.py b/aprsd/cmds/fetch_stats.py index 1993213..6a57d68 100644 --- a/aprsd/cmds/fetch_stats.py +++ b/aprsd/cmds/fetch_stats.py @@ -17,7 +17,7 @@ from aprsd.rpc import client as rpc_client # setup the global logger -# logging.basicConfig(level=logging.DEBUG) # level=10 +# log.basicConfig(level=log.DEBUG) # level=10 LOG = logging.getLogger("APRSD") CONF = cfg.CONF diff --git a/aprsd/cmds/healthcheck.py b/aprsd/cmds/healthcheck.py index b63a6d0..c8c9a38 100644 --- a/aprsd/cmds/healthcheck.py +++ b/aprsd/cmds/healthcheck.py @@ -21,7 +21,7 @@ from aprsd.rpc import client as aprsd_rpc_client # setup the global logger -# logging.basicConfig(level=logging.DEBUG) # level=10 +# log.basicConfig(level=log.DEBUG) # level=10 CONF = cfg.CONF LOG = logging.getLogger("APRSD") console = Console() diff --git a/aprsd/cmds/listen.py b/aprsd/cmds/listen.py index e496013..36622d1 100644 --- a/aprsd/cmds/listen.py +++ b/aprsd/cmds/listen.py @@ -22,7 +22,7 @@ from aprsd.threads import rx # setup the global logger -# logging.basicConfig(level=logging.DEBUG) # level=10 +# log.basicConfig(level=log.DEBUG) # level=10 LOG = logging.getLogger("APRSD") CONF = cfg.CONF console = Console() diff --git a/aprsd/cmds/webchat.py b/aprsd/cmds/webchat.py index 8467377..c0e719d 100644 --- a/aprsd/cmds/webchat.py +++ b/aprsd/cmds/webchat.py @@ -22,7 +22,7 @@ import wrapt import aprsd from aprsd import cli_helper, client, conf, packets, stats, threads, utils -from aprsd.logging import rich as aprsd_logging +from aprsd.log import rich as aprsd_logging from aprsd.main import cli from aprsd.threads import rx, tx from aprsd.utils import objectstore, trace diff --git a/aprsd/conf/__init__.py b/aprsd/conf/__init__.py index db28c55..be2e46c 100644 --- a/aprsd/conf/__init__.py +++ b/aprsd/conf/__init__.py @@ -28,7 +28,7 @@ def set_lib_defaults(): def set_log_defaults(): - # logging.set_defaults(default_log_levels=logging.get_default_log_levels()) + # log.set_defaults(default_log_levels=log.get_default_log_levels()) pass diff --git a/aprsd/conf/client.py b/aprsd/conf/client.py index c0754df..c752f16 100644 --- a/aprsd/conf/client.py +++ b/aprsd/conf/client.py @@ -1,5 +1,5 @@ """ -The options for logging setup +The options for log setup """ from oslo_config import cfg diff --git a/aprsd/conf/log.py b/aprsd/conf/log.py index d48ae30..5a4af95 100644 --- a/aprsd/conf/log.py +++ b/aprsd/conf/log.py @@ -1,5 +1,5 @@ """ -The options for logging setup +The options for log setup """ import logging @@ -33,7 +33,7 @@ logging_opts = [ cfg.BoolOpt( "rich_logging", default=True, - help="Enable Rich logging", + help="Enable Rich log", ), cfg.StrOpt( "logfile", @@ -45,6 +45,12 @@ logging_opts = [ default=DEFAULT_LOG_FORMAT, help="Log file format, unless rich_logging enabled.", ), + cfg.StrOpt( + "log_level", + default="INFO", + choices=LOG_LEVELS.keys(), + help="Log level for logging of events.", + ), ] diff --git a/aprsd/logging/__init__.py b/aprsd/log/__init__.py similarity index 100% rename from aprsd/logging/__init__.py rename to aprsd/log/__init__.py diff --git a/aprsd/logging/log.py b/aprsd/log/log.py similarity index 95% rename from aprsd/logging/log.py rename to aprsd/log/log.py index cb7066d..2a53071 100644 --- a/aprsd/logging/log.py +++ b/aprsd/log/log.py @@ -7,7 +7,7 @@ import sys from oslo_config import cfg from aprsd import conf -from aprsd.logging import rich as aprsd_logging +from aprsd.log import rich as aprsd_logging CONF = cfg.CONF @@ -15,8 +15,8 @@ LOG = logging.getLogger("APRSD") logging_queue = queue.Queue() -# Setup the logging faciility -# to disable logging to stdout, but still log to file +# Setup the log faciility +# to disable log to stdout, but still log to file # use the --quiet option on the cmdln def setup_logging(loglevel, quiet): log_level = conf.log.LOG_LEVELS[loglevel] diff --git a/aprsd/logging/rich.py b/aprsd/log/rich.py similarity index 99% rename from aprsd/logging/rich.py rename to aprsd/log/rich.py index 66137fe..1d70160 100644 --- a/aprsd/logging/rich.py +++ b/aprsd/log/rich.py @@ -130,7 +130,7 @@ class APRSDRichHandler(RichHandler): """Render log for display. Args: - record (LogRecord): logging Record. + record (LogRecord): log Record. traceback (Optional[Traceback]): Traceback instance or None for no Traceback. message_renderable (ConsoleRenderable): Renderable (typically Text) containing log message contents. diff --git a/aprsd/main.py b/aprsd/main.py index 34bc5e8..1f5a4eb 100644 --- a/aprsd/main.py +++ b/aprsd/main.py @@ -39,7 +39,7 @@ from aprsd import cli_helper, packets, stats, threads, utils # setup the global logger -# logging.basicConfig(level=logging.DEBUG) # level=10 +# log.basicConfig(level=log.DEBUG) # level=10 CONF = cfg.CONF LOG = logging.getLogger("APRSD") CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) diff --git a/aprsd/plugins/email.py b/aprsd/plugins/email.py index 740ede9..f6f273a 100644 --- a/aprsd/plugins/email.py +++ b/aprsd/plugins/email.py @@ -666,7 +666,7 @@ class APRSDEmailThread(threads.APRSDThread): EmailInfo().delay = 60 # reset clock - LOG.debug("Done looping over Server.fetch, logging out.") + LOG.debug("Done looping over Server.fetch, log out.") self.past = datetime.datetime.now() try: server.logout() diff --git a/aprsd/threads/log_monitor.py b/aprsd/threads/log_monitor.py index 90bda55..8b93ab5 100644 --- a/aprsd/threads/log_monitor.py +++ b/aprsd/threads/log_monitor.py @@ -4,7 +4,7 @@ import threading import wrapt from aprsd import threads -from aprsd.logging import log +from aprsd.log import log LOG = logging.getLogger("APRSD") diff --git a/aprsd/wsgi.py b/aprsd/wsgi.py new file mode 100644 index 0000000..c5278f5 --- /dev/null +++ b/aprsd/wsgi.py @@ -0,0 +1,12 @@ +import logging + +from oslo_config import cfg + +from aprsd import admin_web +from aprsd import conf # noqa + + +CONF = cfg.CONF +LOG = logging.getLogger("APRSD") +app = None +app = admin_web.create_app() diff --git a/tests/cmds/test_send_message.py b/tests/cmds/test_send_message.py index d862282..701a83d 100644 --- a/tests/cmds/test_send_message.py +++ b/tests/cmds/test_send_message.py @@ -30,7 +30,7 @@ class TestSendMessageCommand(unittest.TestCase): CONF.admin.user = "admin" CONF.admin.password = "password" - @mock.patch("aprsd.logging.log.setup_logging") + @mock.patch("aprsd.log.log.setup_logging") def test_no_tocallsign(self, mock_logging): """Make sure we get an error if there is no tocallsign.""" @@ -47,7 +47,7 @@ class TestSendMessageCommand(unittest.TestCase): assert result.exit_code == 2 assert "Error: Missing argument 'TOCALLSIGN'" in result.output - @mock.patch("aprsd.logging.log.setup_logging") + @mock.patch("aprsd.log.log.setup_logging") def test_no_command(self, mock_logging): """Make sure we get an error if there is no command.""" diff --git a/tests/cmds/test_webchat.py b/tests/cmds/test_webchat.py index ed77fff..3a0103d 100644 --- a/tests/cmds/test_webchat.py +++ b/tests/cmds/test_webchat.py @@ -32,7 +32,7 @@ class TestSendMessageCommand(unittest.TestCase): CONF.admin.user = "admin" CONF.admin.password = "password" - @mock.patch("aprsd.logging.log.setup_logging") + @mock.patch("aprsd.log.log.setup_logging") def test_init_flask(self, mock_logging): """Make sure we get an error if there is no login and config."""