Skip to content

Commit 9f8e11a

Browse files
Fix subprocess logging during database creation (#726)
* fix subprocess logging * Fix branching --------- Co-authored-by: Dan Allan <dallan@bnl.gov>
1 parent 216c7b6 commit 9f8e11a

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

tiled/_tests/test_catalog.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,34 @@ async def test_constraints_on_parameter_and_num(a, assets):
528528
)
529529
],
530530
)
531+
532+
533+
@pytest.mark.asyncio
534+
async def test_init_db_logging(tmpdir, caplog):
535+
config = {
536+
"database": {
537+
"uri": "sqlite+aiosqlite://", # in-memory
538+
},
539+
"trees": [
540+
{
541+
"tree": "catalog",
542+
"path": "/",
543+
"args": {
544+
"uri": f"sqlite+aiosqlite:///{tmpdir}/catalog.db",
545+
"writable_storage": str(tmpdir / "data"),
546+
"init_if_not_exists": True,
547+
},
548+
},
549+
],
550+
}
551+
# Issue 721 notes that the logging of the subprocess that creates
552+
# a database logs normal things to error. This test looks at the log
553+
# and fails if an error log happens. This could catch anything that is
554+
# an error during the app build.
555+
import logging
556+
557+
with caplog.at_level(logging.INFO):
558+
app = build_app_from_config(config)
559+
for record in caplog.records:
560+
assert record.levelname != "ERROR", f"Error found creating app {record.msg}"
561+
assert app

tiled/catalog/adapter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,8 +1397,8 @@ def from_uri(
13971397
# Capture stdout and stderr from the subprocess and write to logging
13981398
stdout = process.stdout.decode()
13991399
stderr = process.stderr.decode()
1400-
logging.info(f"Subprocess stdout: {stdout}")
1401-
logging.error(f"Subprocess stderr: {stderr}")
1400+
logger.info(f"Subprocess stdout: {stdout}")
1401+
logger.info(f"Subprocess stderr: {stderr}")
14021402

14031403
parsed_url = make_url(uri)
14041404
if (parsed_url.get_dialect().name == "sqlite") and (

tiled/commandline/_serve.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ def serve_directory(
178178
adapters_by_mimetype=adapters_by_mimetype,
179179
)
180180
if verbose:
181+
from tiled.catalog.adapter import logger as catalog_logger
182+
183+
catalog_logger.addHandler(StreamHandler())
184+
catalog_logger.setLevel("INFO")
181185
register_logger.addHandler(StreamHandler())
182186
register_logger.setLevel("INFO")
183187
# Set the API key manually here, rather than letting the server do it,
@@ -345,6 +349,12 @@ def serve_catalog(
345349
log_timestamps: bool = typer.Option(
346350
False, help="Include timestamps in log output."
347351
),
352+
verbose: bool = typer.Option(
353+
False,
354+
"--verbose",
355+
"-v",
356+
help=("Log details of catalog creation."),
357+
),
348358
):
349359
"Serve a catalog."
350360
import urllib.parse
@@ -418,6 +428,13 @@ def serve_catalog(
418428
err=True,
419429
)
420430
raise typer.Abort()
431+
elif verbose:
432+
from logging import StreamHandler
433+
434+
from tiled.catalog.adapter import logger as catalog_logger
435+
436+
catalog_logger.addHandler(StreamHandler())
437+
catalog_logger.setLevel("INFO")
421438

422439
if write is None:
423440
typer.echo(

0 commit comments

Comments
 (0)