aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-node/src/meshbay_node/cli/dispatch.py
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-node/src/meshbay_node/cli/dispatch.py')
-rw-r--r--packages/meshbay-node/src/meshbay_node/cli/dispatch.py27
1 files changed, 26 insertions, 1 deletions
diff --git a/packages/meshbay-node/src/meshbay_node/cli/dispatch.py b/packages/meshbay-node/src/meshbay_node/cli/dispatch.py
index 849d589..fc5712f 100644
--- a/packages/meshbay-node/src/meshbay_node/cli/dispatch.py
+++ b/packages/meshbay-node/src/meshbay_node/cli/dispatch.py
@@ -28,11 +28,36 @@ def start():
"reset")
logging.basicConfig(
level=logging.ERROR if quiet else getattr(logging, args.log_level),
- format="%(asctime)s %(levelname)-8s %(name)s: %(message)s",
+ format=LOG_FORMAT,
)
+ if args.command is None:
+ add_log_file()
return args
+LOG_FORMAT = "%(asctime)s %(levelname)-8s %(name)s: %(message)s"
+
+
+def add_log_file() -> None:
+ """Also log the daemon to platform.log_file(), where there is one."""
+ from logging.handlers import RotatingFileHandler
+
+ from meshbay_node.platform import log_file
+ path = log_file()
+ if path is None:
+ return
+ try:
+ path.parent.mkdir(parents=True, exist_ok=True)
+ # delay: nothing is created until there is something to write.
+ handler = RotatingFileHandler(path, maxBytes=5 * 1024 * 1024, backupCount=3,
+ encoding="utf-8", delay=True)
+ except OSError as e:
+ logging.getLogger(__name__).warning("cannot open the log file %s: %s", path, e)
+ return
+ handler.setFormatter(logging.Formatter(LOG_FORMAT))
+ logging.getLogger().addHandler(handler)
+
+
# Each verb and what runs it. A command line naming none of them starts the
# daemon, which is daemon.main's to do.
VERBS = {