-
Notifications
You must be signed in to change notification settings - Fork 10
Feature: Log to file for GraphServer CLI
#248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 1 commit
9829315
bf557ac
a16531e
319f7a3
66101ef
9ded26b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| import argparse | ||
| import logging | ||
| import os | ||
| from datetime import datetime | ||
| from pathlib import Path | ||
|
|
||
| from ..graphserver import GraphService | ||
| from ..netprotocol import Address | ||
|
|
@@ -8,6 +12,14 @@ def add_address_argument(parser: argparse.ArgumentParser) -> None: | |
| parser.add_argument("--address", help="Address for GraphServer", default=None) | ||
|
|
||
|
|
||
| def add_log_file_argument(parser: argparse.ArgumentParser) -> None: | ||
| parser.add_argument( | ||
| "--log-file", | ||
| help="Path to the ezmsg service log file", | ||
| default=None, | ||
| ) | ||
|
|
||
|
|
||
| def add_compact_argument(parser: argparse.ArgumentParser) -> None: | ||
| parser.add_argument( | ||
| "-c", | ||
|
|
@@ -24,3 +36,51 @@ def graph_address_from_args(args: argparse.Namespace) -> Address: | |
| if args.address is None: | ||
| return GraphService.default_address() | ||
| return Address.from_string(args.address) | ||
|
|
||
|
|
||
| def resolve_log_file(args: argparse.Namespace, address: Address) -> Path: | ||
| if args.log_file is not None: | ||
| return Path(args.log_file).expanduser() | ||
|
|
||
| env_log_file = os.environ.get("EZMSG_LOG_FILE") | ||
| if env_log_file is not None: | ||
| return Path(env_log_file).expanduser() | ||
|
|
||
| if os.name == "nt": | ||
| data_home = Path( | ||
| os.environ.get("LOCALAPPDATA", Path.home() / "AppData" / "Local") | ||
| ) | ||
| else: | ||
| data_home = Path( | ||
| os.environ.get("XDG_DATA_HOME", Path.home() / ".local" / "share") | ||
| ) | ||
|
|
||
| log_dir = data_home / "ezmsg" / "logs" / f"GraphServer-{address.port}" | ||
| timestamp = datetime.now().strftime("%Y-%m-%dT%H-%M-%S") | ||
| return log_dir / f"{timestamp}.log" | ||
|
|
||
|
|
||
| def configure_log_file(log_file: Path) -> Path: | ||
| log_path = log_file.expanduser().resolve() | ||
| log_path.parent.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| logger = logging.getLogger("ezmsg") | ||
| if not any( | ||
| isinstance(handler, logging.FileHandler) | ||
| and getattr(handler, "baseFilename", None) == str(log_path) | ||
| for handler in logger.handlers | ||
| ): | ||
| formatter = next( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can just define the standard ezmsg formatter and use that. |
||
| ( | ||
| handler.formatter | ||
| for handler in logger.handlers | ||
| if handler.formatter is not None | ||
| ), | ||
| None, | ||
| ) | ||
| handler = logging.FileHandler(log_path, encoding="utf-8") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should probably deal with lifecycle management here, so that we don't leave open handlers we created. |
||
| if formatter is not None: | ||
| handler.setFormatter(formatter) | ||
| logger.addHandler(handler) | ||
|
|
||
| return log_path | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to plumb this into
run_command()too.