Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the Server Loggers #154

Merged
merged 8 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions rest_tools/server/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from ..utils.json_util import json_decode
from ..utils.pkce import PKCEMixin

LOGGER = logging.getLogger('rest')
LOGGER = logging.getLogger(__name__)


def _log_auth_failed(e: Exception):
Expand Down Expand Up @@ -162,13 +162,19 @@ def get_current_user(self):
@wtt.evented()
def prepare(self):
"""Prepare before http-method request handlers."""
LOGGER.debug(f"{self.request.method} [{self.__class__.__name__}]")

# log at the very start of every new request
logging.info(">>> %s", self._request_summary()) # use the root logger
# ^^^ this mimics the log line that tornado provides at the end of a request:
# see rest_tools.server.tornado_logger().
# ">>>" makes logs line-up (end-of-request log line uses http code: 200, 400, etc.)
LOGGER.debug(f"[{self.__class__.__name__}]")

if self.route_stats is not None:
stat = self.route_stats[self.request.path]
if stat.is_overloaded():
backoff = stat.get_backoff_time()
LOGGER.warn('Server is overloaded, backoff %r', backoff)
LOGGER.warning('Server is overloaded, backoff %r', backoff)
self.set_header('Retry-After', backoff)
raise tornado.web.HTTPError(503, reason="server overloaded")
self.start_time = time.time()
Expand Down
11 changes: 6 additions & 5 deletions rest_tools/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

# fmt:off
# pylint: skip-file

import binascii
import logging
Expand All @@ -14,15 +13,17 @@

AsyncIOMainLoop().install()

LOGGER = logging.getLogger() # this stuff always needs to be logged -> use the 'root' logger


def tornado_logger(handler):
"""Log tornado messages to root logger."""
if handler.get_status() < 400:
log_method = logging.debug
log_method = LOGGER.info
elif handler.get_status() < 500:
log_method = logging.warning
log_method = LOGGER.warning
else:
log_method = logging.error
log_method = LOGGER.error
request_time = 1000.0 * handler.request.request_time()
log_method("%d %s %.2fms", handler.get_status(), handler._request_summary(), request_time)

Expand Down Expand Up @@ -58,7 +59,7 @@ def startup(self, address='localhost', port=8080):
address (str): bind address
port (int): bind port
"""
logging.warning('tornado bound to %s:%d', address, port)
LOGGER.warning('tornado bound to %s:%d', address, port)

app = tornado.web.Application(self.routes, **self.app_args)

Expand Down
Loading