Skip to content

Commit 963dfb7

Browse files
Configurable log level in service and CLI (#100)
* Make log level configurable for CLI * Make log level in service configurable
1 parent 842aa86 commit 963dfb7

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/blueapi/cli/cli.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import itertools
22
import json
3+
import logging
34
from pathlib import Path
45
from typing import Dict, Mapping, Optional
56

@@ -46,11 +47,21 @@ def start_worker(config: Optional[Path]):
4647
help="Broker port",
4748
default=61613,
4849
)
50+
@click.option(
51+
"-l",
52+
"--log-level",
53+
type=str,
54+
help="Logger level: TRACE, DEBUG, INFO, WARNING, ERROR or CRITICAL, "
55+
"defaults to WARNING",
56+
default="WARNING",
57+
)
4958
@click.pass_context
50-
def controller(ctx, host: str, port: int):
59+
def controller(ctx, host: str, port: int, log_level: str):
5160
# if no command is supplied, run with the options passed
5261
if ctx.invoked_subcommand is None:
5362
print("Please invoke subcommand!")
63+
return
64+
logging.basicConfig(level=log_level)
5465
ctx.ensure_object(dict)
5566
client = AmqClient(StompMessagingTemplate.autoconfigured(StompConfig(host, port)))
5667
ctx.obj["client"] = client

src/blueapi/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class EnvironmentConfig:
2222
startup_script: Union[Path, str] = "blueapi.startup.example"
2323

2424

25+
@dataclass
26+
class LoggingConfig:
27+
level: str = "INFO"
28+
29+
2530
@dataclass
2631
class ApplicationConfig:
2732
"""
@@ -31,3 +36,4 @@ class ApplicationConfig:
3136

3237
stomp: StompConfig = field(default_factory=StompConfig)
3338
env: EnvironmentConfig = field(default_factory=EnvironmentConfig)
39+
logging: LoggingConfig = field(default_factory=LoggingConfig)

src/blueapi/service/app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
TaskResponse,
2020
)
2121

22-
logging.basicConfig(level=logging.INFO)
23-
2422

2523
class Service:
2624
_config: ApplicationConfig
@@ -36,6 +34,7 @@ def __init__(self, config: ApplicationConfig) -> None:
3634
self._template = StompMessagingTemplate.autoconfigured(config.stomp)
3735

3836
def run(self) -> None:
37+
logging.basicConfig(level=self._config.logging.level)
3938
self._worker.worker_events.subscribe(self._on_worker_event)
4039
self._worker.task_events.subscribe(self._on_task_event)
4140
self._worker.data_events.subscribe(self._on_data_event)

0 commit comments

Comments
 (0)