X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/dcfb228c9a0035d72de6c3a66f7f329e72cd960e..7d911f9ffbdf63b2b2a45c3a3ee7063d006a5779:/nominatim/api/logging.py diff --git a/nominatim/api/logging.py b/nominatim/api/logging.py index 351da9a1..6c8b1b38 100644 --- a/nominatim/api/logging.py +++ b/nominatim/api/logging.py @@ -9,6 +9,7 @@ Functions for specialised logging with HTML output. """ from typing import Any, Iterator, Optional, List, Tuple, cast from contextvars import ContextVar +import datetime as dt import textwrap import io @@ -98,11 +99,16 @@ class HTMLLogger(BaseLogger): self.buffer = io.StringIO() + def _timestamp(self) -> None: + self._write(f'

[{dt.datetime.now()}]

') + + def get_buffer(self) -> str: return HTML_HEADER + self.buffer.getvalue() + HTML_FOOTER def function(self, func: str, **kwargs: Any) -> None: + self._timestamp() self._write(f"

Debug output for {func}()

\n

Parameters:

") for name, value in kwargs.items(): self._write(f'
{name}
{self._python_var(value)}
') @@ -110,14 +116,17 @@ class HTMLLogger(BaseLogger): def section(self, heading: str) -> None: + self._timestamp() self._write(f"

{heading}

") def comment(self, text: str) -> None: + self._timestamp() self._write(f"

{text}

") def var_dump(self, heading: str, var: Any) -> None: + self._timestamp() if callable(var): var = var() @@ -125,6 +134,7 @@ class HTMLLogger(BaseLogger): def table_dump(self, heading: str, rows: Iterator[Optional[List[Any]]]) -> None: + self._timestamp() head = next(rows) assert head self._write(f'') @@ -143,6 +153,7 @@ class HTMLLogger(BaseLogger): def result_dump(self, heading: str, results: Iterator[Tuple[Any, Any]]) -> None: """ Print a list of search results generated by the generator function. """ + self._timestamp() def format_osm(osm_object: Optional[Tuple[str, int]]) -> str: if not osm_object: return '-' @@ -167,12 +178,13 @@ class HTMLLogger(BaseLogger): self._write(f"rank={res.rank_address}, ") self._write(f"osm={format_osm(res.osm_object)}, ") self._write(f'cc={res.country_code}, ') - self._write(f'importance={res.importance or -1:.5f})') + self._write(f'importance={res.importance or float("nan"):.5f})') total += 1 self._write(f'TOTAL: {total}

') def sql(self, conn: AsyncConnection, statement: 'sa.Executable') -> None: + self._timestamp() sqlstr = self.format_sql(conn, statement) if CODE_HIGHLIGHT: sqlstr = highlight(sqlstr, PostgresLexer(), @@ -184,7 +196,7 @@ class HTMLLogger(BaseLogger): def _python_var(self, var: Any) -> str: if CODE_HIGHLIGHT: - fmt = highlight(repr(var), PythonLexer(), HtmlFormatter(nowrap=True)) + fmt = highlight(str(var), PythonLexer(), HtmlFormatter(nowrap=True)) return f'
{fmt}
' return f'{str(var)}' @@ -348,6 +360,26 @@ HTML_HEADER: str = """ padding: 3pt; border: solid lightgrey 0.1pt } + + table, th, tbody { + border: thin solid; + border-collapse: collapse; + } + td { + border-right: thin solid; + padding-left: 3pt; + padding-right: 3pt; + } + + .timestamp { + font-size: 0.8em; + color: darkblue; + width: calc(100% - 5pt); + text-align: right; + position: absolute; + left: 0; + margin-top: -5px; + }
{heading}