Skip to content

Commit f24b392

Browse files
authored
Merge pull request #730 from danielballan/log-config
Implement `--log-config` for all server CLI commands.
2 parents f44177e + aa67b54 commit f24b392

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

example_log_config.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
version: 1
2+
disable_existing_loggers: false
3+
filters:
4+
correlation_id:
5+
"()": asgi_correlation_id.CorrelationIdFilter
6+
uuid_length: 16
7+
default_value: '-'
8+
formatters:
9+
default:
10+
"()": uvicorn.logging.DefaultFormatter
11+
datefmt: "%Y-%m-%dT%H:%M:%S"
12+
format: '[%(asctime)s.%(msecs)03dZ] [%(correlation_id)s] %(levelprefix)s %(message)s'
13+
use_colors: true
14+
access:
15+
"()": uvicorn.logging.AccessFormatter
16+
datefmt: "%Y-%m-%dT%H:%M:%S"
17+
format: '[%(asctime)s.%(msecs)03dZ] [%(correlation_id)s] %(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s'
18+
handlers:
19+
default:
20+
formatter: default
21+
class: logging.StreamHandler
22+
filters:
23+
- correlation_id
24+
stream: ext://sys.stderr
25+
access:
26+
formatter: access
27+
class: logging.StreamHandler
28+
filters:
29+
- correlation_id
30+
stream: ext://sys.stdout
31+
loggers:
32+
uvicorn.error:
33+
level: INFO
34+
handlers:
35+
- default
36+
propagate: no
37+
uvicorn.access:
38+
level: INFO
39+
handlers:
40+
- access
41+
propagate: no

tiled/commandline/_serve.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,13 @@ def serve_directory(
199199

200200
import anyio
201201
import uvicorn
202+
from uvicorn.config import LOGGING_CONFIG
202203

203204
from ..client import from_uri as client_from_uri
204205

205206
print_admin_api_key_if_generated(web_app, host=host, port=port, force=generated)
206-
config = uvicorn.Config(web_app, host=host, port=port)
207+
log_config = log_config or LOGGING_CONFIG # fall back to uvicorn default
208+
config = uvicorn.Config(web_app, host=host, port=port, log_config=log_config)
207209
server = uvicorn.Server(config)
208210

209211
async def run_server():
@@ -334,6 +336,9 @@ def serve_catalog(
334336
"This verifies that the configuration is compatible with scaled (multi-process) deployments."
335337
),
336338
),
339+
log_config: Optional[str] = typer.Option(
340+
None, help="Custom uvicorn logging configuration file"
341+
),
337342
):
338343
"Serve a catalog."
339344
import urllib.parse
@@ -432,8 +437,10 @@ def serve_catalog(
432437
print_admin_api_key_if_generated(web_app, host=host, port=port)
433438

434439
import uvicorn
440+
from uvicorn.config import LOGGING_CONFIG
435441

436-
uvicorn.run(web_app, host=host, port=port)
442+
log_config = log_config or LOGGING_CONFIG # fall back to uvicorn default
443+
uvicorn.run(web_app, host=host, port=port, log_config=log_config)
437444

438445

439446
serve_app.command("catalog")(serve_catalog)
@@ -477,6 +484,9 @@ def serve_pyobject(
477484
"This verifies that the configuration is compatible with scaled (multi-process) deployments."
478485
),
479486
),
487+
log_config: Optional[str] = typer.Option(
488+
None, help="Custom uvicorn logging configuration file"
489+
),
480490
):
481491
"Serve a Tree instance from a Python module."
482492
from ..server.app import build_app, print_admin_api_key_if_generated
@@ -496,8 +506,10 @@ def serve_pyobject(
496506
print_admin_api_key_if_generated(web_app, host=host, port=port)
497507

498508
import uvicorn
509+
from uvicorn.config import LOGGING_CONFIG
499510

500-
uvicorn.run(web_app, host=host, port=port)
511+
log_config = log_config or LOGGING_CONFIG # fall back to uvicorn default
512+
uvicorn.run(web_app, host=host, port=port, log_config=log_config)
501513

502514

503515
@serve_app.command("demo")
@@ -571,6 +583,9 @@ def serve_config(
571583
"This verifies that the configuration is compatible with scaled (multi-process) deployments."
572584
),
573585
),
586+
log_config: Optional[str] = typer.Option(
587+
None, help="Custom uvicorn logging configuration file"
588+
),
574589
):
575590
"Serve a Tree as specified in configuration file(s)."
576591
import os
@@ -605,9 +620,10 @@ def serve_config(
605620

606621
# Extract config for uvicorn.
607622
uvicorn_kwargs = parsed_config.pop("uvicorn", {})
608-
# If --host is given, it overrides host in config. Same for --port.
623+
# If --host is given, it overrides host in config. Same for --port and --log-config.
609624
uvicorn_kwargs["host"] = host or uvicorn_kwargs.get("host", "127.0.0.1")
610625
uvicorn_kwargs["port"] = port or uvicorn_kwargs.get("port", 8000)
626+
uvicorn_kwargs["log_config"] = log_config or uvicorn_kwargs.get("log_config")
611627

612628
# This config was already validated when it was parsed. Do not re-validate.
613629
logger.info(f"Using configuration from {Path(config_path).absolute()}")

0 commit comments

Comments
 (0)