Skip to content

Commit

Permalink
Allow to set the verbosity level for console output in Overwatcher actor
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Dec 30, 2024
1 parent a5eebe9 commit 5f55393
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#44](https://vscode.dev/github/sdss/lvmgort/pull/44) RORR RID-019: disables the Overwatcher if rain is detected and requires a human to re-enable it when conditions are safe.
* [#46](https://github.com/sdss/lvmgort/pull/46) RORR RID-017: treat lost connectivity to the internet or LCO as an unsafe condition and close.
* Create the night log during the pre-observing task.
* The `gort overwatcher` command now accepts a `--verbose` flat that allow to set the verbosity level of the messages output to the console while the Overwatcher is running (the file log level is always DEBUG). The default level is now WARNING.

### 🏷️ Changed

Expand Down
18 changes: 17 additions & 1 deletion src/gort/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,22 @@ def gort(override_overwatcher: bool = False):
help="Path to the configuration file. Must include a "
"section called 'actor' or 'overwatcher.actor'.",
)
@click.option(
"--verbosity",
"-v",
type=click.Choice(
["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
case_sensitive=False,
),
help="Verbosity level for console output.",
)
@click.option("--dry-run", is_flag=True, help="Runs the actor in dry-run mode.")
@cli_coro()
async def overwatcher(config: str | None = None, dry_run: bool = False):
async def overwatcher(
config: str | None = None,
dry_run: bool = False,
verbosity: str | None = None,
):
"""Starts the overwatcher."""

from sdsstools import read_yaml_file
Expand All @@ -66,6 +79,9 @@ async def overwatcher(config: str | None = None, dry_run: bool = False):
)
actor_config = internal_config

if verbosity:
actor_config["console_verbosity"] = verbosity

actor = OverwatcherActor.from_config(actor_config, dry_run=dry_run)
await actor.start()

Expand Down
1 change: 1 addition & 0 deletions src/gort/etc/lvmgort.yml
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,4 @@ overwatcher:
host: localhost
port: 5672
log_dir: /data/logs/lvmgort/overwatcher
console_verbosity: WARNING
32 changes: 23 additions & 9 deletions src/gort/gort.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class GortClient(AMQPClient):
The user to connect to the exchange.
password
The password to connect to the exchange.
verbosity
The level of console logging verbosity. One of the standard logging levels.
use_rich_output
If :obj:`True`, uses ``rich`` to provide colourised tracebacks and
prettier outputs.
Expand All @@ -110,6 +112,7 @@ def __init__(
port: int = 5672,
user: str = "guest",
password: str = "guest",
verbosity: str = "INFO",
use_rich_output: bool = True,
log_file_path: str | pathlib.Path | Literal[False] | None = None,
):
Expand All @@ -118,7 +121,8 @@ def __init__(
self._console: Console

log = self._prepare_logger(
log_file_path,
log_file_path=log_file_path,
verbosity=verbosity,
use_rich_output=use_rich_output,
)

Expand All @@ -135,9 +139,14 @@ def __init__(
log=log,
)

# We need to set the verbosity again after the super().__init__() call
# because it resets the default log level to WARNING.
self.set_verbosity(verbosity)

def _prepare_logger(
self,
log_file_path: str | pathlib.Path | Literal[False] | None = None,
verbosity: str = "INFO",
use_rich_output: bool = True,
):
"""Creates a logger and start file logging."""
Expand All @@ -148,6 +157,8 @@ def _prepare_logger(
rich_handler_kwargs={"rich_tracebacks": use_rich_output},
)

self.set_verbosity(verbosity, log=log)

if log_file_path is None:
path = config["logging"]["path"]
sjd = get_sjd()
Expand Down Expand Up @@ -316,17 +327,26 @@ def connected(self):

return self.connection and self.connection.connection is not None

def set_verbosity(self, verbosity: str | int | None = None):
def set_verbosity(
self,
verbosity: str | int | None = None,
log: SDSSLogger | None = None,
):
"""Sets the level of verbosity to ``debug``, ``info``, or ``warning``.
Parameters
----------
verbosity
The level of verbosity. Can be a string level name, an integer, or
:obj:`None`, in which case the default verbosity will be used.
log
The logger to set the verbosity for. If :obj:`None`, the internal
logger will be used.
"""

log = log or self.log

verbosity = verbosity or "warning"
if isinstance(verbosity, int):
verbosity = logging.getLevelName(verbosity)
Expand All @@ -339,7 +359,7 @@ def set_verbosity(self, verbosity: str | int | None = None):

level_mapping = logging.getLevelNamesMapping()
if verbosity_level := level_mapping.get(verbosity.upper()):
self.log.sh.setLevel(verbosity_level)
log.sh.setLevel(verbosity_level)


class Gort(GortClient):
Expand All @@ -357,16 +377,13 @@ class Gort(GortClient):
If the Overwatcher is running an exception will be raised to prevent
GORT running commands that may interfere with it. If ``allow_overwatcher=True``
the exception will be suppressed.
verbosity
The level of logging verbosity.
"""

def __init__(
self,
*args,
override_overwatcher: bool | None = None,
verbosity: str | None = None,
config_file: str | pathlib.Path | None = None,
**kwargs,
):
Expand Down Expand Up @@ -404,9 +421,6 @@ def __init__(
self.observer = GortObserver(self)
self.observe_tile = self.observer.observe_tile

if verbosity:
self.set_verbosity(verbosity)

async def init(self) -> Self:
"""Initialises the client and all devices."""

Expand Down
10 changes: 8 additions & 2 deletions src/gort/overwatcher/actor/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ class OverwatcherActor(AMQPActor):
parser = overwatcher_cli
parser_raise_on_error = True

def __init__(self, *args, dry_run: bool = False, **kwargs):
def __init__(
self,
*args,
dry_run: bool = False,
console_verbosity: str = "WARNING",
**kwargs,
):
gort_root = pathlib.Path(gort.__file__).parent
schema = gort_root / "etc" / "actor_schema.json"

super().__init__(*args, schema=schema, version=gort.__version__, **kwargs)

self.overwatcher = Overwatcher(dry_run=dry_run)
self.overwatcher = Overwatcher(dry_run=dry_run, verbosity=console_verbosity)

self.log.info("OverwatcherActor initialised.")

Expand Down
2 changes: 1 addition & 1 deletion src/gort/overwatcher/overwatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def __new__(cls, *args, **kwargs):
def __init__(
self,
gort: Gort | None = None,
verbosity: str = "debug",
verbosity: str = "warning",
calibrations_file: str | pathlib.Path | None = None,
dry_run: bool = False,
**kwargs,
Expand Down

0 comments on commit 5f55393

Please sign in to comment.