Skip to content

Commit

Permalink
Replace print by the Python logging framework
Browse files Browse the repository at this point in the history
We use the logger "fishtest" as configured in production.ini.

For messages to the event log we use the child logger
"fishtest.event", also configured in "production.ini".

Log messages are formatted as follows:

INFO [fishtest] [waitress-0:rundb.py:failed_task:1446] <...message...>

Also: move scheduler to a separate file scheduler.py.
  • Loading branch information
vdbergh committed Jun 25, 2024
1 parent f5fa284 commit a601fb7
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 295 deletions.
15 changes: 12 additions & 3 deletions server/development.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ port = 6542
###

[loggers]
keys = root, fishtest
keys = root, fishtest, fishtest.event

[handlers]
keys = console
keys = console, events

[formatters]
keys = generic
Expand All @@ -52,11 +52,20 @@ level = DEBUG
handlers =
qualname = fishtest

[logger_fishtest.event]
level = DEBUG
handlers = events
qualname = fishtest.event

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[handler_events]
class = fishtest.utils.EventHandler
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
format = %(levelname)s [%(name)s] [%(threadName)s:%(filename)s:%(funcName)s:%(lineno)s] %(message)s
12 changes: 3 additions & 9 deletions server/fishtest/actiondb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime, timezone

from fishtest.schemas import action_schema
from fishtest.util import hex_print, worker_name
from fishtest.util import event_log, hex_print, logger, worker_name
from pymongo import DESCENDING
from vtjson import ValidationError, validate

Expand Down Expand Up @@ -219,13 +219,7 @@ def insert_action(self, **action):
try:
validate(action_schema, action, "action")
except ValidationError as e:
message = (
f"Internal Error. Request {str(action)} does not validate: {str(e)}"
)
print(message, flush=True)
self.log_message(
username="fishtest.system",
message=message,
)
message = f"Request {str(action)} does not validate: {str(e)}"
event_log.error(message)
return
self.actions.insert_one(action)
4 changes: 2 additions & 2 deletions server/fishtest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from fishtest.schemas import api_access_schema, api_schema, gzip_data
from fishtest.stats.stat_util import SPRT_elo, get_elo
from fishtest.util import strip_run, worker_name
from fishtest.util import event_log, logger, strip_run, worker_name
from pyramid.httpexceptions import (
HTTPBadRequest,
HTTPException,
Expand Down Expand Up @@ -69,7 +69,7 @@ def handle_error(self, error, exception=HTTPBadRequest):
)
api = urlparse(full_url).path
error = f"{api}: {error}"
print(error, flush=True)
logger.info(error)
raise exception(self.add_time({"error": error}))


Expand Down
Loading

0 comments on commit a601fb7

Please sign in to comment.