Skip to content

Commit

Permalink
refactor(robot-server): use the server.log file to store logs coming …
Browse files Browse the repository at this point in the history
…in from the robot server (#12894)
  • Loading branch information
Laura-Danielle authored Jun 14, 2023
1 parent c48c327 commit 2c583ca
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
5 changes: 3 additions & 2 deletions api/src/opentrons/system/log_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ async def get_records_dumb(selector: str, records: int, mode: str) -> bytes:
:param selector: The syslog selector to limit responses to
:param records: The maximum number of records to print
:param mode: A journalctl dump mode. Should be either "short" or "json".
:param mode: A journalctl dump mode. Should be either "short-precise" or "json".
"""
selector_flag = "-u" if selector == "opentrons-robot-server" else "-t"
proc = await asyncio.create_subprocess_exec(
"journalctl",
"--no-pager",
"-t",
selector_flag,
selector,
"-n",
str(records),
Expand Down
1 change: 1 addition & 0 deletions robot-server/robot_server/service/legacy/models/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class LogIdentifier(str, Enum):
api = "api.log"
serial = "serial.log"
server = "server.log"
api_server = "combined_api_server.log"


class LogFormat(str, Enum):
Expand Down
3 changes: 2 additions & 1 deletion robot-server/robot_server/service/legacy/routers/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
LogIdentifier.api: "opentrons-api",
LogIdentifier.serial: "opentrons-api-serial",
LogIdentifier.server: "uvicorn",
LogIdentifier.api_server: "opentrons-robot-server",
}


Expand All @@ -28,7 +29,7 @@ async def get_logs(
syslog_id = IDENTIFIER_TO_SYSLOG_ID[log_identifier]
modes = {
LogFormat.json: ("json", "application/json"),
LogFormat.text: ("short", "text/plain"),
LogFormat.text: ("short-precise", "text/plain"),
}
format_type, media_type = modes[format]
output = await log_control.get_records_dumb(syslog_id, records, format_type)
Expand Down
14 changes: 10 additions & 4 deletions robot-server/robot_server/service/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,20 @@ def _robot_log_config(log_level: int) -> Dict[str, Any]:
"class": "systemd.journal.JournalHandler",
"level": logging.DEBUG,
"formatter": "message_only",
"SYSLOG_IDENTIFIER": "opentrons-api",
"SYSLOG_IDENTIFIER": "uvicorn",
},
"syslog_plus_unit_above_warn": {
"class": "systemd.journal.JournalHandler",
"level": logging.WARN,
"formatter": "message_only",
"SYSLOG_IDENTIFER": "opentrons-api",
"SYSLOG_IDENTIFIER": "uvicorn",
},
"unit_only_below_warn": {
"class": "systemd.journal.JournalHandler",
"level": logging.DEBUG,
"formatter": "message_only",
"filters": ["records_below_warning"],
"SYSLOG_IDENTIFIER": "uvicorn",
},
},
"loggers": {
Expand All @@ -70,13 +71,18 @@ def _robot_log_config(log_level: int) -> Dict[str, Any]:
"level": log_level,
"propagate": False,
},
"uvicorn": {
"handlers": ["syslog_plus_unit"],
"level": log_level,
"propagate": False,
},
"fastapi": {
"handlers": ["unit_only"],
"handlers": ["syslog_plus_unit"],
"level": log_level,
"propagate": False,
},
"starlette": {
"handlers": ["unit_only"],
"handlers": ["syslog_plus_unit"],
"level": log_level,
"propagate": False,
},
Expand Down
14 changes: 8 additions & 6 deletions robot-server/tests/service/legacy/routers/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ async def mock_get_records_dumb(identifier, records, format_type):
body = response.text
assert response.status_code == 200
assert body == expected
m.assert_called_once_with("opentrons-api-serial", DEFAULT_RECORDS, "short")
m.assert_called_once_with(
"opentrons-api-serial", DEFAULT_RECORDS, "short-precise"
)


@pytest.mark.parametrize(
"format_param, records_param, mode_param",
[
("json", MAX_RECORDS - 1, "json"),
("text", MAX_RECORDS - 1, "short"),
("text", MAX_RECORDS - 1, "short-precise"),
("json", 1, "json"),
("text", 1, "short"),
("text", 1, "short-precise"),
],
)
def test_get_serial_log_with_params(
Expand Down Expand Up @@ -94,16 +96,16 @@ async def mock_get_records_dumb(identifier, records, format_type):
body = response.text
assert response.status_code == 200
assert body == expected
m.assert_called_once_with("opentrons-api", DEFAULT_RECORDS, "short")
m.assert_called_once_with("opentrons-api", DEFAULT_RECORDS, "short-precise")


@pytest.mark.parametrize(
"format_param, records_param, mode_param",
[
("json", MAX_RECORDS - 1, "json"),
("text", MAX_RECORDS - 1, "short"),
("text", MAX_RECORDS - 1, "short-precise"),
("json", 1, "json"),
("text", 1, "short"),
("text", 1, "short-precise"),
],
)
def test_get_api_log_with_params(api_client, format_param, records_param, mode_param):
Expand Down
2 changes: 2 additions & 0 deletions robot-server/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def mock_log_control() -> Iterator[MagicMock]:
argvalues=[
"/logs/serial.log",
"/logs/api.log",
"/logs/server.log",
"/logs/combined_api_server.log",
"/",
],
)
Expand Down

0 comments on commit 2c583ca

Please sign in to comment.