diff options
author | 2018-02-18 11:24:18 +0000 | |
---|---|---|
committer | 2018-02-18 11:24:18 +0000 | |
commit | ed0603857c2c839c19b7784cd00e0731e57364be (patch) | |
tree | ce12a9d5073a364a027665166626344987690aaa /pysite | |
parent | Rename "Websocket" to "WS" to avoid confusion with the gevents-websocket WebS... (diff) | |
parent | attempt to fix stacktrace when initialising logger (#17) (diff) |
Merge remote-tracking branch 'origin/master'
# Conflicts:
# pysite/views/ws/echo.py
Diffstat (limited to 'pysite')
-rw-r--r-- | pysite/__init__.py | 16 | ||||
-rw-r--r-- | pysite/database.py | 12 | ||||
-rw-r--r-- | pysite/route_manager.py | 14 | ||||
-rw-r--r-- | pysite/views/ws/echo.py | 14 |
4 files changed, 41 insertions, 15 deletions
diff --git a/pysite/__init__.py b/pysite/__init__.py index 9bad5790..a87e79d4 100644 --- a/pysite/__init__.py +++ b/pysite/__init__.py @@ -1 +1,17 @@ # coding=utf-8 +import logging +import os + + +# region Logging +# Get the log level from environment +log_level = os.environ.get("LOG_LEVEL", "info").upper() + +if hasattr(logging, log_level): + log_level = getattr(logging, log_level) +else: + raise RuntimeError("LOG_LEVEL environment variable has an invalid value.") + +# This handler will ensure we log to stdout and stderr +logging.basicConfig(format='[%(asctime)s] [%(levelname)s] %(message)s', level=log_level) +# endregion diff --git a/pysite/database.py b/pysite/database.py index d3506062..589b77d1 100644 --- a/pysite/database.py +++ b/pysite/database.py @@ -1,6 +1,7 @@ # coding=utf-8 - +import logging import os + from typing import Any, Callable, Dict, Iterator, List, Optional, Union from flask import abort @@ -16,6 +17,7 @@ class RethinkDB: self.host = os.environ.get("RETHINKDB_HOST", "127.0.0.1") self.port = os.environ.get("RETHINKDB_PORT", "28016") self.database = os.environ.get("RETHINKDB_DATABASE", "pythondiscord") + self.log = logging.getLogger() self.conn = None rethinkdb.set_loop_type(loop_type) @@ -23,9 +25,9 @@ class RethinkDB: with self.get_connection(connect_database=False) as conn: try: rethinkdb.db_create(self.database).run(conn) - print(f"Database created: '{self.database}'") + self.log.debug(f"Database created: '{self.database}'") except rethinkdb.RqlRuntimeError: - print(f"Database found: '{self.database}'") + self.log.debug(f"Database found: '{self.database}'") def get_connection(self, connect_database: bool=True) -> DefaultConnection: """ @@ -82,7 +84,7 @@ class RethinkDB: all_tables = rethinkdb.db(self.database).table_list().run(conn) if table_name in all_tables: - print(f"Table found: '{table_name}' ({len(all_tables)} tables in total)") + self.log.debug(f"Table found: '{table_name}' ({len(all_tables)} tables in total)") return False # Use a kwargs dict because the driver doesn't check the value @@ -99,7 +101,7 @@ class RethinkDB: rethinkdb.db(self.database).table_create(table_name, **kwargs).run(conn) - print(f"Table created: '{table_name}'") + self.log.debug(f"Table created: '{table_name}'") return True def drop_table(self, table_name: str): diff --git a/pysite/route_manager.py b/pysite/route_manager.py index 739af726..2b6669e9 100644 --- a/pysite/route_manager.py +++ b/pysite/route_manager.py @@ -1,6 +1,7 @@ # coding=utf-8 import importlib import inspect +import logging import os from flask import Blueprint, Flask @@ -25,6 +26,7 @@ class RouteManager: self.sockets = Sockets(self.app) self.db = RethinkDB() + self.log = logging.getLogger() self.app.secret_key = os.environ.get("WEBPAGE_SECRET_KEY", "super_secret") self.app.config["SERVER_NAME"] = os.environ.get("SERVER_NAME", "pythondiscord.com:8080") self.app.before_request(self.db.before_request) @@ -32,26 +34,24 @@ class RouteManager: # Load the main blueprint self.main_blueprint = Blueprint("main", __name__) - print(f"Loading Blueprint: {self.main_blueprint.name}") + self.log.debug(f"Loading Blueprint: {self.main_blueprint.name}") self.load_views(self.main_blueprint, "pysite/views/main") self.app.register_blueprint(self.main_blueprint) - print("") + self.log.debug("") # Load the subdomains self.subdomains = ['api', 'staff'] for sub in self.subdomains: sub_blueprint = Blueprint(sub, __name__, subdomain=sub) - - print(f"Loading Blueprint: {sub_blueprint.name}") + self.log.debug(f"Loading Blueprint: {sub_blueprint.name}") self.load_views(sub_blueprint, f"pysite/views/{sub}") self.app.register_blueprint(sub_blueprint) - print("") # Load the websockets self.ws_blueprint = Blueprint("ws", __name__) - print("Loading websocket routes...") + self.log.debug("Loading websocket routes...") self.load_views(self.ws_blueprint, "pysite/views/ws") self.sockets.register_blueprint(self.ws_blueprint, url_prefix="/ws") @@ -89,4 +89,4 @@ class RouteManager: ) ): cls.setup(self, blueprint) - print(f">> View loaded: {cls.name: <15} ({module.__name__}.{cls_name})") + self.log.debug(f">> View loaded: {cls.name: <15} ({module.__name__}.{cls_name})") diff --git a/pysite/views/ws/echo.py b/pysite/views/ws/echo.py index 06dffdc4..a10f9b57 100644 --- a/pysite/views/ws/echo.py +++ b/pysite/views/ws/echo.py @@ -1,4 +1,8 @@ # coding=utf-8 +import logging + +from geventwebsocket.websocket import WebSocket + from pysite.websockets import WS @@ -6,13 +10,17 @@ class EchoWebsocket(WS): path = "/echo" name = "ws_echo" + def __init__(self, socket: WebSocket): + super().__init__(socket) + self.log = logging.getLogger() + def on_open(self): - print("Echo | WS opened.") + self.log.debug("Echo | WS opened.") self.send("Hey, welcome!") def on_message(self, message): - print(f"Echo | Message: {message}") + self.log.debug(f"Echo | Message: {message}") self.send(message) def on_close(self): - print("Echo | WS closed.") + self.log.debug("Echo | WS closed.") |